Skip to content

Payload

Payload Module to implement functions to process Terrarium payload.

This code processes a Terraform (HCL) or YAML payload file that includes various settings to auto-configure a OpenText Content Management environment, including Content Server (OTCS), OTDS; AppWorks, CoreShare, M365.

  • WebHooks (URLs) to call (e.g. to start-up external services or applications)
  • OTDS partitions and OAuth clients
  • OTDS trusted sites and system attributes
  • OTDS licenses
  • Content Server users and groups
  • Microsoft 365 user, groups, and teams
  • Salesforce users and groups
  • SuccessFactors users
  • Core Share users and groups
  • Content Server Admin Settings (LLConfig)
  • Content Server External System Connections (SAP, SuccessFactors, ...)
  • Content Server Transport Packages (scenarios and demo content)
  • Content Server CS Applications (typically based on Web Reports)
  • Content Server Web Reports to run
  • Content Server Workspaces to create (incl. members, workspace relationships)
  • Content Server User photos, user favorites and user settings
  • Content Server Items to create and permissions to apply
  • Content Server Items to rename
  • Content Server Documents to generate (from templates)
  • Content Server Assignments (used e.g. for Government scenario)
  • Content Server Records Management settings, Security Clearance, Supplemental Markings, and Holds
  • SAP RFCs (Remote Function Calls)
  • Commands to execute in Kubernetes Pods
  • Browser Automations (for things that cannot be automated via an API)

This code typically runs in a container as part of the cloud automation.

Payload

Class Payload is used to process Terrarium payload.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486
  487
  488
  489
  490
  491
  492
  493
  494
  495
  496
  497
  498
  499
  500
  501
  502
  503
  504
  505
  506
  507
  508
  509
  510
  511
  512
  513
  514
  515
  516
  517
  518
  519
  520
  521
  522
  523
  524
  525
  526
  527
  528
  529
  530
  531
  532
  533
  534
  535
  536
  537
  538
  539
  540
  541
  542
  543
  544
  545
  546
  547
  548
  549
  550
  551
  552
  553
  554
  555
  556
  557
  558
  559
  560
  561
  562
  563
  564
  565
  566
  567
  568
  569
  570
  571
  572
  573
  574
  575
  576
  577
  578
  579
  580
  581
  582
  583
  584
  585
  586
  587
  588
  589
  590
  591
  592
  593
  594
  595
  596
  597
  598
  599
  600
  601
  602
  603
  604
  605
  606
  607
  608
  609
  610
  611
  612
  613
  614
  615
  616
  617
  618
  619
  620
  621
  622
  623
  624
  625
  626
  627
  628
  629
  630
  631
  632
  633
  634
  635
  636
  637
  638
  639
  640
  641
  642
  643
  644
  645
  646
  647
  648
  649
  650
  651
  652
  653
  654
  655
  656
  657
  658
  659
  660
  661
  662
  663
  664
  665
  666
  667
  668
  669
  670
  671
  672
  673
  674
  675
  676
  677
  678
  679
  680
  681
  682
  683
  684
  685
  686
  687
  688
  689
  690
  691
  692
  693
  694
  695
  696
  697
  698
  699
  700
  701
  702
  703
  704
  705
  706
  707
  708
  709
  710
  711
  712
  713
  714
  715
  716
  717
  718
  719
  720
  721
  722
  723
  724
  725
  726
  727
  728
  729
  730
  731
  732
  733
  734
  735
  736
  737
  738
  739
  740
  741
  742
  743
  744
  745
  746
  747
  748
  749
  750
  751
  752
  753
  754
  755
  756
  757
  758
  759
  760
  761
  762
  763
  764
  765
  766
  767
  768
  769
  770
  771
  772
  773
  774
  775
  776
  777
  778
  779
  780
  781
  782
  783
  784
  785
  786
  787
  788
  789
  790
  791
  792
  793
  794
  795
  796
  797
  798
  799
  800
  801
  802
  803
  804
  805
  806
  807
  808
  809
  810
  811
  812
  813
  814
  815
  816
  817
  818
  819
  820
  821
  822
  823
  824
  825
  826
  827
  828
  829
  830
  831
  832
  833
  834
  835
  836
  837
  838
  839
  840
  841
  842
  843
  844
  845
  846
  847
  848
  849
  850
  851
  852
  853
  854
  855
  856
  857
  858
  859
  860
  861
  862
  863
  864
  865
  866
  867
  868
  869
  870
  871
  872
  873
  874
  875
  876
  877
  878
  879
  880
  881
  882
  883
  884
  885
  886
  887
  888
  889
  890
  891
  892
  893
  894
  895
  896
  897
  898
  899
  900
  901
  902
  903
  904
  905
  906
  907
  908
  909
  910
  911
  912
  913
  914
  915
  916
  917
  918
  919
  920
  921
  922
  923
  924
  925
  926
  927
  928
  929
  930
  931
  932
  933
  934
  935
  936
  937
  938
  939
  940
  941
  942
  943
  944
  945
  946
  947
  948
  949
  950
  951
  952
  953
  954
  955
  956
  957
  958
  959
  960
  961
  962
  963
  964
  965
  966
  967
  968
  969
  970
  971
  972
  973
  974
  975
  976
  977
  978
  979
  980
  981
  982
  983
  984
  985
  986
  987
  988
  989
  990
  991
  992
  993
  994
  995
  996
  997
  998
  999
 1000
 1001
 1002
 1003
 1004
 1005
 1006
 1007
 1008
 1009
 1010
 1011
 1012
 1013
 1014
 1015
 1016
 1017
 1018
 1019
 1020
 1021
 1022
 1023
 1024
 1025
 1026
 1027
 1028
 1029
 1030
 1031
 1032
 1033
 1034
 1035
 1036
 1037
 1038
 1039
 1040
 1041
 1042
 1043
 1044
 1045
 1046
 1047
 1048
 1049
 1050
 1051
 1052
 1053
 1054
 1055
 1056
 1057
 1058
 1059
 1060
 1061
 1062
 1063
 1064
 1065
 1066
 1067
 1068
 1069
 1070
 1071
 1072
 1073
 1074
 1075
 1076
 1077
 1078
 1079
 1080
 1081
 1082
 1083
 1084
 1085
 1086
 1087
 1088
 1089
 1090
 1091
 1092
 1093
 1094
 1095
 1096
 1097
 1098
 1099
 1100
 1101
 1102
 1103
 1104
 1105
 1106
 1107
 1108
 1109
 1110
 1111
 1112
 1113
 1114
 1115
 1116
 1117
 1118
 1119
 1120
 1121
 1122
 1123
 1124
 1125
 1126
 1127
 1128
 1129
 1130
 1131
 1132
 1133
 1134
 1135
 1136
 1137
 1138
 1139
 1140
 1141
 1142
 1143
 1144
 1145
 1146
 1147
 1148
 1149
 1150
 1151
 1152
 1153
 1154
 1155
 1156
 1157
 1158
 1159
 1160
 1161
 1162
 1163
 1164
 1165
 1166
 1167
 1168
 1169
 1170
 1171
 1172
 1173
 1174
 1175
 1176
 1177
 1178
 1179
 1180
 1181
 1182
 1183
 1184
 1185
 1186
 1187
 1188
 1189
 1190
 1191
 1192
 1193
 1194
 1195
 1196
 1197
 1198
 1199
 1200
 1201
 1202
 1203
 1204
 1205
 1206
 1207
 1208
 1209
 1210
 1211
 1212
 1213
 1214
 1215
 1216
 1217
 1218
 1219
 1220
 1221
 1222
 1223
 1224
 1225
 1226
 1227
 1228
 1229
 1230
 1231
 1232
 1233
 1234
 1235
 1236
 1237
 1238
 1239
 1240
 1241
 1242
 1243
 1244
 1245
 1246
 1247
 1248
 1249
 1250
 1251
 1252
 1253
 1254
 1255
 1256
 1257
 1258
 1259
 1260
 1261
 1262
 1263
 1264
 1265
 1266
 1267
 1268
 1269
 1270
 1271
 1272
 1273
 1274
 1275
 1276
 1277
 1278
 1279
 1280
 1281
 1282
 1283
 1284
 1285
 1286
 1287
 1288
 1289
 1290
 1291
 1292
 1293
 1294
 1295
 1296
 1297
 1298
 1299
 1300
 1301
 1302
 1303
 1304
 1305
 1306
 1307
 1308
 1309
 1310
 1311
 1312
 1313
 1314
 1315
 1316
 1317
 1318
 1319
 1320
 1321
 1322
 1323
 1324
 1325
 1326
 1327
 1328
 1329
 1330
 1331
 1332
 1333
 1334
 1335
 1336
 1337
 1338
 1339
 1340
 1341
 1342
 1343
 1344
 1345
 1346
 1347
 1348
 1349
 1350
 1351
 1352
 1353
 1354
 1355
 1356
 1357
 1358
 1359
 1360
 1361
 1362
 1363
 1364
 1365
 1366
 1367
 1368
 1369
 1370
 1371
 1372
 1373
 1374
 1375
 1376
 1377
 1378
 1379
 1380
 1381
 1382
 1383
 1384
 1385
 1386
 1387
 1388
 1389
 1390
 1391
 1392
 1393
 1394
 1395
 1396
 1397
 1398
 1399
 1400
 1401
 1402
 1403
 1404
 1405
 1406
 1407
 1408
 1409
 1410
 1411
 1412
 1413
 1414
 1415
 1416
 1417
 1418
 1419
 1420
 1421
 1422
 1423
 1424
 1425
 1426
 1427
 1428
 1429
 1430
 1431
 1432
 1433
 1434
 1435
 1436
 1437
 1438
 1439
 1440
 1441
 1442
 1443
 1444
 1445
 1446
 1447
 1448
 1449
 1450
 1451
 1452
 1453
 1454
 1455
 1456
 1457
 1458
 1459
 1460
 1461
 1462
 1463
 1464
 1465
 1466
 1467
 1468
 1469
 1470
 1471
 1472
 1473
 1474
 1475
 1476
 1477
 1478
 1479
 1480
 1481
 1482
 1483
 1484
 1485
 1486
 1487
 1488
 1489
 1490
 1491
 1492
 1493
 1494
 1495
 1496
 1497
 1498
 1499
 1500
 1501
 1502
 1503
 1504
 1505
 1506
 1507
 1508
 1509
 1510
 1511
 1512
 1513
 1514
 1515
 1516
 1517
 1518
 1519
 1520
 1521
 1522
 1523
 1524
 1525
 1526
 1527
 1528
 1529
 1530
 1531
 1532
 1533
 1534
 1535
 1536
 1537
 1538
 1539
 1540
 1541
 1542
 1543
 1544
 1545
 1546
 1547
 1548
 1549
 1550
 1551
 1552
 1553
 1554
 1555
 1556
 1557
 1558
 1559
 1560
 1561
 1562
 1563
 1564
 1565
 1566
 1567
 1568
 1569
 1570
 1571
 1572
 1573
 1574
 1575
 1576
 1577
 1578
 1579
 1580
 1581
 1582
 1583
 1584
 1585
 1586
 1587
 1588
 1589
 1590
 1591
 1592
 1593
 1594
 1595
 1596
 1597
 1598
 1599
 1600
 1601
 1602
 1603
 1604
 1605
 1606
 1607
 1608
 1609
 1610
 1611
 1612
 1613
 1614
 1615
 1616
 1617
 1618
 1619
 1620
 1621
 1622
 1623
 1624
 1625
 1626
 1627
 1628
 1629
 1630
 1631
 1632
 1633
 1634
 1635
 1636
 1637
 1638
 1639
 1640
 1641
 1642
 1643
 1644
 1645
 1646
 1647
 1648
 1649
 1650
 1651
 1652
 1653
 1654
 1655
 1656
 1657
 1658
 1659
 1660
 1661
 1662
 1663
 1664
 1665
 1666
 1667
 1668
 1669
 1670
 1671
 1672
 1673
 1674
 1675
 1676
 1677
 1678
 1679
 1680
 1681
 1682
 1683
 1684
 1685
 1686
 1687
 1688
 1689
 1690
 1691
 1692
 1693
 1694
 1695
 1696
 1697
 1698
 1699
 1700
 1701
 1702
 1703
 1704
 1705
 1706
 1707
 1708
 1709
 1710
 1711
 1712
 1713
 1714
 1715
 1716
 1717
 1718
 1719
 1720
 1721
 1722
 1723
 1724
 1725
 1726
 1727
 1728
 1729
 1730
 1731
 1732
 1733
 1734
 1735
 1736
 1737
 1738
 1739
 1740
 1741
 1742
 1743
 1744
 1745
 1746
 1747
 1748
 1749
 1750
 1751
 1752
 1753
 1754
 1755
 1756
 1757
 1758
 1759
 1760
 1761
 1762
 1763
 1764
 1765
 1766
 1767
 1768
 1769
 1770
 1771
 1772
 1773
 1774
 1775
 1776
 1777
 1778
 1779
 1780
 1781
 1782
 1783
 1784
 1785
 1786
 1787
 1788
 1789
 1790
 1791
 1792
 1793
 1794
 1795
 1796
 1797
 1798
 1799
 1800
 1801
 1802
 1803
 1804
 1805
 1806
 1807
 1808
 1809
 1810
 1811
 1812
 1813
 1814
 1815
 1816
 1817
 1818
 1819
 1820
 1821
 1822
 1823
 1824
 1825
 1826
 1827
 1828
 1829
 1830
 1831
 1832
 1833
 1834
 1835
 1836
 1837
 1838
 1839
 1840
 1841
 1842
 1843
 1844
 1845
 1846
 1847
 1848
 1849
 1850
 1851
 1852
 1853
 1854
 1855
 1856
 1857
 1858
 1859
 1860
 1861
 1862
 1863
 1864
 1865
 1866
 1867
 1868
 1869
 1870
 1871
 1872
 1873
 1874
 1875
 1876
 1877
 1878
 1879
 1880
 1881
 1882
 1883
 1884
 1885
 1886
 1887
 1888
 1889
 1890
 1891
 1892
 1893
 1894
 1895
 1896
 1897
 1898
 1899
 1900
 1901
 1902
 1903
 1904
 1905
 1906
 1907
 1908
 1909
 1910
 1911
 1912
 1913
 1914
 1915
 1916
 1917
 1918
 1919
 1920
 1921
 1922
 1923
 1924
 1925
 1926
 1927
 1928
 1929
 1930
 1931
 1932
 1933
 1934
 1935
 1936
 1937
 1938
 1939
 1940
 1941
 1942
 1943
 1944
 1945
 1946
 1947
 1948
 1949
 1950
 1951
 1952
 1953
 1954
 1955
 1956
 1957
 1958
 1959
 1960
 1961
 1962
 1963
 1964
 1965
 1966
 1967
 1968
 1969
 1970
 1971
 1972
 1973
 1974
 1975
 1976
 1977
 1978
 1979
 1980
 1981
 1982
 1983
 1984
 1985
 1986
 1987
 1988
 1989
 1990
 1991
 1992
 1993
 1994
 1995
 1996
 1997
 1998
 1999
 2000
 2001
 2002
 2003
 2004
 2005
 2006
 2007
 2008
 2009
 2010
 2011
 2012
 2013
 2014
 2015
 2016
 2017
 2018
 2019
 2020
 2021
 2022
 2023
 2024
 2025
 2026
 2027
 2028
 2029
 2030
 2031
 2032
 2033
 2034
 2035
 2036
 2037
 2038
 2039
 2040
 2041
 2042
 2043
 2044
 2045
 2046
 2047
 2048
 2049
 2050
 2051
 2052
 2053
 2054
 2055
 2056
 2057
 2058
 2059
 2060
 2061
 2062
 2063
 2064
 2065
 2066
 2067
 2068
 2069
 2070
 2071
 2072
 2073
 2074
 2075
 2076
 2077
 2078
 2079
 2080
 2081
 2082
 2083
 2084
 2085
 2086
 2087
 2088
 2089
 2090
 2091
 2092
 2093
 2094
 2095
 2096
 2097
 2098
 2099
 2100
 2101
 2102
 2103
 2104
 2105
 2106
 2107
 2108
 2109
 2110
 2111
 2112
 2113
 2114
 2115
 2116
 2117
 2118
 2119
 2120
 2121
 2122
 2123
 2124
 2125
 2126
 2127
 2128
 2129
 2130
 2131
 2132
 2133
 2134
 2135
 2136
 2137
 2138
 2139
 2140
 2141
 2142
 2143
 2144
 2145
 2146
 2147
 2148
 2149
 2150
 2151
 2152
 2153
 2154
 2155
 2156
 2157
 2158
 2159
 2160
 2161
 2162
 2163
 2164
 2165
 2166
 2167
 2168
 2169
 2170
 2171
 2172
 2173
 2174
 2175
 2176
 2177
 2178
 2179
 2180
 2181
 2182
 2183
 2184
 2185
 2186
 2187
 2188
 2189
 2190
 2191
 2192
 2193
 2194
 2195
 2196
 2197
 2198
 2199
 2200
 2201
 2202
 2203
 2204
 2205
 2206
 2207
 2208
 2209
 2210
 2211
 2212
 2213
 2214
 2215
 2216
 2217
 2218
 2219
 2220
 2221
 2222
 2223
 2224
 2225
 2226
 2227
 2228
 2229
 2230
 2231
 2232
 2233
 2234
 2235
 2236
 2237
 2238
 2239
 2240
 2241
 2242
 2243
 2244
 2245
 2246
 2247
 2248
 2249
 2250
 2251
 2252
 2253
 2254
 2255
 2256
 2257
 2258
 2259
 2260
 2261
 2262
 2263
 2264
 2265
 2266
 2267
 2268
 2269
 2270
 2271
 2272
 2273
 2274
 2275
 2276
 2277
 2278
 2279
 2280
 2281
 2282
 2283
 2284
 2285
 2286
 2287
 2288
 2289
 2290
 2291
 2292
 2293
 2294
 2295
 2296
 2297
 2298
 2299
 2300
 2301
 2302
 2303
 2304
 2305
 2306
 2307
 2308
 2309
 2310
 2311
 2312
 2313
 2314
 2315
 2316
 2317
 2318
 2319
 2320
 2321
 2322
 2323
 2324
 2325
 2326
 2327
 2328
 2329
 2330
 2331
 2332
 2333
 2334
 2335
 2336
 2337
 2338
 2339
 2340
 2341
 2342
 2343
 2344
 2345
 2346
 2347
 2348
 2349
 2350
 2351
 2352
 2353
 2354
 2355
 2356
 2357
 2358
 2359
 2360
 2361
 2362
 2363
 2364
 2365
 2366
 2367
 2368
 2369
 2370
 2371
 2372
 2373
 2374
 2375
 2376
 2377
 2378
 2379
 2380
 2381
 2382
 2383
 2384
 2385
 2386
 2387
 2388
 2389
 2390
 2391
 2392
 2393
 2394
 2395
 2396
 2397
 2398
 2399
 2400
 2401
 2402
 2403
 2404
 2405
 2406
 2407
 2408
 2409
 2410
 2411
 2412
 2413
 2414
 2415
 2416
 2417
 2418
 2419
 2420
 2421
 2422
 2423
 2424
 2425
 2426
 2427
 2428
 2429
 2430
 2431
 2432
 2433
 2434
 2435
 2436
 2437
 2438
 2439
 2440
 2441
 2442
 2443
 2444
 2445
 2446
 2447
 2448
 2449
 2450
 2451
 2452
 2453
 2454
 2455
 2456
 2457
 2458
 2459
 2460
 2461
 2462
 2463
 2464
 2465
 2466
 2467
 2468
 2469
 2470
 2471
 2472
 2473
 2474
 2475
 2476
 2477
 2478
 2479
 2480
 2481
 2482
 2483
 2484
 2485
 2486
 2487
 2488
 2489
 2490
 2491
 2492
 2493
 2494
 2495
 2496
 2497
 2498
 2499
 2500
 2501
 2502
 2503
 2504
 2505
 2506
 2507
 2508
 2509
 2510
 2511
 2512
 2513
 2514
 2515
 2516
 2517
 2518
 2519
 2520
 2521
 2522
 2523
 2524
 2525
 2526
 2527
 2528
 2529
 2530
 2531
 2532
 2533
 2534
 2535
 2536
 2537
 2538
 2539
 2540
 2541
 2542
 2543
 2544
 2545
 2546
 2547
 2548
 2549
 2550
 2551
 2552
 2553
 2554
 2555
 2556
 2557
 2558
 2559
 2560
 2561
 2562
 2563
 2564
 2565
 2566
 2567
 2568
 2569
 2570
 2571
 2572
 2573
 2574
 2575
 2576
 2577
 2578
 2579
 2580
 2581
 2582
 2583
 2584
 2585
 2586
 2587
 2588
 2589
 2590
 2591
 2592
 2593
 2594
 2595
 2596
 2597
 2598
 2599
 2600
 2601
 2602
 2603
 2604
 2605
 2606
 2607
 2608
 2609
 2610
 2611
 2612
 2613
 2614
 2615
 2616
 2617
 2618
 2619
 2620
 2621
 2622
 2623
 2624
 2625
 2626
 2627
 2628
 2629
 2630
 2631
 2632
 2633
 2634
 2635
 2636
 2637
 2638
 2639
 2640
 2641
 2642
 2643
 2644
 2645
 2646
 2647
 2648
 2649
 2650
 2651
 2652
 2653
 2654
 2655
 2656
 2657
 2658
 2659
 2660
 2661
 2662
 2663
 2664
 2665
 2666
 2667
 2668
 2669
 2670
 2671
 2672
 2673
 2674
 2675
 2676
 2677
 2678
 2679
 2680
 2681
 2682
 2683
 2684
 2685
 2686
 2687
 2688
 2689
 2690
 2691
 2692
 2693
 2694
 2695
 2696
 2697
 2698
 2699
 2700
 2701
 2702
 2703
 2704
 2705
 2706
 2707
 2708
 2709
 2710
 2711
 2712
 2713
 2714
 2715
 2716
 2717
 2718
 2719
 2720
 2721
 2722
 2723
 2724
 2725
 2726
 2727
 2728
 2729
 2730
 2731
 2732
 2733
 2734
 2735
 2736
 2737
 2738
 2739
 2740
 2741
 2742
 2743
 2744
 2745
 2746
 2747
 2748
 2749
 2750
 2751
 2752
 2753
 2754
 2755
 2756
 2757
 2758
 2759
 2760
 2761
 2762
 2763
 2764
 2765
 2766
 2767
 2768
 2769
 2770
 2771
 2772
 2773
 2774
 2775
 2776
 2777
 2778
 2779
 2780
 2781
 2782
 2783
 2784
 2785
 2786
 2787
 2788
 2789
 2790
 2791
 2792
 2793
 2794
 2795
 2796
 2797
 2798
 2799
 2800
 2801
 2802
 2803
 2804
 2805
 2806
 2807
 2808
 2809
 2810
 2811
 2812
 2813
 2814
 2815
 2816
 2817
 2818
 2819
 2820
 2821
 2822
 2823
 2824
 2825
 2826
 2827
 2828
 2829
 2830
 2831
 2832
 2833
 2834
 2835
 2836
 2837
 2838
 2839
 2840
 2841
 2842
 2843
 2844
 2845
 2846
 2847
 2848
 2849
 2850
 2851
 2852
 2853
 2854
 2855
 2856
 2857
 2858
 2859
 2860
 2861
 2862
 2863
 2864
 2865
 2866
 2867
 2868
 2869
 2870
 2871
 2872
 2873
 2874
 2875
 2876
 2877
 2878
 2879
 2880
 2881
 2882
 2883
 2884
 2885
 2886
 2887
 2888
 2889
 2890
 2891
 2892
 2893
 2894
 2895
 2896
 2897
 2898
 2899
 2900
 2901
 2902
 2903
 2904
 2905
 2906
 2907
 2908
 2909
 2910
 2911
 2912
 2913
 2914
 2915
 2916
 2917
 2918
 2919
 2920
 2921
 2922
 2923
 2924
 2925
 2926
 2927
 2928
 2929
 2930
 2931
 2932
 2933
 2934
 2935
 2936
 2937
 2938
 2939
 2940
 2941
 2942
 2943
 2944
 2945
 2946
 2947
 2948
 2949
 2950
 2951
 2952
 2953
 2954
 2955
 2956
 2957
 2958
 2959
 2960
 2961
 2962
 2963
 2964
 2965
 2966
 2967
 2968
 2969
 2970
 2971
 2972
 2973
 2974
 2975
 2976
 2977
 2978
 2979
 2980
 2981
 2982
 2983
 2984
 2985
 2986
 2987
 2988
 2989
 2990
 2991
 2992
 2993
 2994
 2995
 2996
 2997
 2998
 2999
 3000
 3001
 3002
 3003
 3004
 3005
 3006
 3007
 3008
 3009
 3010
 3011
 3012
 3013
 3014
 3015
 3016
 3017
 3018
 3019
 3020
 3021
 3022
 3023
 3024
 3025
 3026
 3027
 3028
 3029
 3030
 3031
 3032
 3033
 3034
 3035
 3036
 3037
 3038
 3039
 3040
 3041
 3042
 3043
 3044
 3045
 3046
 3047
 3048
 3049
 3050
 3051
 3052
 3053
 3054
 3055
 3056
 3057
 3058
 3059
 3060
 3061
 3062
 3063
 3064
 3065
 3066
 3067
 3068
 3069
 3070
 3071
 3072
 3073
 3074
 3075
 3076
 3077
 3078
 3079
 3080
 3081
 3082
 3083
 3084
 3085
 3086
 3087
 3088
 3089
 3090
 3091
 3092
 3093
 3094
 3095
 3096
 3097
 3098
 3099
 3100
 3101
 3102
 3103
 3104
 3105
 3106
 3107
 3108
 3109
 3110
 3111
 3112
 3113
 3114
 3115
 3116
 3117
 3118
 3119
 3120
 3121
 3122
 3123
 3124
 3125
 3126
 3127
 3128
 3129
 3130
 3131
 3132
 3133
 3134
 3135
 3136
 3137
 3138
 3139
 3140
 3141
 3142
 3143
 3144
 3145
 3146
 3147
 3148
 3149
 3150
 3151
 3152
 3153
 3154
 3155
 3156
 3157
 3158
 3159
 3160
 3161
 3162
 3163
 3164
 3165
 3166
 3167
 3168
 3169
 3170
 3171
 3172
 3173
 3174
 3175
 3176
 3177
 3178
 3179
 3180
 3181
 3182
 3183
 3184
 3185
 3186
 3187
 3188
 3189
 3190
 3191
 3192
 3193
 3194
 3195
 3196
 3197
 3198
 3199
 3200
 3201
 3202
 3203
 3204
 3205
 3206
 3207
 3208
 3209
 3210
 3211
 3212
 3213
 3214
 3215
 3216
 3217
 3218
 3219
 3220
 3221
 3222
 3223
 3224
 3225
 3226
 3227
 3228
 3229
 3230
 3231
 3232
 3233
 3234
 3235
 3236
 3237
 3238
 3239
 3240
 3241
 3242
 3243
 3244
 3245
 3246
 3247
 3248
 3249
 3250
 3251
 3252
 3253
 3254
 3255
 3256
 3257
 3258
 3259
 3260
 3261
 3262
 3263
 3264
 3265
 3266
 3267
 3268
 3269
 3270
 3271
 3272
 3273
 3274
 3275
 3276
 3277
 3278
 3279
 3280
 3281
 3282
 3283
 3284
 3285
 3286
 3287
 3288
 3289
 3290
 3291
 3292
 3293
 3294
 3295
 3296
 3297
 3298
 3299
 3300
 3301
 3302
 3303
 3304
 3305
 3306
 3307
 3308
 3309
 3310
 3311
 3312
 3313
 3314
 3315
 3316
 3317
 3318
 3319
 3320
 3321
 3322
 3323
 3324
 3325
 3326
 3327
 3328
 3329
 3330
 3331
 3332
 3333
 3334
 3335
 3336
 3337
 3338
 3339
 3340
 3341
 3342
 3343
 3344
 3345
 3346
 3347
 3348
 3349
 3350
 3351
 3352
 3353
 3354
 3355
 3356
 3357
 3358
 3359
 3360
 3361
 3362
 3363
 3364
 3365
 3366
 3367
 3368
 3369
 3370
 3371
 3372
 3373
 3374
 3375
 3376
 3377
 3378
 3379
 3380
 3381
 3382
 3383
 3384
 3385
 3386
 3387
 3388
 3389
 3390
 3391
 3392
 3393
 3394
 3395
 3396
 3397
 3398
 3399
 3400
 3401
 3402
 3403
 3404
 3405
 3406
 3407
 3408
 3409
 3410
 3411
 3412
 3413
 3414
 3415
 3416
 3417
 3418
 3419
 3420
 3421
 3422
 3423
 3424
 3425
 3426
 3427
 3428
 3429
 3430
 3431
 3432
 3433
 3434
 3435
 3436
 3437
 3438
 3439
 3440
 3441
 3442
 3443
 3444
 3445
 3446
 3447
 3448
 3449
 3450
 3451
 3452
 3453
 3454
 3455
 3456
 3457
 3458
 3459
 3460
 3461
 3462
 3463
 3464
 3465
 3466
 3467
 3468
 3469
 3470
 3471
 3472
 3473
 3474
 3475
 3476
 3477
 3478
 3479
 3480
 3481
 3482
 3483
 3484
 3485
 3486
 3487
 3488
 3489
 3490
 3491
 3492
 3493
 3494
 3495
 3496
 3497
 3498
 3499
 3500
 3501
 3502
 3503
 3504
 3505
 3506
 3507
 3508
 3509
 3510
 3511
 3512
 3513
 3514
 3515
 3516
 3517
 3518
 3519
 3520
 3521
 3522
 3523
 3524
 3525
 3526
 3527
 3528
 3529
 3530
 3531
 3532
 3533
 3534
 3535
 3536
 3537
 3538
 3539
 3540
 3541
 3542
 3543
 3544
 3545
 3546
 3547
 3548
 3549
 3550
 3551
 3552
 3553
 3554
 3555
 3556
 3557
 3558
 3559
 3560
 3561
 3562
 3563
 3564
 3565
 3566
 3567
 3568
 3569
 3570
 3571
 3572
 3573
 3574
 3575
 3576
 3577
 3578
 3579
 3580
 3581
 3582
 3583
 3584
 3585
 3586
 3587
 3588
 3589
 3590
 3591
 3592
 3593
 3594
 3595
 3596
 3597
 3598
 3599
 3600
 3601
 3602
 3603
 3604
 3605
 3606
 3607
 3608
 3609
 3610
 3611
 3612
 3613
 3614
 3615
 3616
 3617
 3618
 3619
 3620
 3621
 3622
 3623
 3624
 3625
 3626
 3627
 3628
 3629
 3630
 3631
 3632
 3633
 3634
 3635
 3636
 3637
 3638
 3639
 3640
 3641
 3642
 3643
 3644
 3645
 3646
 3647
 3648
 3649
 3650
 3651
 3652
 3653
 3654
 3655
 3656
 3657
 3658
 3659
 3660
 3661
 3662
 3663
 3664
 3665
 3666
 3667
 3668
 3669
 3670
 3671
 3672
 3673
 3674
 3675
 3676
 3677
 3678
 3679
 3680
 3681
 3682
 3683
 3684
 3685
 3686
 3687
 3688
 3689
 3690
 3691
 3692
 3693
 3694
 3695
 3696
 3697
 3698
 3699
 3700
 3701
 3702
 3703
 3704
 3705
 3706
 3707
 3708
 3709
 3710
 3711
 3712
 3713
 3714
 3715
 3716
 3717
 3718
 3719
 3720
 3721
 3722
 3723
 3724
 3725
 3726
 3727
 3728
 3729
 3730
 3731
 3732
 3733
 3734
 3735
 3736
 3737
 3738
 3739
 3740
 3741
 3742
 3743
 3744
 3745
 3746
 3747
 3748
 3749
 3750
 3751
 3752
 3753
 3754
 3755
 3756
 3757
 3758
 3759
 3760
 3761
 3762
 3763
 3764
 3765
 3766
 3767
 3768
 3769
 3770
 3771
 3772
 3773
 3774
 3775
 3776
 3777
 3778
 3779
 3780
 3781
 3782
 3783
 3784
 3785
 3786
 3787
 3788
 3789
 3790
 3791
 3792
 3793
 3794
 3795
 3796
 3797
 3798
 3799
 3800
 3801
 3802
 3803
 3804
 3805
 3806
 3807
 3808
 3809
 3810
 3811
 3812
 3813
 3814
 3815
 3816
 3817
 3818
 3819
 3820
 3821
 3822
 3823
 3824
 3825
 3826
 3827
 3828
 3829
 3830
 3831
 3832
 3833
 3834
 3835
 3836
 3837
 3838
 3839
 3840
 3841
 3842
 3843
 3844
 3845
 3846
 3847
 3848
 3849
 3850
 3851
 3852
 3853
 3854
 3855
 3856
 3857
 3858
 3859
 3860
 3861
 3862
 3863
 3864
 3865
 3866
 3867
 3868
 3869
 3870
 3871
 3872
 3873
 3874
 3875
 3876
 3877
 3878
 3879
 3880
 3881
 3882
 3883
 3884
 3885
 3886
 3887
 3888
 3889
 3890
 3891
 3892
 3893
 3894
 3895
 3896
 3897
 3898
 3899
 3900
 3901
 3902
 3903
 3904
 3905
 3906
 3907
 3908
 3909
 3910
 3911
 3912
 3913
 3914
 3915
 3916
 3917
 3918
 3919
 3920
 3921
 3922
 3923
 3924
 3925
 3926
 3927
 3928
 3929
 3930
 3931
 3932
 3933
 3934
 3935
 3936
 3937
 3938
 3939
 3940
 3941
 3942
 3943
 3944
 3945
 3946
 3947
 3948
 3949
 3950
 3951
 3952
 3953
 3954
 3955
 3956
 3957
 3958
 3959
 3960
 3961
 3962
 3963
 3964
 3965
 3966
 3967
 3968
 3969
 3970
 3971
 3972
 3973
 3974
 3975
 3976
 3977
 3978
 3979
 3980
 3981
 3982
 3983
 3984
 3985
 3986
 3987
 3988
 3989
 3990
 3991
 3992
 3993
 3994
 3995
 3996
 3997
 3998
 3999
 4000
 4001
 4002
 4003
 4004
 4005
 4006
 4007
 4008
 4009
 4010
 4011
 4012
 4013
 4014
 4015
 4016
 4017
 4018
 4019
 4020
 4021
 4022
 4023
 4024
 4025
 4026
 4027
 4028
 4029
 4030
 4031
 4032
 4033
 4034
 4035
 4036
 4037
 4038
 4039
 4040
 4041
 4042
 4043
 4044
 4045
 4046
 4047
 4048
 4049
 4050
 4051
 4052
 4053
 4054
 4055
 4056
 4057
 4058
 4059
 4060
 4061
 4062
 4063
 4064
 4065
 4066
 4067
 4068
 4069
 4070
 4071
 4072
 4073
 4074
 4075
 4076
 4077
 4078
 4079
 4080
 4081
 4082
 4083
 4084
 4085
 4086
 4087
 4088
 4089
 4090
 4091
 4092
 4093
 4094
 4095
 4096
 4097
 4098
 4099
 4100
 4101
 4102
 4103
 4104
 4105
 4106
 4107
 4108
 4109
 4110
 4111
 4112
 4113
 4114
 4115
 4116
 4117
 4118
 4119
 4120
 4121
 4122
 4123
 4124
 4125
 4126
 4127
 4128
 4129
 4130
 4131
 4132
 4133
 4134
 4135
 4136
 4137
 4138
 4139
 4140
 4141
 4142
 4143
 4144
 4145
 4146
 4147
 4148
 4149
 4150
 4151
 4152
 4153
 4154
 4155
 4156
 4157
 4158
 4159
 4160
 4161
 4162
 4163
 4164
 4165
 4166
 4167
 4168
 4169
 4170
 4171
 4172
 4173
 4174
 4175
 4176
 4177
 4178
 4179
 4180
 4181
 4182
 4183
 4184
 4185
 4186
 4187
 4188
 4189
 4190
 4191
 4192
 4193
 4194
 4195
 4196
 4197
 4198
 4199
 4200
 4201
 4202
 4203
 4204
 4205
 4206
 4207
 4208
 4209
 4210
 4211
 4212
 4213
 4214
 4215
 4216
 4217
 4218
 4219
 4220
 4221
 4222
 4223
 4224
 4225
 4226
 4227
 4228
 4229
 4230
 4231
 4232
 4233
 4234
 4235
 4236
 4237
 4238
 4239
 4240
 4241
 4242
 4243
 4244
 4245
 4246
 4247
 4248
 4249
 4250
 4251
 4252
 4253
 4254
 4255
 4256
 4257
 4258
 4259
 4260
 4261
 4262
 4263
 4264
 4265
 4266
 4267
 4268
 4269
 4270
 4271
 4272
 4273
 4274
 4275
 4276
 4277
 4278
 4279
 4280
 4281
 4282
 4283
 4284
 4285
 4286
 4287
 4288
 4289
 4290
 4291
 4292
 4293
 4294
 4295
 4296
 4297
 4298
 4299
 4300
 4301
 4302
 4303
 4304
 4305
 4306
 4307
 4308
 4309
 4310
 4311
 4312
 4313
 4314
 4315
 4316
 4317
 4318
 4319
 4320
 4321
 4322
 4323
 4324
 4325
 4326
 4327
 4328
 4329
 4330
 4331
 4332
 4333
 4334
 4335
 4336
 4337
 4338
 4339
 4340
 4341
 4342
 4343
 4344
 4345
 4346
 4347
 4348
 4349
 4350
 4351
 4352
 4353
 4354
 4355
 4356
 4357
 4358
 4359
 4360
 4361
 4362
 4363
 4364
 4365
 4366
 4367
 4368
 4369
 4370
 4371
 4372
 4373
 4374
 4375
 4376
 4377
 4378
 4379
 4380
 4381
 4382
 4383
 4384
 4385
 4386
 4387
 4388
 4389
 4390
 4391
 4392
 4393
 4394
 4395
 4396
 4397
 4398
 4399
 4400
 4401
 4402
 4403
 4404
 4405
 4406
 4407
 4408
 4409
 4410
 4411
 4412
 4413
 4414
 4415
 4416
 4417
 4418
 4419
 4420
 4421
 4422
 4423
 4424
 4425
 4426
 4427
 4428
 4429
 4430
 4431
 4432
 4433
 4434
 4435
 4436
 4437
 4438
 4439
 4440
 4441
 4442
 4443
 4444
 4445
 4446
 4447
 4448
 4449
 4450
 4451
 4452
 4453
 4454
 4455
 4456
 4457
 4458
 4459
 4460
 4461
 4462
 4463
 4464
 4465
 4466
 4467
 4468
 4469
 4470
 4471
 4472
 4473
 4474
 4475
 4476
 4477
 4478
 4479
 4480
 4481
 4482
 4483
 4484
 4485
 4486
 4487
 4488
 4489
 4490
 4491
 4492
 4493
 4494
 4495
 4496
 4497
 4498
 4499
 4500
 4501
 4502
 4503
 4504
 4505
 4506
 4507
 4508
 4509
 4510
 4511
 4512
 4513
 4514
 4515
 4516
 4517
 4518
 4519
 4520
 4521
 4522
 4523
 4524
 4525
 4526
 4527
 4528
 4529
 4530
 4531
 4532
 4533
 4534
 4535
 4536
 4537
 4538
 4539
 4540
 4541
 4542
 4543
 4544
 4545
 4546
 4547
 4548
 4549
 4550
 4551
 4552
 4553
 4554
 4555
 4556
 4557
 4558
 4559
 4560
 4561
 4562
 4563
 4564
 4565
 4566
 4567
 4568
 4569
 4570
 4571
 4572
 4573
 4574
 4575
 4576
 4577
 4578
 4579
 4580
 4581
 4582
 4583
 4584
 4585
 4586
 4587
 4588
 4589
 4590
 4591
 4592
 4593
 4594
 4595
 4596
 4597
 4598
 4599
 4600
 4601
 4602
 4603
 4604
 4605
 4606
 4607
 4608
 4609
 4610
 4611
 4612
 4613
 4614
 4615
 4616
 4617
 4618
 4619
 4620
 4621
 4622
 4623
 4624
 4625
 4626
 4627
 4628
 4629
 4630
 4631
 4632
 4633
 4634
 4635
 4636
 4637
 4638
 4639
 4640
 4641
 4642
 4643
 4644
 4645
 4646
 4647
 4648
 4649
 4650
 4651
 4652
 4653
 4654
 4655
 4656
 4657
 4658
 4659
 4660
 4661
 4662
 4663
 4664
 4665
 4666
 4667
 4668
 4669
 4670
 4671
 4672
 4673
 4674
 4675
 4676
 4677
 4678
 4679
 4680
 4681
 4682
 4683
 4684
 4685
 4686
 4687
 4688
 4689
 4690
 4691
 4692
 4693
 4694
 4695
 4696
 4697
 4698
 4699
 4700
 4701
 4702
 4703
 4704
 4705
 4706
 4707
 4708
 4709
 4710
 4711
 4712
 4713
 4714
 4715
 4716
 4717
 4718
 4719
 4720
 4721
 4722
 4723
 4724
 4725
 4726
 4727
 4728
 4729
 4730
 4731
 4732
 4733
 4734
 4735
 4736
 4737
 4738
 4739
 4740
 4741
 4742
 4743
 4744
 4745
 4746
 4747
 4748
 4749
 4750
 4751
 4752
 4753
 4754
 4755
 4756
 4757
 4758
 4759
 4760
 4761
 4762
 4763
 4764
 4765
 4766
 4767
 4768
 4769
 4770
 4771
 4772
 4773
 4774
 4775
 4776
 4777
 4778
 4779
 4780
 4781
 4782
 4783
 4784
 4785
 4786
 4787
 4788
 4789
 4790
 4791
 4792
 4793
 4794
 4795
 4796
 4797
 4798
 4799
 4800
 4801
 4802
 4803
 4804
 4805
 4806
 4807
 4808
 4809
 4810
 4811
 4812
 4813
 4814
 4815
 4816
 4817
 4818
 4819
 4820
 4821
 4822
 4823
 4824
 4825
 4826
 4827
 4828
 4829
 4830
 4831
 4832
 4833
 4834
 4835
 4836
 4837
 4838
 4839
 4840
 4841
 4842
 4843
 4844
 4845
 4846
 4847
 4848
 4849
 4850
 4851
 4852
 4853
 4854
 4855
 4856
 4857
 4858
 4859
 4860
 4861
 4862
 4863
 4864
 4865
 4866
 4867
 4868
 4869
 4870
 4871
 4872
 4873
 4874
 4875
 4876
 4877
 4878
 4879
 4880
 4881
 4882
 4883
 4884
 4885
 4886
 4887
 4888
 4889
 4890
 4891
 4892
 4893
 4894
 4895
 4896
 4897
 4898
 4899
 4900
 4901
 4902
 4903
 4904
 4905
 4906
 4907
 4908
 4909
 4910
 4911
 4912
 4913
 4914
 4915
 4916
 4917
 4918
 4919
 4920
 4921
 4922
 4923
 4924
 4925
 4926
 4927
 4928
 4929
 4930
 4931
 4932
 4933
 4934
 4935
 4936
 4937
 4938
 4939
 4940
 4941
 4942
 4943
 4944
 4945
 4946
 4947
 4948
 4949
 4950
 4951
 4952
 4953
 4954
 4955
 4956
 4957
 4958
 4959
 4960
 4961
 4962
 4963
 4964
 4965
 4966
 4967
 4968
 4969
 4970
 4971
 4972
 4973
 4974
 4975
 4976
 4977
 4978
 4979
 4980
 4981
 4982
 4983
 4984
 4985
 4986
 4987
 4988
 4989
 4990
 4991
 4992
 4993
 4994
 4995
 4996
 4997
 4998
 4999
 5000
 5001
 5002
 5003
 5004
 5005
 5006
 5007
 5008
 5009
 5010
 5011
 5012
 5013
 5014
 5015
 5016
 5017
 5018
 5019
 5020
 5021
 5022
 5023
 5024
 5025
 5026
 5027
 5028
 5029
 5030
 5031
 5032
 5033
 5034
 5035
 5036
 5037
 5038
 5039
 5040
 5041
 5042
 5043
 5044
 5045
 5046
 5047
 5048
 5049
 5050
 5051
 5052
 5053
 5054
 5055
 5056
 5057
 5058
 5059
 5060
 5061
 5062
 5063
 5064
 5065
 5066
 5067
 5068
 5069
 5070
 5071
 5072
 5073
 5074
 5075
 5076
 5077
 5078
 5079
 5080
 5081
 5082
 5083
 5084
 5085
 5086
 5087
 5088
 5089
 5090
 5091
 5092
 5093
 5094
 5095
 5096
 5097
 5098
 5099
 5100
 5101
 5102
 5103
 5104
 5105
 5106
 5107
 5108
 5109
 5110
 5111
 5112
 5113
 5114
 5115
 5116
 5117
 5118
 5119
 5120
 5121
 5122
 5123
 5124
 5125
 5126
 5127
 5128
 5129
 5130
 5131
 5132
 5133
 5134
 5135
 5136
 5137
 5138
 5139
 5140
 5141
 5142
 5143
 5144
 5145
 5146
 5147
 5148
 5149
 5150
 5151
 5152
 5153
 5154
 5155
 5156
 5157
 5158
 5159
 5160
 5161
 5162
 5163
 5164
 5165
 5166
 5167
 5168
 5169
 5170
 5171
 5172
 5173
 5174
 5175
 5176
 5177
 5178
 5179
 5180
 5181
 5182
 5183
 5184
 5185
 5186
 5187
 5188
 5189
 5190
 5191
 5192
 5193
 5194
 5195
 5196
 5197
 5198
 5199
 5200
 5201
 5202
 5203
 5204
 5205
 5206
 5207
 5208
 5209
 5210
 5211
 5212
 5213
 5214
 5215
 5216
 5217
 5218
 5219
 5220
 5221
 5222
 5223
 5224
 5225
 5226
 5227
 5228
 5229
 5230
 5231
 5232
 5233
 5234
 5235
 5236
 5237
 5238
 5239
 5240
 5241
 5242
 5243
 5244
 5245
 5246
 5247
 5248
 5249
 5250
 5251
 5252
 5253
 5254
 5255
 5256
 5257
 5258
 5259
 5260
 5261
 5262
 5263
 5264
 5265
 5266
 5267
 5268
 5269
 5270
 5271
 5272
 5273
 5274
 5275
 5276
 5277
 5278
 5279
 5280
 5281
 5282
 5283
 5284
 5285
 5286
 5287
 5288
 5289
 5290
 5291
 5292
 5293
 5294
 5295
 5296
 5297
 5298
 5299
 5300
 5301
 5302
 5303
 5304
 5305
 5306
 5307
 5308
 5309
 5310
 5311
 5312
 5313
 5314
 5315
 5316
 5317
 5318
 5319
 5320
 5321
 5322
 5323
 5324
 5325
 5326
 5327
 5328
 5329
 5330
 5331
 5332
 5333
 5334
 5335
 5336
 5337
 5338
 5339
 5340
 5341
 5342
 5343
 5344
 5345
 5346
 5347
 5348
 5349
 5350
 5351
 5352
 5353
 5354
 5355
 5356
 5357
 5358
 5359
 5360
 5361
 5362
 5363
 5364
 5365
 5366
 5367
 5368
 5369
 5370
 5371
 5372
 5373
 5374
 5375
 5376
 5377
 5378
 5379
 5380
 5381
 5382
 5383
 5384
 5385
 5386
 5387
 5388
 5389
 5390
 5391
 5392
 5393
 5394
 5395
 5396
 5397
 5398
 5399
 5400
 5401
 5402
 5403
 5404
 5405
 5406
 5407
 5408
 5409
 5410
 5411
 5412
 5413
 5414
 5415
 5416
 5417
 5418
 5419
 5420
 5421
 5422
 5423
 5424
 5425
 5426
 5427
 5428
 5429
 5430
 5431
 5432
 5433
 5434
 5435
 5436
 5437
 5438
 5439
 5440
 5441
 5442
 5443
 5444
 5445
 5446
 5447
 5448
 5449
 5450
 5451
 5452
 5453
 5454
 5455
 5456
 5457
 5458
 5459
 5460
 5461
 5462
 5463
 5464
 5465
 5466
 5467
 5468
 5469
 5470
 5471
 5472
 5473
 5474
 5475
 5476
 5477
 5478
 5479
 5480
 5481
 5482
 5483
 5484
 5485
 5486
 5487
 5488
 5489
 5490
 5491
 5492
 5493
 5494
 5495
 5496
 5497
 5498
 5499
 5500
 5501
 5502
 5503
 5504
 5505
 5506
 5507
 5508
 5509
 5510
 5511
 5512
 5513
 5514
 5515
 5516
 5517
 5518
 5519
 5520
 5521
 5522
 5523
 5524
 5525
 5526
 5527
 5528
 5529
 5530
 5531
 5532
 5533
 5534
 5535
 5536
 5537
 5538
 5539
 5540
 5541
 5542
 5543
 5544
 5545
 5546
 5547
 5548
 5549
 5550
 5551
 5552
 5553
 5554
 5555
 5556
 5557
 5558
 5559
 5560
 5561
 5562
 5563
 5564
 5565
 5566
 5567
 5568
 5569
 5570
 5571
 5572
 5573
 5574
 5575
 5576
 5577
 5578
 5579
 5580
 5581
 5582
 5583
 5584
 5585
 5586
 5587
 5588
 5589
 5590
 5591
 5592
 5593
 5594
 5595
 5596
 5597
 5598
 5599
 5600
 5601
 5602
 5603
 5604
 5605
 5606
 5607
 5608
 5609
 5610
 5611
 5612
 5613
 5614
 5615
 5616
 5617
 5618
 5619
 5620
 5621
 5622
 5623
 5624
 5625
 5626
 5627
 5628
 5629
 5630
 5631
 5632
 5633
 5634
 5635
 5636
 5637
 5638
 5639
 5640
 5641
 5642
 5643
 5644
 5645
 5646
 5647
 5648
 5649
 5650
 5651
 5652
 5653
 5654
 5655
 5656
 5657
 5658
 5659
 5660
 5661
 5662
 5663
 5664
 5665
 5666
 5667
 5668
 5669
 5670
 5671
 5672
 5673
 5674
 5675
 5676
 5677
 5678
 5679
 5680
 5681
 5682
 5683
 5684
 5685
 5686
 5687
 5688
 5689
 5690
 5691
 5692
 5693
 5694
 5695
 5696
 5697
 5698
 5699
 5700
 5701
 5702
 5703
 5704
 5705
 5706
 5707
 5708
 5709
 5710
 5711
 5712
 5713
 5714
 5715
 5716
 5717
 5718
 5719
 5720
 5721
 5722
 5723
 5724
 5725
 5726
 5727
 5728
 5729
 5730
 5731
 5732
 5733
 5734
 5735
 5736
 5737
 5738
 5739
 5740
 5741
 5742
 5743
 5744
 5745
 5746
 5747
 5748
 5749
 5750
 5751
 5752
 5753
 5754
 5755
 5756
 5757
 5758
 5759
 5760
 5761
 5762
 5763
 5764
 5765
 5766
 5767
 5768
 5769
 5770
 5771
 5772
 5773
 5774
 5775
 5776
 5777
 5778
 5779
 5780
 5781
 5782
 5783
 5784
 5785
 5786
 5787
 5788
 5789
 5790
 5791
 5792
 5793
 5794
 5795
 5796
 5797
 5798
 5799
 5800
 5801
 5802
 5803
 5804
 5805
 5806
 5807
 5808
 5809
 5810
 5811
 5812
 5813
 5814
 5815
 5816
 5817
 5818
 5819
 5820
 5821
 5822
 5823
 5824
 5825
 5826
 5827
 5828
 5829
 5830
 5831
 5832
 5833
 5834
 5835
 5836
 5837
 5838
 5839
 5840
 5841
 5842
 5843
 5844
 5845
 5846
 5847
 5848
 5849
 5850
 5851
 5852
 5853
 5854
 5855
 5856
 5857
 5858
 5859
 5860
 5861
 5862
 5863
 5864
 5865
 5866
 5867
 5868
 5869
 5870
 5871
 5872
 5873
 5874
 5875
 5876
 5877
 5878
 5879
 5880
 5881
 5882
 5883
 5884
 5885
 5886
 5887
 5888
 5889
 5890
 5891
 5892
 5893
 5894
 5895
 5896
 5897
 5898
 5899
 5900
 5901
 5902
 5903
 5904
 5905
 5906
 5907
 5908
 5909
 5910
 5911
 5912
 5913
 5914
 5915
 5916
 5917
 5918
 5919
 5920
 5921
 5922
 5923
 5924
 5925
 5926
 5927
 5928
 5929
 5930
 5931
 5932
 5933
 5934
 5935
 5936
 5937
 5938
 5939
 5940
 5941
 5942
 5943
 5944
 5945
 5946
 5947
 5948
 5949
 5950
 5951
 5952
 5953
 5954
 5955
 5956
 5957
 5958
 5959
 5960
 5961
 5962
 5963
 5964
 5965
 5966
 5967
 5968
 5969
 5970
 5971
 5972
 5973
 5974
 5975
 5976
 5977
 5978
 5979
 5980
 5981
 5982
 5983
 5984
 5985
 5986
 5987
 5988
 5989
 5990
 5991
 5992
 5993
 5994
 5995
 5996
 5997
 5998
 5999
 6000
 6001
 6002
 6003
 6004
 6005
 6006
 6007
 6008
 6009
 6010
 6011
 6012
 6013
 6014
 6015
 6016
 6017
 6018
 6019
 6020
 6021
 6022
 6023
 6024
 6025
 6026
 6027
 6028
 6029
 6030
 6031
 6032
 6033
 6034
 6035
 6036
 6037
 6038
 6039
 6040
 6041
 6042
 6043
 6044
 6045
 6046
 6047
 6048
 6049
 6050
 6051
 6052
 6053
 6054
 6055
 6056
 6057
 6058
 6059
 6060
 6061
 6062
 6063
 6064
 6065
 6066
 6067
 6068
 6069
 6070
 6071
 6072
 6073
 6074
 6075
 6076
 6077
 6078
 6079
 6080
 6081
 6082
 6083
 6084
 6085
 6086
 6087
 6088
 6089
 6090
 6091
 6092
 6093
 6094
 6095
 6096
 6097
 6098
 6099
 6100
 6101
 6102
 6103
 6104
 6105
 6106
 6107
 6108
 6109
 6110
 6111
 6112
 6113
 6114
 6115
 6116
 6117
 6118
 6119
 6120
 6121
 6122
 6123
 6124
 6125
 6126
 6127
 6128
 6129
 6130
 6131
 6132
 6133
 6134
 6135
 6136
 6137
 6138
 6139
 6140
 6141
 6142
 6143
 6144
 6145
 6146
 6147
 6148
 6149
 6150
 6151
 6152
 6153
 6154
 6155
 6156
 6157
 6158
 6159
 6160
 6161
 6162
 6163
 6164
 6165
 6166
 6167
 6168
 6169
 6170
 6171
 6172
 6173
 6174
 6175
 6176
 6177
 6178
 6179
 6180
 6181
 6182
 6183
 6184
 6185
 6186
 6187
 6188
 6189
 6190
 6191
 6192
 6193
 6194
 6195
 6196
 6197
 6198
 6199
 6200
 6201
 6202
 6203
 6204
 6205
 6206
 6207
 6208
 6209
 6210
 6211
 6212
 6213
 6214
 6215
 6216
 6217
 6218
 6219
 6220
 6221
 6222
 6223
 6224
 6225
 6226
 6227
 6228
 6229
 6230
 6231
 6232
 6233
 6234
 6235
 6236
 6237
 6238
 6239
 6240
 6241
 6242
 6243
 6244
 6245
 6246
 6247
 6248
 6249
 6250
 6251
 6252
 6253
 6254
 6255
 6256
 6257
 6258
 6259
 6260
 6261
 6262
 6263
 6264
 6265
 6266
 6267
 6268
 6269
 6270
 6271
 6272
 6273
 6274
 6275
 6276
 6277
 6278
 6279
 6280
 6281
 6282
 6283
 6284
 6285
 6286
 6287
 6288
 6289
 6290
 6291
 6292
 6293
 6294
 6295
 6296
 6297
 6298
 6299
 6300
 6301
 6302
 6303
 6304
 6305
 6306
 6307
 6308
 6309
 6310
 6311
 6312
 6313
 6314
 6315
 6316
 6317
 6318
 6319
 6320
 6321
 6322
 6323
 6324
 6325
 6326
 6327
 6328
 6329
 6330
 6331
 6332
 6333
 6334
 6335
 6336
 6337
 6338
 6339
 6340
 6341
 6342
 6343
 6344
 6345
 6346
 6347
 6348
 6349
 6350
 6351
 6352
 6353
 6354
 6355
 6356
 6357
 6358
 6359
 6360
 6361
 6362
 6363
 6364
 6365
 6366
 6367
 6368
 6369
 6370
 6371
 6372
 6373
 6374
 6375
 6376
 6377
 6378
 6379
 6380
 6381
 6382
 6383
 6384
 6385
 6386
 6387
 6388
 6389
 6390
 6391
 6392
 6393
 6394
 6395
 6396
 6397
 6398
 6399
 6400
 6401
 6402
 6403
 6404
 6405
 6406
 6407
 6408
 6409
 6410
 6411
 6412
 6413
 6414
 6415
 6416
 6417
 6418
 6419
 6420
 6421
 6422
 6423
 6424
 6425
 6426
 6427
 6428
 6429
 6430
 6431
 6432
 6433
 6434
 6435
 6436
 6437
 6438
 6439
 6440
 6441
 6442
 6443
 6444
 6445
 6446
 6447
 6448
 6449
 6450
 6451
 6452
 6453
 6454
 6455
 6456
 6457
 6458
 6459
 6460
 6461
 6462
 6463
 6464
 6465
 6466
 6467
 6468
 6469
 6470
 6471
 6472
 6473
 6474
 6475
 6476
 6477
 6478
 6479
 6480
 6481
 6482
 6483
 6484
 6485
 6486
 6487
 6488
 6489
 6490
 6491
 6492
 6493
 6494
 6495
 6496
 6497
 6498
 6499
 6500
 6501
 6502
 6503
 6504
 6505
 6506
 6507
 6508
 6509
 6510
 6511
 6512
 6513
 6514
 6515
 6516
 6517
 6518
 6519
 6520
 6521
 6522
 6523
 6524
 6525
 6526
 6527
 6528
 6529
 6530
 6531
 6532
 6533
 6534
 6535
 6536
 6537
 6538
 6539
 6540
 6541
 6542
 6543
 6544
 6545
 6546
 6547
 6548
 6549
 6550
 6551
 6552
 6553
 6554
 6555
 6556
 6557
 6558
 6559
 6560
 6561
 6562
 6563
 6564
 6565
 6566
 6567
 6568
 6569
 6570
 6571
 6572
 6573
 6574
 6575
 6576
 6577
 6578
 6579
 6580
 6581
 6582
 6583
 6584
 6585
 6586
 6587
 6588
 6589
 6590
 6591
 6592
 6593
 6594
 6595
 6596
 6597
 6598
 6599
 6600
 6601
 6602
 6603
 6604
 6605
 6606
 6607
 6608
 6609
 6610
 6611
 6612
 6613
 6614
 6615
 6616
 6617
 6618
 6619
 6620
 6621
 6622
 6623
 6624
 6625
 6626
 6627
 6628
 6629
 6630
 6631
 6632
 6633
 6634
 6635
 6636
 6637
 6638
 6639
 6640
 6641
 6642
 6643
 6644
 6645
 6646
 6647
 6648
 6649
 6650
 6651
 6652
 6653
 6654
 6655
 6656
 6657
 6658
 6659
 6660
 6661
 6662
 6663
 6664
 6665
 6666
 6667
 6668
 6669
 6670
 6671
 6672
 6673
 6674
 6675
 6676
 6677
 6678
 6679
 6680
 6681
 6682
 6683
 6684
 6685
 6686
 6687
 6688
 6689
 6690
 6691
 6692
 6693
 6694
 6695
 6696
 6697
 6698
 6699
 6700
 6701
 6702
 6703
 6704
 6705
 6706
 6707
 6708
 6709
 6710
 6711
 6712
 6713
 6714
 6715
 6716
 6717
 6718
 6719
 6720
 6721
 6722
 6723
 6724
 6725
 6726
 6727
 6728
 6729
 6730
 6731
 6732
 6733
 6734
 6735
 6736
 6737
 6738
 6739
 6740
 6741
 6742
 6743
 6744
 6745
 6746
 6747
 6748
 6749
 6750
 6751
 6752
 6753
 6754
 6755
 6756
 6757
 6758
 6759
 6760
 6761
 6762
 6763
 6764
 6765
 6766
 6767
 6768
 6769
 6770
 6771
 6772
 6773
 6774
 6775
 6776
 6777
 6778
 6779
 6780
 6781
 6782
 6783
 6784
 6785
 6786
 6787
 6788
 6789
 6790
 6791
 6792
 6793
 6794
 6795
 6796
 6797
 6798
 6799
 6800
 6801
 6802
 6803
 6804
 6805
 6806
 6807
 6808
 6809
 6810
 6811
 6812
 6813
 6814
 6815
 6816
 6817
 6818
 6819
 6820
 6821
 6822
 6823
 6824
 6825
 6826
 6827
 6828
 6829
 6830
 6831
 6832
 6833
 6834
 6835
 6836
 6837
 6838
 6839
 6840
 6841
 6842
 6843
 6844
 6845
 6846
 6847
 6848
 6849
 6850
 6851
 6852
 6853
 6854
 6855
 6856
 6857
 6858
 6859
 6860
 6861
 6862
 6863
 6864
 6865
 6866
 6867
 6868
 6869
 6870
 6871
 6872
 6873
 6874
 6875
 6876
 6877
 6878
 6879
 6880
 6881
 6882
 6883
 6884
 6885
 6886
 6887
 6888
 6889
 6890
 6891
 6892
 6893
 6894
 6895
 6896
 6897
 6898
 6899
 6900
 6901
 6902
 6903
 6904
 6905
 6906
 6907
 6908
 6909
 6910
 6911
 6912
 6913
 6914
 6915
 6916
 6917
 6918
 6919
 6920
 6921
 6922
 6923
 6924
 6925
 6926
 6927
 6928
 6929
 6930
 6931
 6932
 6933
 6934
 6935
 6936
 6937
 6938
 6939
 6940
 6941
 6942
 6943
 6944
 6945
 6946
 6947
 6948
 6949
 6950
 6951
 6952
 6953
 6954
 6955
 6956
 6957
 6958
 6959
 6960
 6961
 6962
 6963
 6964
 6965
 6966
 6967
 6968
 6969
 6970
 6971
 6972
 6973
 6974
 6975
 6976
 6977
 6978
 6979
 6980
 6981
 6982
 6983
 6984
 6985
 6986
 6987
 6988
 6989
 6990
 6991
 6992
 6993
 6994
 6995
 6996
 6997
 6998
 6999
 7000
 7001
 7002
 7003
 7004
 7005
 7006
 7007
 7008
 7009
 7010
 7011
 7012
 7013
 7014
 7015
 7016
 7017
 7018
 7019
 7020
 7021
 7022
 7023
 7024
 7025
 7026
 7027
 7028
 7029
 7030
 7031
 7032
 7033
 7034
 7035
 7036
 7037
 7038
 7039
 7040
 7041
 7042
 7043
 7044
 7045
 7046
 7047
 7048
 7049
 7050
 7051
 7052
 7053
 7054
 7055
 7056
 7057
 7058
 7059
 7060
 7061
 7062
 7063
 7064
 7065
 7066
 7067
 7068
 7069
 7070
 7071
 7072
 7073
 7074
 7075
 7076
 7077
 7078
 7079
 7080
 7081
 7082
 7083
 7084
 7085
 7086
 7087
 7088
 7089
 7090
 7091
 7092
 7093
 7094
 7095
 7096
 7097
 7098
 7099
 7100
 7101
 7102
 7103
 7104
 7105
 7106
 7107
 7108
 7109
 7110
 7111
 7112
 7113
 7114
 7115
 7116
 7117
 7118
 7119
 7120
 7121
 7122
 7123
 7124
 7125
 7126
 7127
 7128
 7129
 7130
 7131
 7132
 7133
 7134
 7135
 7136
 7137
 7138
 7139
 7140
 7141
 7142
 7143
 7144
 7145
 7146
 7147
 7148
 7149
 7150
 7151
 7152
 7153
 7154
 7155
 7156
 7157
 7158
 7159
 7160
 7161
 7162
 7163
 7164
 7165
 7166
 7167
 7168
 7169
 7170
 7171
 7172
 7173
 7174
 7175
 7176
 7177
 7178
 7179
 7180
 7181
 7182
 7183
 7184
 7185
 7186
 7187
 7188
 7189
 7190
 7191
 7192
 7193
 7194
 7195
 7196
 7197
 7198
 7199
 7200
 7201
 7202
 7203
 7204
 7205
 7206
 7207
 7208
 7209
 7210
 7211
 7212
 7213
 7214
 7215
 7216
 7217
 7218
 7219
 7220
 7221
 7222
 7223
 7224
 7225
 7226
 7227
 7228
 7229
 7230
 7231
 7232
 7233
 7234
 7235
 7236
 7237
 7238
 7239
 7240
 7241
 7242
 7243
 7244
 7245
 7246
 7247
 7248
 7249
 7250
 7251
 7252
 7253
 7254
 7255
 7256
 7257
 7258
 7259
 7260
 7261
 7262
 7263
 7264
 7265
 7266
 7267
 7268
 7269
 7270
 7271
 7272
 7273
 7274
 7275
 7276
 7277
 7278
 7279
 7280
 7281
 7282
 7283
 7284
 7285
 7286
 7287
 7288
 7289
 7290
 7291
 7292
 7293
 7294
 7295
 7296
 7297
 7298
 7299
 7300
 7301
 7302
 7303
 7304
 7305
 7306
 7307
 7308
 7309
 7310
 7311
 7312
 7313
 7314
 7315
 7316
 7317
 7318
 7319
 7320
 7321
 7322
 7323
 7324
 7325
 7326
 7327
 7328
 7329
 7330
 7331
 7332
 7333
 7334
 7335
 7336
 7337
 7338
 7339
 7340
 7341
 7342
 7343
 7344
 7345
 7346
 7347
 7348
 7349
 7350
 7351
 7352
 7353
 7354
 7355
 7356
 7357
 7358
 7359
 7360
 7361
 7362
 7363
 7364
 7365
 7366
 7367
 7368
 7369
 7370
 7371
 7372
 7373
 7374
 7375
 7376
 7377
 7378
 7379
 7380
 7381
 7382
 7383
 7384
 7385
 7386
 7387
 7388
 7389
 7390
 7391
 7392
 7393
 7394
 7395
 7396
 7397
 7398
 7399
 7400
 7401
 7402
 7403
 7404
 7405
 7406
 7407
 7408
 7409
 7410
 7411
 7412
 7413
 7414
 7415
 7416
 7417
 7418
 7419
 7420
 7421
 7422
 7423
 7424
 7425
 7426
 7427
 7428
 7429
 7430
 7431
 7432
 7433
 7434
 7435
 7436
 7437
 7438
 7439
 7440
 7441
 7442
 7443
 7444
 7445
 7446
 7447
 7448
 7449
 7450
 7451
 7452
 7453
 7454
 7455
 7456
 7457
 7458
 7459
 7460
 7461
 7462
 7463
 7464
 7465
 7466
 7467
 7468
 7469
 7470
 7471
 7472
 7473
 7474
 7475
 7476
 7477
 7478
 7479
 7480
 7481
 7482
 7483
 7484
 7485
 7486
 7487
 7488
 7489
 7490
 7491
 7492
 7493
 7494
 7495
 7496
 7497
 7498
 7499
 7500
 7501
 7502
 7503
 7504
 7505
 7506
 7507
 7508
 7509
 7510
 7511
 7512
 7513
 7514
 7515
 7516
 7517
 7518
 7519
 7520
 7521
 7522
 7523
 7524
 7525
 7526
 7527
 7528
 7529
 7530
 7531
 7532
 7533
 7534
 7535
 7536
 7537
 7538
 7539
 7540
 7541
 7542
 7543
 7544
 7545
 7546
 7547
 7548
 7549
 7550
 7551
 7552
 7553
 7554
 7555
 7556
 7557
 7558
 7559
 7560
 7561
 7562
 7563
 7564
 7565
 7566
 7567
 7568
 7569
 7570
 7571
 7572
 7573
 7574
 7575
 7576
 7577
 7578
 7579
 7580
 7581
 7582
 7583
 7584
 7585
 7586
 7587
 7588
 7589
 7590
 7591
 7592
 7593
 7594
 7595
 7596
 7597
 7598
 7599
 7600
 7601
 7602
 7603
 7604
 7605
 7606
 7607
 7608
 7609
 7610
 7611
 7612
 7613
 7614
 7615
 7616
 7617
 7618
 7619
 7620
 7621
 7622
 7623
 7624
 7625
 7626
 7627
 7628
 7629
 7630
 7631
 7632
 7633
 7634
 7635
 7636
 7637
 7638
 7639
 7640
 7641
 7642
 7643
 7644
 7645
 7646
 7647
 7648
 7649
 7650
 7651
 7652
 7653
 7654
 7655
 7656
 7657
 7658
 7659
 7660
 7661
 7662
 7663
 7664
 7665
 7666
 7667
 7668
 7669
 7670
 7671
 7672
 7673
 7674
 7675
 7676
 7677
 7678
 7679
 7680
 7681
 7682
 7683
 7684
 7685
 7686
 7687
 7688
 7689
 7690
 7691
 7692
 7693
 7694
 7695
 7696
 7697
 7698
 7699
 7700
 7701
 7702
 7703
 7704
 7705
 7706
 7707
 7708
 7709
 7710
 7711
 7712
 7713
 7714
 7715
 7716
 7717
 7718
 7719
 7720
 7721
 7722
 7723
 7724
 7725
 7726
 7727
 7728
 7729
 7730
 7731
 7732
 7733
 7734
 7735
 7736
 7737
 7738
 7739
 7740
 7741
 7742
 7743
 7744
 7745
 7746
 7747
 7748
 7749
 7750
 7751
 7752
 7753
 7754
 7755
 7756
 7757
 7758
 7759
 7760
 7761
 7762
 7763
 7764
 7765
 7766
 7767
 7768
 7769
 7770
 7771
 7772
 7773
 7774
 7775
 7776
 7777
 7778
 7779
 7780
 7781
 7782
 7783
 7784
 7785
 7786
 7787
 7788
 7789
 7790
 7791
 7792
 7793
 7794
 7795
 7796
 7797
 7798
 7799
 7800
 7801
 7802
 7803
 7804
 7805
 7806
 7807
 7808
 7809
 7810
 7811
 7812
 7813
 7814
 7815
 7816
 7817
 7818
 7819
 7820
 7821
 7822
 7823
 7824
 7825
 7826
 7827
 7828
 7829
 7830
 7831
 7832
 7833
 7834
 7835
 7836
 7837
 7838
 7839
 7840
 7841
 7842
 7843
 7844
 7845
 7846
 7847
 7848
 7849
 7850
 7851
 7852
 7853
 7854
 7855
 7856
 7857
 7858
 7859
 7860
 7861
 7862
 7863
 7864
 7865
 7866
 7867
 7868
 7869
 7870
 7871
 7872
 7873
 7874
 7875
 7876
 7877
 7878
 7879
 7880
 7881
 7882
 7883
 7884
 7885
 7886
 7887
 7888
 7889
 7890
 7891
 7892
 7893
 7894
 7895
 7896
 7897
 7898
 7899
 7900
 7901
 7902
 7903
 7904
 7905
 7906
 7907
 7908
 7909
 7910
 7911
 7912
 7913
 7914
 7915
 7916
 7917
 7918
 7919
 7920
 7921
 7922
 7923
 7924
 7925
 7926
 7927
 7928
 7929
 7930
 7931
 7932
 7933
 7934
 7935
 7936
 7937
 7938
 7939
 7940
 7941
 7942
 7943
 7944
 7945
 7946
 7947
 7948
 7949
 7950
 7951
 7952
 7953
 7954
 7955
 7956
 7957
 7958
 7959
 7960
 7961
 7962
 7963
 7964
 7965
 7966
 7967
 7968
 7969
 7970
 7971
 7972
 7973
 7974
 7975
 7976
 7977
 7978
 7979
 7980
 7981
 7982
 7983
 7984
 7985
 7986
 7987
 7988
 7989
 7990
 7991
 7992
 7993
 7994
 7995
 7996
 7997
 7998
 7999
 8000
 8001
 8002
 8003
 8004
 8005
 8006
 8007
 8008
 8009
 8010
 8011
 8012
 8013
 8014
 8015
 8016
 8017
 8018
 8019
 8020
 8021
 8022
 8023
 8024
 8025
 8026
 8027
 8028
 8029
 8030
 8031
 8032
 8033
 8034
 8035
 8036
 8037
 8038
 8039
 8040
 8041
 8042
 8043
 8044
 8045
 8046
 8047
 8048
 8049
 8050
 8051
 8052
 8053
 8054
 8055
 8056
 8057
 8058
 8059
 8060
 8061
 8062
 8063
 8064
 8065
 8066
 8067
 8068
 8069
 8070
 8071
 8072
 8073
 8074
 8075
 8076
 8077
 8078
 8079
 8080
 8081
 8082
 8083
 8084
 8085
 8086
 8087
 8088
 8089
 8090
 8091
 8092
 8093
 8094
 8095
 8096
 8097
 8098
 8099
 8100
 8101
 8102
 8103
 8104
 8105
 8106
 8107
 8108
 8109
 8110
 8111
 8112
 8113
 8114
 8115
 8116
 8117
 8118
 8119
 8120
 8121
 8122
 8123
 8124
 8125
 8126
 8127
 8128
 8129
 8130
 8131
 8132
 8133
 8134
 8135
 8136
 8137
 8138
 8139
 8140
 8141
 8142
 8143
 8144
 8145
 8146
 8147
 8148
 8149
 8150
 8151
 8152
 8153
 8154
 8155
 8156
 8157
 8158
 8159
 8160
 8161
 8162
 8163
 8164
 8165
 8166
 8167
 8168
 8169
 8170
 8171
 8172
 8173
 8174
 8175
 8176
 8177
 8178
 8179
 8180
 8181
 8182
 8183
 8184
 8185
 8186
 8187
 8188
 8189
 8190
 8191
 8192
 8193
 8194
 8195
 8196
 8197
 8198
 8199
 8200
 8201
 8202
 8203
 8204
 8205
 8206
 8207
 8208
 8209
 8210
 8211
 8212
 8213
 8214
 8215
 8216
 8217
 8218
 8219
 8220
 8221
 8222
 8223
 8224
 8225
 8226
 8227
 8228
 8229
 8230
 8231
 8232
 8233
 8234
 8235
 8236
 8237
 8238
 8239
 8240
 8241
 8242
 8243
 8244
 8245
 8246
 8247
 8248
 8249
 8250
 8251
 8252
 8253
 8254
 8255
 8256
 8257
 8258
 8259
 8260
 8261
 8262
 8263
 8264
 8265
 8266
 8267
 8268
 8269
 8270
 8271
 8272
 8273
 8274
 8275
 8276
 8277
 8278
 8279
 8280
 8281
 8282
 8283
 8284
 8285
 8286
 8287
 8288
 8289
 8290
 8291
 8292
 8293
 8294
 8295
 8296
 8297
 8298
 8299
 8300
 8301
 8302
 8303
 8304
 8305
 8306
 8307
 8308
 8309
 8310
 8311
 8312
 8313
 8314
 8315
 8316
 8317
 8318
 8319
 8320
 8321
 8322
 8323
 8324
 8325
 8326
 8327
 8328
 8329
 8330
 8331
 8332
 8333
 8334
 8335
 8336
 8337
 8338
 8339
 8340
 8341
 8342
 8343
 8344
 8345
 8346
 8347
 8348
 8349
 8350
 8351
 8352
 8353
 8354
 8355
 8356
 8357
 8358
 8359
 8360
 8361
 8362
 8363
 8364
 8365
 8366
 8367
 8368
 8369
 8370
 8371
 8372
 8373
 8374
 8375
 8376
 8377
 8378
 8379
 8380
 8381
 8382
 8383
 8384
 8385
 8386
 8387
 8388
 8389
 8390
 8391
 8392
 8393
 8394
 8395
 8396
 8397
 8398
 8399
 8400
 8401
 8402
 8403
 8404
 8405
 8406
 8407
 8408
 8409
 8410
 8411
 8412
 8413
 8414
 8415
 8416
 8417
 8418
 8419
 8420
 8421
 8422
 8423
 8424
 8425
 8426
 8427
 8428
 8429
 8430
 8431
 8432
 8433
 8434
 8435
 8436
 8437
 8438
 8439
 8440
 8441
 8442
 8443
 8444
 8445
 8446
 8447
 8448
 8449
 8450
 8451
 8452
 8453
 8454
 8455
 8456
 8457
 8458
 8459
 8460
 8461
 8462
 8463
 8464
 8465
 8466
 8467
 8468
 8469
 8470
 8471
 8472
 8473
 8474
 8475
 8476
 8477
 8478
 8479
 8480
 8481
 8482
 8483
 8484
 8485
 8486
 8487
 8488
 8489
 8490
 8491
 8492
 8493
 8494
 8495
 8496
 8497
 8498
 8499
 8500
 8501
 8502
 8503
 8504
 8505
 8506
 8507
 8508
 8509
 8510
 8511
 8512
 8513
 8514
 8515
 8516
 8517
 8518
 8519
 8520
 8521
 8522
 8523
 8524
 8525
 8526
 8527
 8528
 8529
 8530
 8531
 8532
 8533
 8534
 8535
 8536
 8537
 8538
 8539
 8540
 8541
 8542
 8543
 8544
 8545
 8546
 8547
 8548
 8549
 8550
 8551
 8552
 8553
 8554
 8555
 8556
 8557
 8558
 8559
 8560
 8561
 8562
 8563
 8564
 8565
 8566
 8567
 8568
 8569
 8570
 8571
 8572
 8573
 8574
 8575
 8576
 8577
 8578
 8579
 8580
 8581
 8582
 8583
 8584
 8585
 8586
 8587
 8588
 8589
 8590
 8591
 8592
 8593
 8594
 8595
 8596
 8597
 8598
 8599
 8600
 8601
 8602
 8603
 8604
 8605
 8606
 8607
 8608
 8609
 8610
 8611
 8612
 8613
 8614
 8615
 8616
 8617
 8618
 8619
 8620
 8621
 8622
 8623
 8624
 8625
 8626
 8627
 8628
 8629
 8630
 8631
 8632
 8633
 8634
 8635
 8636
 8637
 8638
 8639
 8640
 8641
 8642
 8643
 8644
 8645
 8646
 8647
 8648
 8649
 8650
 8651
 8652
 8653
 8654
 8655
 8656
 8657
 8658
 8659
 8660
 8661
 8662
 8663
 8664
 8665
 8666
 8667
 8668
 8669
 8670
 8671
 8672
 8673
 8674
 8675
 8676
 8677
 8678
 8679
 8680
 8681
 8682
 8683
 8684
 8685
 8686
 8687
 8688
 8689
 8690
 8691
 8692
 8693
 8694
 8695
 8696
 8697
 8698
 8699
 8700
 8701
 8702
 8703
 8704
 8705
 8706
 8707
 8708
 8709
 8710
 8711
 8712
 8713
 8714
 8715
 8716
 8717
 8718
 8719
 8720
 8721
 8722
 8723
 8724
 8725
 8726
 8727
 8728
 8729
 8730
 8731
 8732
 8733
 8734
 8735
 8736
 8737
 8738
 8739
 8740
 8741
 8742
 8743
 8744
 8745
 8746
 8747
 8748
 8749
 8750
 8751
 8752
 8753
 8754
 8755
 8756
 8757
 8758
 8759
 8760
 8761
 8762
 8763
 8764
 8765
 8766
 8767
 8768
 8769
 8770
 8771
 8772
 8773
 8774
 8775
 8776
 8777
 8778
 8779
 8780
 8781
 8782
 8783
 8784
 8785
 8786
 8787
 8788
 8789
 8790
 8791
 8792
 8793
 8794
 8795
 8796
 8797
 8798
 8799
 8800
 8801
 8802
 8803
 8804
 8805
 8806
 8807
 8808
 8809
 8810
 8811
 8812
 8813
 8814
 8815
 8816
 8817
 8818
 8819
 8820
 8821
 8822
 8823
 8824
 8825
 8826
 8827
 8828
 8829
 8830
 8831
 8832
 8833
 8834
 8835
 8836
 8837
 8838
 8839
 8840
 8841
 8842
 8843
 8844
 8845
 8846
 8847
 8848
 8849
 8850
 8851
 8852
 8853
 8854
 8855
 8856
 8857
 8858
 8859
 8860
 8861
 8862
 8863
 8864
 8865
 8866
 8867
 8868
 8869
 8870
 8871
 8872
 8873
 8874
 8875
 8876
 8877
 8878
 8879
 8880
 8881
 8882
 8883
 8884
 8885
 8886
 8887
 8888
 8889
 8890
 8891
 8892
 8893
 8894
 8895
 8896
 8897
 8898
 8899
 8900
 8901
 8902
 8903
 8904
 8905
 8906
 8907
 8908
 8909
 8910
 8911
 8912
 8913
 8914
 8915
 8916
 8917
 8918
 8919
 8920
 8921
 8922
 8923
 8924
 8925
 8926
 8927
 8928
 8929
 8930
 8931
 8932
 8933
 8934
 8935
 8936
 8937
 8938
 8939
 8940
 8941
 8942
 8943
 8944
 8945
 8946
 8947
 8948
 8949
 8950
 8951
 8952
 8953
 8954
 8955
 8956
 8957
 8958
 8959
 8960
 8961
 8962
 8963
 8964
 8965
 8966
 8967
 8968
 8969
 8970
 8971
 8972
 8973
 8974
 8975
 8976
 8977
 8978
 8979
 8980
 8981
 8982
 8983
 8984
 8985
 8986
 8987
 8988
 8989
 8990
 8991
 8992
 8993
 8994
 8995
 8996
 8997
 8998
 8999
 9000
 9001
 9002
 9003
 9004
 9005
 9006
 9007
 9008
 9009
 9010
 9011
 9012
 9013
 9014
 9015
 9016
 9017
 9018
 9019
 9020
 9021
 9022
 9023
 9024
 9025
 9026
 9027
 9028
 9029
 9030
 9031
 9032
 9033
 9034
 9035
 9036
 9037
 9038
 9039
 9040
 9041
 9042
 9043
 9044
 9045
 9046
 9047
 9048
 9049
 9050
 9051
 9052
 9053
 9054
 9055
 9056
 9057
 9058
 9059
 9060
 9061
 9062
 9063
 9064
 9065
 9066
 9067
 9068
 9069
 9070
 9071
 9072
 9073
 9074
 9075
 9076
 9077
 9078
 9079
 9080
 9081
 9082
 9083
 9084
 9085
 9086
 9087
 9088
 9089
 9090
 9091
 9092
 9093
 9094
 9095
 9096
 9097
 9098
 9099
 9100
 9101
 9102
 9103
 9104
 9105
 9106
 9107
 9108
 9109
 9110
 9111
 9112
 9113
 9114
 9115
 9116
 9117
 9118
 9119
 9120
 9121
 9122
 9123
 9124
 9125
 9126
 9127
 9128
 9129
 9130
 9131
 9132
 9133
 9134
 9135
 9136
 9137
 9138
 9139
 9140
 9141
 9142
 9143
 9144
 9145
 9146
 9147
 9148
 9149
 9150
 9151
 9152
 9153
 9154
 9155
 9156
 9157
 9158
 9159
 9160
 9161
 9162
 9163
 9164
 9165
 9166
 9167
 9168
 9169
 9170
 9171
 9172
 9173
 9174
 9175
 9176
 9177
 9178
 9179
 9180
 9181
 9182
 9183
 9184
 9185
 9186
 9187
 9188
 9189
 9190
 9191
 9192
 9193
 9194
 9195
 9196
 9197
 9198
 9199
 9200
 9201
 9202
 9203
 9204
 9205
 9206
 9207
 9208
 9209
 9210
 9211
 9212
 9213
 9214
 9215
 9216
 9217
 9218
 9219
 9220
 9221
 9222
 9223
 9224
 9225
 9226
 9227
 9228
 9229
 9230
 9231
 9232
 9233
 9234
 9235
 9236
 9237
 9238
 9239
 9240
 9241
 9242
 9243
 9244
 9245
 9246
 9247
 9248
 9249
 9250
 9251
 9252
 9253
 9254
 9255
 9256
 9257
 9258
 9259
 9260
 9261
 9262
 9263
 9264
 9265
 9266
 9267
 9268
 9269
 9270
 9271
 9272
 9273
 9274
 9275
 9276
 9277
 9278
 9279
 9280
 9281
 9282
 9283
 9284
 9285
 9286
 9287
 9288
 9289
 9290
 9291
 9292
 9293
 9294
 9295
 9296
 9297
 9298
 9299
 9300
 9301
 9302
 9303
 9304
 9305
 9306
 9307
 9308
 9309
 9310
 9311
 9312
 9313
 9314
 9315
 9316
 9317
 9318
 9319
 9320
 9321
 9322
 9323
 9324
 9325
 9326
 9327
 9328
 9329
 9330
 9331
 9332
 9333
 9334
 9335
 9336
 9337
 9338
 9339
 9340
 9341
 9342
 9343
 9344
 9345
 9346
 9347
 9348
 9349
 9350
 9351
 9352
 9353
 9354
 9355
 9356
 9357
 9358
 9359
 9360
 9361
 9362
 9363
 9364
 9365
 9366
 9367
 9368
 9369
 9370
 9371
 9372
 9373
 9374
 9375
 9376
 9377
 9378
 9379
 9380
 9381
 9382
 9383
 9384
 9385
 9386
 9387
 9388
 9389
 9390
 9391
 9392
 9393
 9394
 9395
 9396
 9397
 9398
 9399
 9400
 9401
 9402
 9403
 9404
 9405
 9406
 9407
 9408
 9409
 9410
 9411
 9412
 9413
 9414
 9415
 9416
 9417
 9418
 9419
 9420
 9421
 9422
 9423
 9424
 9425
 9426
 9427
 9428
 9429
 9430
 9431
 9432
 9433
 9434
 9435
 9436
 9437
 9438
 9439
 9440
 9441
 9442
 9443
 9444
 9445
 9446
 9447
 9448
 9449
 9450
 9451
 9452
 9453
 9454
 9455
 9456
 9457
 9458
 9459
 9460
 9461
 9462
 9463
 9464
 9465
 9466
 9467
 9468
 9469
 9470
 9471
 9472
 9473
 9474
 9475
 9476
 9477
 9478
 9479
 9480
 9481
 9482
 9483
 9484
 9485
 9486
 9487
 9488
 9489
 9490
 9491
 9492
 9493
 9494
 9495
 9496
 9497
 9498
 9499
 9500
 9501
 9502
 9503
 9504
 9505
 9506
 9507
 9508
 9509
 9510
 9511
 9512
 9513
 9514
 9515
 9516
 9517
 9518
 9519
 9520
 9521
 9522
 9523
 9524
 9525
 9526
 9527
 9528
 9529
 9530
 9531
 9532
 9533
 9534
 9535
 9536
 9537
 9538
 9539
 9540
 9541
 9542
 9543
 9544
 9545
 9546
 9547
 9548
 9549
 9550
 9551
 9552
 9553
 9554
 9555
 9556
 9557
 9558
 9559
 9560
 9561
 9562
 9563
 9564
 9565
 9566
 9567
 9568
 9569
 9570
 9571
 9572
 9573
 9574
 9575
 9576
 9577
 9578
 9579
 9580
 9581
 9582
 9583
 9584
 9585
 9586
 9587
 9588
 9589
 9590
 9591
 9592
 9593
 9594
 9595
 9596
 9597
 9598
 9599
 9600
 9601
 9602
 9603
 9604
 9605
 9606
 9607
 9608
 9609
 9610
 9611
 9612
 9613
 9614
 9615
 9616
 9617
 9618
 9619
 9620
 9621
 9622
 9623
 9624
 9625
 9626
 9627
 9628
 9629
 9630
 9631
 9632
 9633
 9634
 9635
 9636
 9637
 9638
 9639
 9640
 9641
 9642
 9643
 9644
 9645
 9646
 9647
 9648
 9649
 9650
 9651
 9652
 9653
 9654
 9655
 9656
 9657
 9658
 9659
 9660
 9661
 9662
 9663
 9664
 9665
 9666
 9667
 9668
 9669
 9670
 9671
 9672
 9673
 9674
 9675
 9676
 9677
 9678
 9679
 9680
 9681
 9682
 9683
 9684
 9685
 9686
 9687
 9688
 9689
 9690
 9691
 9692
 9693
 9694
 9695
 9696
 9697
 9698
 9699
 9700
 9701
 9702
 9703
 9704
 9705
 9706
 9707
 9708
 9709
 9710
 9711
 9712
 9713
 9714
 9715
 9716
 9717
 9718
 9719
 9720
 9721
 9722
 9723
 9724
 9725
 9726
 9727
 9728
 9729
 9730
 9731
 9732
 9733
 9734
 9735
 9736
 9737
 9738
 9739
 9740
 9741
 9742
 9743
 9744
 9745
 9746
 9747
 9748
 9749
 9750
 9751
 9752
 9753
 9754
 9755
 9756
 9757
 9758
 9759
 9760
 9761
 9762
 9763
 9764
 9765
 9766
 9767
 9768
 9769
 9770
 9771
 9772
 9773
 9774
 9775
 9776
 9777
 9778
 9779
 9780
 9781
 9782
 9783
 9784
 9785
 9786
 9787
 9788
 9789
 9790
 9791
 9792
 9793
 9794
 9795
 9796
 9797
 9798
 9799
 9800
 9801
 9802
 9803
 9804
 9805
 9806
 9807
 9808
 9809
 9810
 9811
 9812
 9813
 9814
 9815
 9816
 9817
 9818
 9819
 9820
 9821
 9822
 9823
 9824
 9825
 9826
 9827
 9828
 9829
 9830
 9831
 9832
 9833
 9834
 9835
 9836
 9837
 9838
 9839
 9840
 9841
 9842
 9843
 9844
 9845
 9846
 9847
 9848
 9849
 9850
 9851
 9852
 9853
 9854
 9855
 9856
 9857
 9858
 9859
 9860
 9861
 9862
 9863
 9864
 9865
 9866
 9867
 9868
 9869
 9870
 9871
 9872
 9873
 9874
 9875
 9876
 9877
 9878
 9879
 9880
 9881
 9882
 9883
 9884
 9885
 9886
 9887
 9888
 9889
 9890
 9891
 9892
 9893
 9894
 9895
 9896
 9897
 9898
 9899
 9900
 9901
 9902
 9903
 9904
 9905
 9906
 9907
 9908
 9909
 9910
 9911
 9912
 9913
 9914
 9915
 9916
 9917
 9918
 9919
 9920
 9921
 9922
 9923
 9924
 9925
 9926
 9927
 9928
 9929
 9930
 9931
 9932
 9933
 9934
 9935
 9936
 9937
 9938
 9939
 9940
 9941
 9942
 9943
 9944
 9945
 9946
 9947
 9948
 9949
 9950
 9951
 9952
 9953
 9954
 9955
 9956
 9957
 9958
 9959
 9960
 9961
 9962
 9963
 9964
 9965
 9966
 9967
 9968
 9969
 9970
 9971
 9972
 9973
 9974
 9975
 9976
 9977
 9978
 9979
 9980
 9981
 9982
 9983
 9984
 9985
 9986
 9987
 9988
 9989
 9990
 9991
 9992
 9993
 9994
 9995
 9996
 9997
 9998
 9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
22413
22414
22415
22416
22417
22418
22419
22420
22421
22422
22423
22424
22425
22426
22427
22428
22429
22430
22431
22432
22433
22434
22435
22436
22437
22438
22439
22440
22441
22442
22443
22444
22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
22461
22462
22463
22464
22465
22466
22467
22468
22469
22470
22471
22472
22473
22474
22475
22476
22477
22478
22479
22480
22481
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
22671
22672
22673
22674
22675
22676
22677
22678
22679
22680
22681
22682
22683
22684
22685
22686
22687
22688
22689
22690
22691
22692
22693
22694
22695
22696
22697
22698
22699
22700
22701
22702
22703
22704
22705
22706
22707
22708
22709
22710
22711
22712
22713
22714
22715
22716
22717
22718
22719
22720
22721
22722
22723
22724
22725
22726
22727
22728
22729
22730
22731
22732
22733
22734
22735
22736
22737
22738
22739
22740
22741
22742
22743
22744
22745
22746
22747
22748
22749
22750
22751
22752
22753
22754
22755
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769
22770
22771
22772
22773
22774
22775
22776
22777
22778
22779
22780
22781
22782
22783
22784
22785
22786
22787
22788
22789
22790
22791
22792
22793
22794
22795
22796
22797
22798
22799
22800
22801
22802
22803
22804
22805
22806
22807
22808
22809
22810
22811
22812
22813
22814
22815
22816
22817
22818
22819
22820
22821
22822
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834
22835
22836
22837
22838
22839
22840
22841
22842
22843
22844
22845
22846
22847
22848
22849
22850
22851
22852
22853
22854
22855
22856
22857
22858
22859
22860
22861
22862
22863
22864
22865
22866
22867
22868
22869
22870
22871
22872
22873
22874
22875
22876
22877
22878
22879
22880
22881
22882
22883
22884
22885
22886
22887
22888
22889
22890
22891
22892
22893
22894
22895
22896
22897
22898
22899
22900
22901
22902
22903
22904
22905
22906
22907
22908
22909
22910
22911
22912
22913
22914
22915
22916
22917
22918
22919
22920
22921
22922
22923
22924
22925
22926
22927
22928
22929
22930
22931
22932
22933
22934
22935
22936
22937
22938
22939
22940
22941
22942
22943
22944
22945
22946
22947
22948
22949
22950
22951
22952
22953
22954
22955
22956
22957
22958
22959
22960
22961
22962
22963
22964
22965
22966
22967
22968
22969
22970
22971
22972
22973
22974
22975
22976
22977
22978
22979
22980
22981
22982
22983
22984
22985
22986
22987
22988
22989
22990
22991
22992
22993
22994
22995
22996
22997
22998
22999
23000
23001
23002
23003
23004
23005
23006
23007
23008
23009
23010
23011
23012
23013
23014
23015
23016
23017
23018
23019
23020
23021
23022
23023
23024
23025
23026
23027
23028
23029
23030
23031
23032
23033
23034
23035
23036
23037
23038
23039
23040
23041
23042
23043
23044
23045
23046
23047
23048
23049
23050
23051
23052
23053
23054
23055
23056
23057
23058
23059
23060
23061
23062
23063
23064
23065
23066
23067
23068
23069
23070
23071
23072
23073
23074
23075
23076
23077
23078
23079
23080
23081
23082
23083
23084
23085
23086
23087
23088
23089
23090
23091
23092
23093
23094
23095
23096
23097
23098
23099
23100
23101
23102
23103
23104
23105
23106
23107
23108
23109
23110
23111
23112
23113
23114
23115
23116
23117
23118
23119
23120
23121
23122
23123
23124
23125
23126
23127
23128
23129
23130
23131
23132
23133
23134
23135
23136
23137
23138
23139
23140
23141
23142
23143
23144
23145
23146
23147
23148
23149
23150
23151
23152
23153
23154
23155
23156
23157
23158
23159
23160
23161
23162
23163
23164
23165
23166
23167
23168
23169
23170
23171
23172
23173
23174
23175
23176
23177
23178
23179
23180
23181
23182
23183
23184
23185
23186
23187
23188
23189
23190
23191
23192
23193
23194
23195
23196
23197
23198
23199
23200
23201
23202
23203
23204
23205
23206
23207
23208
23209
23210
23211
23212
23213
23214
23215
23216
23217
23218
23219
23220
23221
23222
23223
23224
23225
23226
23227
23228
23229
23230
23231
23232
23233
23234
23235
23236
23237
23238
23239
23240
23241
23242
23243
23244
23245
23246
23247
23248
23249
23250
23251
23252
23253
23254
23255
23256
23257
23258
23259
23260
23261
23262
23263
23264
23265
23266
23267
23268
23269
23270
23271
23272
23273
23274
23275
23276
23277
23278
23279
23280
23281
23282
23283
23284
23285
23286
23287
23288
23289
23290
23291
23292
23293
23294
23295
23296
23297
23298
23299
23300
23301
23302
23303
23304
23305
23306
23307
23308
23309
23310
23311
23312
23313
23314
23315
23316
23317
23318
23319
23320
23321
23322
23323
23324
23325
23326
23327
23328
23329
23330
23331
23332
23333
23334
23335
23336
23337
23338
23339
23340
23341
23342
23343
23344
23345
23346
23347
23348
23349
23350
23351
23352
23353
23354
23355
23356
23357
23358
23359
23360
23361
23362
23363
23364
23365
23366
23367
23368
23369
23370
23371
23372
23373
23374
23375
23376
23377
23378
23379
23380
23381
23382
23383
23384
23385
23386
23387
23388
23389
23390
23391
23392
23393
23394
23395
23396
23397
23398
23399
23400
23401
23402
23403
23404
23405
23406
23407
23408
23409
23410
23411
23412
23413
23414
23415
23416
23417
23418
23419
23420
23421
23422
23423
23424
23425
23426
23427
23428
23429
23430
23431
23432
23433
23434
23435
23436
23437
23438
23439
23440
23441
23442
23443
23444
23445
23446
23447
23448
23449
23450
23451
23452
23453
23454
23455
23456
23457
23458
23459
23460
23461
23462
23463
23464
23465
23466
23467
23468
23469
23470
23471
23472
23473
23474
23475
23476
23477
23478
23479
23480
23481
23482
23483
23484
23485
23486
23487
23488
23489
23490
23491
23492
23493
23494
23495
23496
23497
23498
23499
23500
23501
23502
23503
23504
23505
23506
23507
23508
23509
23510
23511
23512
23513
23514
23515
23516
23517
23518
23519
23520
23521
23522
23523
23524
23525
23526
23527
23528
23529
23530
23531
23532
23533
23534
23535
23536
23537
23538
23539
23540
23541
23542
23543
23544
23545
23546
23547
23548
23549
23550
23551
23552
23553
23554
23555
23556
23557
23558
23559
23560
23561
23562
23563
23564
23565
23566
23567
23568
23569
23570
23571
23572
23573
23574
23575
23576
23577
23578
23579
23580
23581
23582
23583
23584
23585
23586
23587
23588
23589
23590
23591
23592
23593
23594
23595
23596
23597
23598
23599
23600
23601
23602
23603
23604
23605
23606
23607
23608
23609
23610
23611
23612
23613
23614
23615
23616
23617
23618
23619
23620
23621
23622
23623
23624
23625
23626
23627
23628
23629
23630
23631
23632
23633
23634
23635
23636
23637
23638
23639
23640
23641
23642
23643
23644
23645
23646
23647
23648
23649
23650
23651
23652
23653
23654
23655
23656
23657
23658
23659
23660
23661
23662
23663
23664
23665
23666
23667
23668
23669
23670
23671
23672
23673
23674
23675
23676
23677
23678
23679
23680
23681
23682
23683
23684
23685
23686
23687
23688
23689
23690
23691
23692
23693
23694
23695
23696
23697
23698
23699
23700
23701
23702
23703
23704
23705
23706
23707
23708
23709
23710
23711
23712
23713
23714
23715
23716
23717
23718
23719
23720
23721
23722
23723
23724
23725
23726
23727
23728
23729
23730
23731
23732
23733
23734
23735
23736
23737
23738
23739
23740
23741
23742
23743
23744
23745
23746
23747
23748
23749
23750
23751
23752
23753
23754
23755
23756
23757
23758
23759
23760
23761
23762
23763
23764
23765
23766
23767
23768
23769
23770
23771
23772
23773
23774
23775
23776
23777
23778
23779
23780
23781
23782
23783
23784
23785
23786
23787
23788
23789
23790
23791
23792
23793
23794
23795
23796
23797
23798
23799
23800
23801
23802
23803
23804
23805
23806
23807
23808
23809
23810
23811
23812
23813
23814
23815
23816
23817
23818
23819
23820
23821
23822
23823
23824
23825
23826
23827
23828
23829
23830
23831
23832
23833
23834
23835
23836
23837
23838
23839
23840
23841
23842
23843
23844
23845
23846
23847
23848
23849
23850
23851
23852
23853
23854
23855
23856
23857
23858
23859
23860
23861
23862
23863
23864
23865
23866
23867
23868
23869
23870
23871
23872
23873
23874
23875
23876
23877
23878
23879
23880
23881
23882
23883
23884
23885
23886
23887
23888
23889
23890
23891
23892
23893
23894
23895
23896
23897
23898
23899
23900
23901
23902
23903
23904
23905
23906
23907
23908
23909
23910
23911
23912
23913
23914
23915
23916
23917
23918
23919
23920
23921
23922
23923
23924
23925
23926
23927
23928
23929
23930
23931
23932
23933
23934
23935
23936
23937
23938
23939
23940
23941
23942
23943
23944
23945
23946
23947
23948
23949
23950
23951
23952
23953
23954
23955
23956
23957
23958
23959
23960
23961
23962
23963
23964
23965
23966
23967
23968
23969
23970
23971
23972
23973
23974
23975
23976
23977
23978
23979
23980
23981
23982
23983
23984
23985
23986
23987
23988
23989
23990
23991
23992
23993
23994
23995
23996
23997
23998
23999
24000
24001
24002
24003
24004
24005
24006
24007
24008
24009
24010
24011
24012
24013
24014
24015
24016
24017
24018
24019
24020
24021
24022
24023
24024
24025
24026
24027
24028
24029
24030
24031
24032
24033
24034
24035
24036
24037
24038
24039
24040
24041
24042
24043
24044
24045
24046
24047
24048
24049
24050
24051
24052
24053
24054
24055
24056
24057
24058
24059
24060
24061
24062
24063
24064
24065
24066
24067
24068
24069
24070
24071
24072
24073
24074
24075
24076
24077
24078
24079
24080
24081
24082
24083
24084
24085
24086
24087
24088
24089
24090
24091
24092
24093
24094
24095
24096
24097
24098
24099
24100
24101
24102
24103
24104
24105
24106
24107
24108
24109
24110
24111
24112
24113
24114
24115
24116
24117
24118
24119
24120
24121
24122
24123
24124
24125
24126
24127
24128
24129
24130
24131
24132
24133
24134
24135
24136
24137
24138
24139
24140
24141
24142
24143
24144
24145
24146
24147
24148
24149
24150
24151
24152
24153
24154
24155
24156
24157
24158
24159
24160
24161
24162
24163
24164
24165
24166
24167
24168
24169
24170
24171
24172
24173
24174
24175
24176
24177
24178
24179
24180
24181
24182
24183
24184
24185
24186
24187
24188
24189
24190
24191
24192
24193
24194
24195
24196
24197
24198
24199
24200
24201
24202
24203
24204
24205
24206
24207
24208
24209
24210
24211
24212
24213
24214
24215
24216
24217
24218
24219
24220
24221
24222
24223
24224
24225
24226
24227
24228
24229
24230
24231
24232
24233
24234
24235
24236
24237
24238
24239
24240
24241
24242
24243
24244
24245
24246
24247
24248
24249
24250
24251
24252
24253
24254
24255
24256
24257
24258
24259
24260
24261
24262
24263
24264
24265
24266
24267
24268
24269
24270
24271
24272
24273
24274
24275
24276
24277
24278
24279
24280
24281
24282
24283
24284
24285
24286
24287
24288
24289
24290
24291
24292
24293
24294
24295
24296
24297
24298
24299
24300
24301
24302
24303
24304
24305
24306
24307
24308
24309
24310
24311
24312
24313
24314
24315
24316
24317
24318
24319
24320
24321
24322
24323
24324
24325
24326
24327
24328
24329
24330
24331
24332
24333
24334
24335
24336
24337
24338
24339
24340
24341
24342
24343
24344
24345
24346
24347
24348
24349
24350
24351
24352
24353
24354
24355
24356
24357
24358
24359
24360
24361
24362
24363
24364
24365
24366
24367
24368
24369
24370
24371
24372
24373
24374
24375
24376
24377
24378
24379
24380
24381
24382
24383
24384
24385
24386
24387
24388
24389
24390
24391
24392
24393
24394
24395
24396
24397
24398
24399
24400
24401
24402
24403
24404
24405
24406
24407
24408
24409
24410
24411
24412
24413
24414
24415
24416
24417
24418
24419
24420
24421
24422
24423
24424
24425
24426
24427
24428
24429
24430
24431
24432
24433
24434
24435
24436
24437
24438
24439
24440
24441
24442
24443
24444
24445
24446
24447
24448
24449
24450
24451
24452
24453
24454
24455
24456
24457
24458
24459
24460
24461
24462
24463
24464
24465
24466
24467
24468
24469
24470
24471
24472
24473
24474
24475
24476
24477
24478
24479
24480
24481
24482
24483
24484
24485
24486
24487
24488
24489
24490
24491
24492
24493
24494
24495
24496
24497
24498
24499
24500
24501
24502
24503
24504
24505
24506
24507
24508
24509
24510
24511
24512
24513
24514
24515
24516
24517
24518
24519
24520
24521
24522
24523
24524
24525
24526
24527
24528
24529
24530
24531
24532
24533
24534
24535
24536
24537
24538
24539
24540
24541
24542
24543
24544
24545
24546
24547
24548
24549
24550
24551
24552
24553
24554
24555
24556
24557
24558
24559
24560
24561
24562
24563
24564
24565
24566
24567
24568
24569
24570
24571
24572
24573
24574
24575
24576
24577
24578
24579
24580
24581
24582
24583
24584
24585
24586
24587
24588
24589
24590
24591
24592
24593
24594
24595
24596
24597
24598
24599
24600
24601
24602
24603
24604
24605
24606
24607
24608
24609
24610
24611
24612
24613
24614
24615
24616
24617
24618
24619
24620
24621
24622
24623
24624
24625
24626
24627
24628
24629
24630
24631
24632
24633
24634
24635
24636
24637
24638
24639
24640
24641
24642
24643
24644
24645
24646
24647
24648
24649
24650
24651
24652
24653
24654
24655
24656
24657
24658
24659
24660
24661
24662
24663
24664
24665
24666
24667
24668
24669
24670
24671
24672
24673
24674
24675
24676
24677
24678
24679
24680
24681
24682
24683
24684
24685
24686
24687
24688
24689
24690
24691
24692
24693
24694
24695
24696
24697
24698
24699
24700
24701
24702
24703
24704
24705
24706
24707
24708
24709
24710
24711
24712
24713
24714
24715
24716
24717
24718
24719
24720
24721
24722
24723
24724
24725
24726
24727
24728
24729
24730
24731
24732
24733
24734
24735
24736
24737
24738
24739
24740
24741
24742
24743
24744
24745
24746
24747
24748
24749
24750
24751
24752
24753
24754
24755
24756
24757
24758
24759
24760
24761
24762
24763
24764
24765
24766
24767
24768
24769
24770
24771
24772
24773
24774
24775
24776
24777
24778
24779
24780
24781
24782
24783
24784
24785
24786
24787
24788
24789
24790
24791
24792
24793
24794
24795
24796
24797
24798
24799
24800
24801
24802
24803
24804
24805
24806
24807
24808
24809
24810
24811
24812
24813
24814
24815
24816
24817
24818
24819
24820
24821
24822
24823
24824
24825
24826
24827
24828
24829
24830
24831
24832
24833
24834
24835
24836
24837
24838
24839
24840
24841
24842
24843
24844
24845
24846
24847
24848
24849
24850
24851
24852
24853
24854
24855
24856
24857
24858
24859
24860
24861
24862
24863
24864
24865
24866
24867
24868
24869
24870
24871
24872
24873
24874
24875
24876
24877
24878
24879
24880
24881
24882
24883
24884
24885
24886
24887
24888
24889
24890
24891
24892
24893
24894
24895
24896
24897
24898
24899
24900
24901
24902
24903
24904
24905
24906
24907
24908
24909
24910
24911
24912
24913
24914
24915
24916
24917
24918
24919
24920
24921
24922
24923
24924
24925
24926
24927
24928
24929
24930
24931
24932
24933
24934
24935
24936
24937
24938
24939
24940
24941
24942
24943
24944
24945
24946
24947
24948
24949
24950
24951
24952
24953
24954
24955
24956
24957
24958
24959
24960
24961
24962
24963
24964
24965
24966
24967
24968
24969
24970
24971
24972
24973
24974
24975
24976
24977
24978
24979
24980
24981
24982
24983
24984
24985
24986
24987
24988
24989
24990
24991
24992
24993
24994
24995
24996
24997
24998
24999
25000
25001
25002
25003
25004
25005
25006
25007
25008
25009
25010
25011
25012
25013
25014
25015
25016
25017
25018
25019
25020
25021
25022
25023
25024
25025
25026
25027
25028
25029
25030
25031
25032
25033
25034
25035
25036
25037
25038
25039
25040
25041
25042
25043
25044
25045
25046
25047
25048
25049
25050
25051
25052
25053
25054
25055
25056
25057
25058
25059
25060
25061
25062
25063
25064
25065
25066
25067
25068
25069
25070
25071
25072
25073
25074
25075
25076
25077
25078
25079
25080
25081
25082
25083
25084
25085
25086
25087
25088
25089
25090
25091
25092
25093
25094
25095
25096
25097
25098
25099
25100
25101
25102
25103
25104
25105
25106
25107
25108
25109
25110
25111
25112
25113
25114
25115
25116
25117
25118
25119
25120
25121
25122
25123
25124
25125
25126
25127
25128
25129
25130
25131
25132
25133
25134
25135
25136
25137
25138
25139
25140
25141
25142
25143
25144
25145
25146
25147
25148
25149
25150
25151
25152
25153
25154
25155
25156
25157
25158
25159
25160
25161
25162
25163
25164
25165
25166
25167
25168
25169
25170
25171
25172
25173
25174
25175
25176
25177
25178
25179
25180
25181
25182
25183
25184
25185
25186
25187
25188
25189
25190
25191
25192
25193
25194
25195
25196
25197
25198
25199
25200
25201
25202
25203
25204
25205
25206
25207
25208
25209
25210
25211
25212
25213
25214
25215
25216
25217
25218
25219
25220
25221
25222
25223
25224
25225
25226
25227
25228
25229
25230
25231
25232
25233
25234
25235
25236
25237
25238
25239
25240
25241
25242
25243
25244
25245
25246
25247
25248
25249
25250
25251
25252
25253
25254
25255
25256
25257
25258
25259
25260
25261
25262
25263
25264
25265
25266
25267
25268
25269
25270
25271
25272
25273
25274
25275
25276
25277
25278
25279
25280
25281
25282
25283
25284
25285
25286
25287
25288
25289
25290
25291
25292
25293
25294
25295
25296
25297
25298
25299
25300
25301
25302
25303
25304
25305
25306
25307
25308
25309
25310
25311
25312
25313
25314
25315
25316
25317
25318
25319
25320
25321
25322
25323
25324
25325
25326
25327
25328
25329
25330
25331
25332
25333
25334
25335
25336
25337
25338
25339
25340
25341
25342
25343
25344
25345
25346
25347
25348
25349
25350
25351
25352
25353
25354
25355
25356
25357
25358
25359
25360
25361
25362
25363
25364
25365
25366
25367
25368
25369
25370
25371
25372
25373
25374
25375
25376
25377
25378
25379
25380
25381
25382
25383
25384
25385
25386
25387
25388
25389
25390
25391
25392
25393
25394
25395
25396
25397
25398
25399
25400
25401
25402
25403
25404
25405
25406
25407
25408
25409
25410
25411
25412
25413
25414
25415
25416
25417
25418
25419
25420
25421
25422
25423
25424
25425
25426
25427
25428
25429
25430
25431
25432
25433
25434
25435
25436
25437
25438
25439
25440
25441
25442
25443
25444
25445
25446
25447
25448
25449
25450
25451
25452
25453
25454
25455
25456
25457
25458
25459
25460
25461
25462
25463
25464
25465
25466
25467
25468
25469
25470
25471
25472
25473
25474
25475
25476
25477
25478
25479
25480
25481
25482
25483
25484
25485
25486
25487
25488
25489
25490
25491
25492
25493
25494
25495
25496
25497
25498
25499
25500
25501
25502
25503
25504
25505
25506
25507
25508
25509
25510
25511
25512
25513
25514
25515
25516
25517
25518
25519
25520
25521
25522
25523
25524
25525
25526
25527
25528
25529
25530
25531
25532
25533
25534
25535
25536
25537
25538
25539
25540
25541
25542
25543
25544
25545
25546
25547
25548
25549
25550
25551
25552
25553
25554
25555
25556
25557
25558
25559
25560
25561
25562
25563
25564
25565
25566
25567
25568
25569
25570
25571
25572
25573
25574
25575
25576
25577
25578
25579
25580
25581
25582
25583
25584
25585
25586
25587
25588
25589
25590
25591
25592
25593
25594
25595
25596
25597
25598
25599
25600
25601
25602
25603
25604
25605
25606
25607
25608
25609
25610
25611
25612
25613
25614
25615
25616
25617
25618
25619
25620
25621
25622
25623
25624
25625
25626
25627
25628
25629
25630
25631
25632
25633
25634
25635
25636
25637
25638
25639
25640
25641
25642
25643
25644
25645
25646
25647
25648
25649
25650
25651
25652
25653
25654
25655
25656
25657
25658
25659
25660
25661
25662
25663
25664
25665
25666
25667
25668
25669
25670
25671
25672
25673
25674
25675
25676
25677
25678
25679
25680
25681
25682
25683
25684
25685
25686
25687
25688
25689
25690
25691
25692
25693
25694
25695
25696
25697
25698
25699
25700
25701
25702
25703
25704
25705
25706
25707
25708
25709
25710
25711
25712
25713
25714
25715
25716
25717
25718
25719
25720
25721
25722
25723
25724
25725
25726
25727
25728
25729
25730
25731
25732
25733
25734
25735
25736
25737
25738
25739
25740
25741
25742
25743
25744
25745
25746
25747
25748
25749
25750
25751
25752
25753
25754
25755
25756
25757
25758
25759
25760
25761
25762
25763
25764
25765
25766
25767
25768
25769
25770
25771
25772
25773
25774
25775
25776
25777
25778
25779
25780
25781
25782
25783
25784
25785
25786
25787
25788
25789
25790
25791
25792
25793
25794
25795
25796
25797
25798
25799
25800
25801
25802
25803
25804
25805
25806
25807
25808
25809
25810
25811
25812
25813
25814
25815
25816
25817
25818
25819
25820
25821
25822
25823
25824
25825
25826
25827
25828
25829
25830
25831
25832
25833
25834
25835
25836
25837
25838
25839
25840
25841
25842
25843
25844
25845
25846
25847
25848
25849
25850
25851
25852
25853
25854
25855
25856
25857
25858
25859
25860
25861
25862
25863
25864
25865
25866
25867
25868
25869
25870
25871
25872
25873
25874
25875
25876
25877
25878
25879
25880
25881
25882
25883
25884
25885
25886
25887
25888
25889
25890
25891
25892
25893
25894
25895
25896
25897
25898
25899
25900
25901
25902
25903
25904
25905
25906
25907
25908
25909
25910
25911
25912
25913
25914
25915
25916
25917
25918
25919
25920
25921
25922
25923
25924
25925
25926
25927
25928
25929
25930
25931
25932
25933
25934
25935
25936
25937
25938
25939
25940
25941
25942
25943
25944
25945
25946
25947
25948
25949
25950
25951
25952
25953
25954
25955
25956
25957
25958
25959
25960
25961
25962
25963
25964
25965
25966
25967
25968
25969
25970
25971
25972
25973
25974
25975
25976
25977
25978
25979
25980
25981
25982
25983
25984
25985
25986
25987
25988
25989
25990
25991
25992
25993
25994
25995
25996
25997
25998
25999
26000
26001
26002
26003
26004
26005
26006
26007
26008
26009
26010
26011
26012
26013
26014
26015
26016
26017
26018
26019
26020
26021
26022
26023
26024
26025
26026
26027
26028
26029
26030
26031
26032
26033
26034
26035
26036
26037
26038
26039
26040
26041
26042
26043
26044
26045
26046
26047
26048
26049
26050
26051
26052
26053
26054
26055
26056
26057
26058
26059
26060
26061
26062
26063
26064
26065
26066
26067
26068
26069
26070
26071
26072
26073
26074
26075
26076
26077
26078
26079
26080
26081
26082
26083
26084
26085
26086
26087
26088
26089
26090
26091
26092
26093
26094
26095
26096
26097
26098
26099
26100
26101
26102
26103
26104
26105
26106
26107
26108
26109
26110
26111
26112
26113
26114
26115
26116
26117
26118
26119
26120
26121
26122
26123
26124
26125
26126
26127
26128
26129
26130
26131
26132
26133
26134
26135
26136
26137
26138
26139
26140
26141
26142
26143
26144
26145
26146
26147
26148
26149
26150
26151
26152
26153
26154
26155
26156
26157
26158
26159
26160
26161
26162
26163
26164
26165
26166
26167
26168
26169
26170
26171
26172
26173
26174
26175
26176
26177
26178
26179
26180
26181
26182
26183
26184
26185
26186
26187
26188
26189
26190
26191
26192
26193
26194
26195
26196
26197
26198
26199
26200
26201
26202
26203
26204
26205
26206
26207
26208
26209
26210
26211
26212
26213
26214
26215
26216
26217
26218
26219
26220
26221
26222
26223
26224
26225
26226
26227
26228
26229
26230
26231
26232
26233
26234
26235
26236
26237
26238
26239
26240
26241
26242
26243
26244
26245
26246
26247
26248
26249
26250
26251
26252
26253
26254
26255
26256
26257
26258
26259
26260
26261
26262
26263
26264
26265
26266
26267
26268
26269
26270
26271
26272
26273
26274
26275
26276
26277
26278
26279
26280
26281
26282
26283
26284
26285
26286
26287
26288
26289
26290
26291
26292
26293
26294
26295
26296
26297
26298
26299
26300
26301
26302
26303
26304
26305
26306
26307
26308
26309
26310
26311
26312
26313
26314
26315
26316
26317
26318
26319
26320
26321
26322
26323
26324
26325
26326
26327
26328
26329
26330
26331
26332
26333
26334
26335
26336
26337
26338
26339
26340
26341
26342
26343
26344
26345
26346
26347
26348
26349
26350
26351
26352
26353
26354
26355
26356
26357
26358
26359
26360
26361
26362
26363
26364
26365
26366
26367
26368
26369
26370
26371
26372
26373
26374
26375
26376
26377
26378
26379
26380
26381
26382
26383
26384
26385
26386
26387
26388
26389
26390
26391
26392
26393
26394
26395
26396
26397
26398
26399
26400
26401
26402
26403
26404
26405
26406
26407
26408
26409
26410
26411
26412
26413
26414
26415
26416
26417
26418
26419
26420
26421
26422
26423
26424
26425
26426
26427
26428
26429
26430
26431
26432
26433
26434
26435
26436
26437
26438
26439
26440
26441
26442
26443
26444
26445
26446
26447
26448
26449
26450
26451
26452
26453
26454
26455
26456
26457
26458
26459
26460
26461
26462
26463
26464
26465
26466
26467
26468
26469
26470
26471
26472
26473
26474
26475
26476
26477
26478
26479
26480
26481
26482
26483
26484
26485
26486
26487
26488
26489
26490
26491
26492
26493
26494
26495
26496
26497
26498
26499
26500
26501
26502
26503
26504
26505
26506
26507
26508
26509
26510
26511
26512
26513
26514
26515
26516
26517
26518
26519
26520
26521
26522
26523
26524
26525
26526
26527
26528
26529
26530
26531
26532
26533
26534
26535
26536
26537
26538
26539
26540
26541
26542
26543
26544
26545
26546
26547
26548
26549
26550
26551
26552
26553
26554
26555
26556
26557
26558
26559
26560
26561
26562
26563
26564
26565
26566
26567
26568
26569
26570
26571
26572
26573
26574
26575
26576
26577
26578
26579
26580
26581
26582
26583
26584
26585
26586
26587
26588
26589
26590
26591
26592
26593
26594
26595
26596
26597
26598
26599
26600
26601
26602
26603
26604
26605
26606
26607
26608
26609
26610
26611
26612
26613
26614
26615
26616
26617
26618
26619
26620
26621
26622
26623
26624
26625
26626
26627
26628
26629
26630
26631
26632
26633
26634
26635
26636
26637
26638
26639
26640
26641
26642
26643
26644
26645
26646
26647
26648
26649
26650
26651
26652
26653
26654
26655
26656
26657
26658
26659
26660
26661
26662
26663
26664
26665
26666
26667
26668
26669
26670
26671
26672
26673
26674
26675
26676
26677
26678
26679
26680
26681
26682
26683
26684
26685
26686
26687
26688
26689
26690
26691
26692
26693
26694
26695
26696
26697
26698
26699
26700
26701
26702
26703
26704
26705
26706
26707
26708
26709
26710
26711
26712
26713
26714
26715
26716
26717
26718
26719
26720
26721
26722
26723
26724
26725
26726
26727
26728
26729
26730
26731
26732
26733
26734
26735
26736
26737
26738
26739
26740
26741
26742
26743
26744
26745
26746
26747
26748
26749
26750
26751
26752
26753
26754
26755
26756
26757
26758
26759
26760
26761
26762
26763
26764
26765
26766
26767
26768
26769
26770
26771
26772
26773
26774
26775
26776
26777
26778
26779
26780
26781
26782
26783
26784
26785
26786
26787
26788
26789
26790
26791
26792
26793
26794
26795
26796
26797
26798
26799
26800
26801
26802
26803
26804
26805
26806
26807
26808
26809
26810
26811
26812
26813
26814
26815
26816
26817
26818
26819
26820
26821
26822
26823
26824
26825
26826
26827
26828
26829
26830
26831
26832
26833
26834
26835
26836
26837
26838
26839
26840
26841
26842
26843
26844
26845
26846
26847
26848
26849
26850
26851
26852
26853
26854
26855
26856
26857
26858
26859
26860
26861
26862
26863
26864
26865
26866
26867
26868
26869
26870
26871
26872
26873
26874
26875
26876
26877
26878
26879
26880
26881
26882
26883
26884
26885
26886
26887
26888
26889
26890
26891
26892
26893
26894
26895
26896
26897
26898
26899
26900
26901
26902
26903
26904
26905
26906
26907
26908
26909
26910
26911
26912
26913
26914
26915
26916
26917
26918
26919
26920
26921
26922
26923
26924
26925
26926
26927
26928
26929
26930
26931
26932
26933
26934
26935
26936
26937
26938
26939
26940
26941
26942
26943
26944
26945
26946
26947
26948
26949
26950
26951
26952
26953
26954
26955
26956
26957
26958
26959
26960
26961
26962
26963
26964
26965
26966
26967
26968
26969
26970
26971
26972
26973
26974
26975
26976
26977
26978
26979
26980
26981
26982
26983
26984
26985
26986
26987
26988
26989
26990
26991
26992
26993
26994
26995
26996
26997
26998
26999
27000
27001
27002
27003
27004
27005
27006
27007
27008
27009
27010
27011
27012
27013
27014
27015
27016
27017
27018
27019
27020
27021
27022
27023
27024
27025
27026
27027
27028
27029
27030
27031
27032
27033
27034
27035
27036
27037
27038
27039
27040
27041
27042
27043
27044
27045
27046
27047
27048
27049
27050
27051
27052
27053
27054
27055
27056
27057
27058
27059
27060
27061
27062
27063
27064
27065
27066
27067
27068
27069
27070
27071
27072
27073
27074
27075
27076
27077
27078
27079
27080
27081
27082
27083
27084
27085
27086
27087
27088
27089
27090
27091
27092
27093
27094
27095
27096
27097
27098
27099
27100
27101
27102
27103
27104
27105
27106
27107
27108
27109
27110
27111
27112
27113
27114
27115
27116
27117
27118
27119
27120
27121
27122
27123
27124
27125
27126
27127
27128
27129
27130
27131
27132
27133
27134
27135
27136
27137
27138
27139
27140
27141
27142
27143
27144
27145
27146
27147
27148
27149
27150
27151
27152
27153
27154
27155
27156
27157
27158
27159
27160
27161
27162
27163
27164
27165
27166
27167
27168
27169
27170
27171
27172
27173
27174
27175
27176
27177
27178
27179
27180
27181
27182
27183
27184
27185
27186
27187
27188
27189
27190
27191
27192
27193
27194
27195
27196
27197
27198
27199
27200
27201
27202
27203
27204
27205
27206
27207
27208
27209
27210
27211
27212
27213
27214
27215
27216
27217
27218
27219
27220
27221
27222
27223
27224
27225
27226
27227
27228
27229
27230
27231
27232
27233
27234
27235
27236
27237
27238
27239
27240
27241
27242
27243
27244
27245
27246
27247
27248
27249
27250
27251
27252
27253
27254
27255
27256
27257
27258
27259
27260
27261
27262
27263
27264
27265
27266
27267
27268
27269
27270
27271
27272
27273
27274
27275
27276
27277
27278
27279
27280
27281
27282
27283
27284
27285
27286
27287
27288
27289
27290
27291
27292
27293
27294
27295
27296
27297
27298
27299
27300
27301
27302
27303
27304
27305
27306
27307
27308
27309
27310
27311
27312
27313
27314
27315
27316
27317
27318
27319
27320
27321
27322
27323
27324
27325
27326
27327
27328
27329
27330
27331
27332
27333
27334
27335
27336
27337
27338
27339
27340
27341
27342
27343
27344
27345
27346
27347
27348
27349
27350
27351
27352
27353
27354
27355
27356
27357
27358
27359
27360
27361
27362
27363
27364
27365
27366
27367
27368
27369
27370
27371
27372
27373
27374
27375
27376
27377
27378
27379
27380
27381
27382
27383
27384
27385
27386
27387
27388
27389
27390
27391
27392
27393
27394
27395
27396
27397
27398
27399
27400
27401
27402
27403
27404
27405
27406
27407
27408
27409
27410
27411
27412
27413
27414
27415
27416
27417
27418
27419
27420
27421
27422
27423
27424
27425
27426
27427
27428
27429
27430
27431
27432
27433
27434
27435
27436
27437
27438
27439
27440
27441
27442
27443
27444
27445
27446
27447
27448
27449
27450
27451
27452
27453
27454
27455
27456
27457
27458
27459
27460
27461
27462
27463
27464
27465
27466
27467
27468
27469
27470
27471
27472
27473
27474
27475
27476
27477
27478
27479
27480
27481
27482
27483
27484
27485
27486
27487
27488
27489
27490
27491
27492
27493
27494
27495
27496
27497
27498
27499
27500
27501
27502
27503
27504
27505
27506
27507
27508
27509
27510
27511
27512
27513
27514
27515
27516
27517
27518
27519
27520
27521
27522
27523
27524
27525
27526
27527
27528
27529
27530
27531
27532
27533
27534
27535
27536
27537
27538
27539
27540
27541
27542
27543
27544
27545
27546
27547
27548
27549
27550
27551
27552
27553
27554
27555
27556
27557
27558
27559
27560
27561
27562
27563
27564
27565
27566
27567
27568
27569
27570
27571
27572
27573
27574
27575
27576
27577
27578
27579
27580
27581
27582
27583
27584
27585
27586
27587
27588
27589
27590
27591
27592
27593
27594
27595
27596
27597
27598
27599
27600
27601
27602
27603
27604
27605
27606
27607
27608
27609
27610
27611
27612
27613
27614
27615
27616
27617
27618
27619
27620
27621
27622
27623
27624
27625
27626
27627
27628
27629
27630
27631
27632
27633
27634
27635
27636
27637
27638
27639
27640
27641
27642
27643
27644
27645
27646
27647
27648
27649
27650
27651
27652
27653
27654
27655
27656
27657
27658
27659
27660
27661
27662
27663
27664
27665
27666
27667
27668
27669
27670
27671
27672
27673
27674
27675
27676
27677
27678
27679
27680
27681
27682
27683
27684
27685
27686
27687
27688
27689
27690
27691
27692
27693
27694
27695
27696
27697
27698
27699
27700
27701
27702
27703
27704
27705
27706
27707
27708
27709
27710
27711
27712
27713
27714
27715
27716
27717
27718
27719
27720
27721
27722
27723
27724
27725
27726
27727
27728
27729
27730
27731
27732
27733
27734
27735
27736
27737
27738
27739
27740
27741
27742
27743
27744
27745
27746
27747
27748
27749
27750
27751
27752
27753
27754
27755
27756
27757
27758
27759
27760
27761
27762
27763
27764
27765
27766
27767
27768
27769
27770
27771
27772
27773
27774
27775
27776
27777
27778
27779
27780
27781
27782
27783
27784
27785
27786
27787
27788
27789
27790
27791
27792
27793
27794
27795
27796
27797
27798
27799
27800
27801
27802
27803
27804
27805
27806
27807
27808
27809
27810
27811
27812
27813
27814
27815
27816
27817
27818
27819
27820
27821
27822
27823
27824
27825
27826
27827
27828
27829
27830
27831
27832
27833
27834
27835
27836
27837
27838
27839
27840
27841
27842
27843
27844
27845
27846
27847
27848
27849
27850
27851
27852
27853
27854
27855
27856
27857
27858
27859
27860
27861
27862
27863
27864
27865
27866
27867
27868
27869
27870
27871
27872
27873
27874
27875
27876
27877
27878
27879
27880
27881
27882
27883
27884
27885
27886
27887
27888
27889
27890
27891
27892
27893
27894
27895
27896
27897
27898
27899
27900
27901
27902
27903
27904
27905
27906
27907
27908
27909
27910
27911
27912
27913
27914
27915
27916
27917
27918
27919
27920
27921
27922
27923
27924
27925
27926
27927
27928
27929
27930
27931
27932
27933
27934
27935
27936
27937
27938
27939
27940
27941
27942
27943
27944
27945
27946
27947
27948
27949
27950
27951
27952
27953
27954
27955
27956
27957
27958
27959
27960
27961
27962
27963
27964
27965
27966
27967
27968
27969
27970
27971
27972
27973
27974
27975
27976
27977
27978
27979
27980
27981
27982
27983
27984
27985
27986
27987
27988
27989
27990
27991
27992
27993
27994
27995
27996
27997
27998
27999
28000
28001
28002
28003
28004
28005
28006
28007
28008
28009
28010
28011
28012
28013
28014
28015
28016
28017
28018
28019
28020
28021
28022
28023
28024
28025
28026
28027
28028
28029
28030
28031
28032
28033
28034
28035
28036
28037
28038
28039
28040
28041
28042
28043
28044
28045
28046
28047
28048
28049
28050
28051
28052
28053
28054
28055
28056
28057
28058
28059
28060
28061
28062
28063
28064
28065
28066
28067
28068
28069
28070
28071
28072
28073
28074
28075
28076
28077
28078
28079
28080
28081
28082
28083
28084
28085
28086
28087
28088
28089
28090
28091
28092
28093
28094
28095
28096
28097
28098
28099
28100
28101
28102
28103
28104
28105
28106
28107
28108
28109
28110
28111
28112
28113
28114
28115
28116
28117
28118
28119
28120
28121
28122
28123
28124
28125
28126
28127
28128
28129
28130
28131
28132
28133
28134
28135
28136
28137
28138
28139
28140
28141
28142
28143
28144
28145
28146
28147
28148
28149
28150
28151
28152
28153
28154
28155
28156
28157
28158
28159
28160
28161
28162
28163
28164
28165
28166
28167
28168
28169
28170
28171
28172
28173
28174
28175
28176
28177
28178
28179
28180
28181
28182
28183
28184
28185
28186
28187
28188
28189
28190
28191
28192
28193
28194
28195
28196
28197
28198
28199
28200
28201
28202
28203
28204
28205
28206
28207
28208
28209
28210
28211
28212
28213
28214
28215
28216
28217
28218
28219
28220
28221
28222
28223
28224
28225
28226
28227
28228
28229
28230
28231
28232
28233
28234
28235
28236
28237
28238
28239
28240
28241
28242
28243
28244
28245
28246
28247
28248
28249
28250
28251
28252
28253
28254
28255
28256
28257
28258
28259
28260
28261
28262
28263
28264
28265
28266
28267
28268
28269
28270
28271
28272
28273
28274
28275
28276
28277
28278
28279
28280
28281
28282
28283
28284
28285
28286
28287
28288
28289
28290
28291
28292
28293
28294
28295
28296
28297
28298
28299
28300
28301
28302
28303
28304
28305
28306
28307
28308
28309
28310
28311
28312
28313
28314
28315
28316
28317
28318
28319
28320
28321
28322
28323
28324
28325
28326
28327
28328
28329
28330
28331
28332
28333
28334
28335
28336
28337
28338
28339
28340
28341
28342
28343
28344
28345
28346
28347
28348
28349
28350
28351
28352
28353
28354
28355
28356
28357
28358
28359
28360
28361
28362
28363
28364
28365
28366
28367
28368
28369
28370
28371
28372
28373
28374
28375
28376
28377
28378
28379
28380
28381
28382
28383
28384
28385
28386
28387
28388
28389
28390
28391
28392
28393
28394
28395
28396
28397
28398
28399
28400
28401
28402
28403
28404
28405
28406
28407
28408
28409
28410
28411
28412
28413
28414
28415
28416
28417
28418
28419
28420
28421
28422
28423
28424
28425
28426
28427
28428
28429
28430
28431
28432
28433
28434
28435
28436
28437
28438
28439
28440
28441
28442
28443
28444
28445
28446
28447
28448
28449
28450
28451
28452
28453
28454
28455
28456
28457
28458
28459
28460
28461
28462
28463
28464
28465
28466
28467
28468
28469
28470
28471
28472
28473
28474
28475
28476
28477
28478
28479
28480
28481
28482
28483
28484
28485
28486
28487
28488
28489
28490
28491
28492
28493
28494
28495
28496
28497
28498
28499
28500
28501
28502
28503
28504
28505
28506
28507
28508
28509
28510
28511
28512
28513
28514
28515
28516
28517
28518
28519
28520
28521
28522
28523
28524
28525
28526
28527
28528
28529
28530
28531
28532
28533
28534
28535
28536
28537
28538
28539
28540
28541
28542
28543
28544
28545
28546
28547
28548
28549
28550
28551
28552
28553
28554
28555
28556
28557
28558
28559
28560
28561
28562
28563
28564
28565
28566
28567
28568
28569
28570
28571
28572
28573
28574
28575
28576
28577
28578
28579
28580
28581
28582
28583
28584
28585
28586
28587
28588
28589
28590
28591
28592
28593
28594
28595
28596
28597
28598
28599
28600
28601
28602
28603
28604
28605
28606
28607
28608
28609
28610
28611
28612
28613
28614
28615
28616
28617
28618
28619
28620
28621
28622
28623
28624
28625
28626
28627
28628
28629
28630
28631
28632
28633
28634
28635
28636
28637
28638
28639
28640
28641
28642
28643
28644
28645
28646
28647
28648
28649
28650
28651
28652
28653
28654
28655
28656
28657
28658
28659
28660
28661
28662
28663
28664
28665
28666
28667
28668
28669
28670
28671
28672
28673
28674
28675
28676
28677
28678
28679
28680
28681
28682
28683
28684
28685
28686
28687
28688
28689
28690
28691
28692
28693
28694
28695
28696
28697
28698
28699
28700
28701
28702
28703
28704
28705
28706
28707
28708
28709
28710
28711
28712
28713
28714
28715
28716
28717
28718
28719
28720
28721
28722
28723
28724
28725
28726
28727
28728
28729
28730
28731
28732
28733
28734
28735
28736
28737
28738
28739
28740
28741
28742
28743
28744
28745
28746
28747
28748
28749
28750
28751
28752
28753
28754
28755
28756
28757
28758
28759
28760
28761
28762
28763
28764
28765
28766
28767
28768
28769
28770
28771
28772
28773
28774
28775
28776
28777
28778
28779
28780
28781
28782
28783
28784
28785
28786
28787
28788
28789
28790
28791
28792
28793
28794
28795
28796
28797
28798
28799
28800
28801
28802
28803
28804
28805
28806
28807
28808
28809
28810
28811
28812
28813
28814
28815
28816
28817
28818
28819
28820
28821
28822
28823
28824
28825
28826
28827
28828
28829
28830
28831
28832
28833
28834
28835
28836
28837
28838
28839
28840
28841
28842
28843
28844
28845
28846
28847
28848
28849
28850
28851
28852
28853
28854
28855
28856
28857
28858
28859
28860
28861
28862
28863
28864
28865
28866
28867
28868
28869
28870
28871
28872
28873
28874
28875
28876
28877
28878
28879
28880
28881
28882
28883
28884
28885
28886
28887
28888
28889
28890
28891
28892
28893
28894
28895
28896
28897
28898
28899
28900
28901
28902
28903
28904
28905
28906
28907
28908
28909
28910
28911
28912
28913
28914
28915
28916
28917
28918
28919
28920
28921
28922
28923
28924
28925
28926
28927
28928
28929
28930
28931
28932
28933
28934
28935
28936
28937
28938
28939
28940
28941
28942
28943
28944
28945
28946
28947
28948
28949
28950
28951
28952
28953
28954
28955
28956
28957
28958
28959
28960
28961
28962
28963
28964
28965
28966
28967
28968
28969
28970
28971
28972
28973
28974
28975
28976
28977
28978
28979
28980
28981
28982
28983
28984
28985
28986
28987
28988
28989
28990
28991
28992
28993
28994
28995
28996
28997
28998
28999
29000
29001
29002
29003
29004
29005
29006
29007
29008
29009
29010
29011
29012
29013
29014
29015
29016
29017
29018
29019
29020
29021
29022
29023
29024
29025
29026
29027
29028
29029
29030
29031
29032
29033
29034
29035
29036
29037
29038
29039
29040
29041
29042
29043
29044
29045
29046
29047
29048
29049
29050
29051
29052
29053
29054
29055
29056
29057
29058
29059
29060
29061
29062
29063
29064
29065
29066
29067
29068
29069
29070
29071
29072
29073
29074
29075
29076
29077
29078
29079
29080
29081
29082
29083
29084
29085
29086
29087
29088
29089
29090
29091
29092
29093
29094
29095
29096
29097
29098
29099
29100
29101
29102
29103
29104
29105
29106
29107
29108
29109
29110
29111
29112
29113
29114
29115
29116
29117
29118
29119
29120
29121
29122
29123
29124
29125
29126
29127
29128
29129
29130
29131
29132
29133
29134
29135
29136
29137
29138
29139
29140
29141
29142
29143
29144
29145
29146
29147
29148
29149
29150
29151
29152
29153
29154
29155
29156
29157
29158
29159
29160
29161
29162
29163
29164
29165
29166
29167
29168
29169
29170
29171
29172
29173
29174
29175
29176
29177
29178
29179
29180
29181
29182
29183
29184
29185
29186
29187
29188
29189
29190
29191
29192
29193
29194
29195
29196
29197
29198
29199
29200
29201
29202
29203
29204
29205
29206
29207
29208
29209
29210
29211
29212
29213
29214
29215
29216
29217
29218
29219
29220
29221
29222
29223
29224
29225
29226
29227
29228
29229
29230
29231
29232
29233
29234
29235
29236
29237
29238
29239
29240
29241
29242
29243
29244
29245
29246
29247
29248
29249
29250
29251
29252
29253
29254
29255
29256
29257
29258
29259
29260
29261
29262
29263
29264
29265
29266
29267
29268
29269
29270
29271
29272
29273
29274
29275
29276
29277
29278
29279
29280
29281
29282
29283
29284
29285
29286
29287
29288
29289
29290
29291
29292
29293
29294
29295
29296
29297
29298
29299
29300
29301
29302
29303
29304
29305
29306
29307
29308
29309
29310
29311
29312
29313
29314
29315
29316
29317
29318
29319
29320
29321
29322
29323
29324
29325
29326
29327
29328
29329
29330
29331
29332
29333
29334
29335
29336
29337
29338
29339
29340
29341
29342
29343
29344
29345
29346
29347
29348
29349
29350
29351
29352
29353
29354
29355
29356
29357
29358
29359
29360
29361
29362
29363
29364
29365
29366
29367
29368
29369
29370
29371
29372
29373
29374
29375
29376
29377
29378
29379
29380
29381
29382
29383
29384
29385
29386
29387
29388
29389
29390
29391
29392
29393
29394
29395
29396
29397
29398
29399
29400
29401
29402
29403
29404
29405
29406
29407
29408
29409
29410
29411
29412
29413
29414
29415
29416
29417
29418
29419
29420
29421
29422
29423
29424
29425
29426
29427
29428
29429
29430
29431
29432
29433
29434
29435
29436
29437
29438
29439
29440
29441
29442
29443
29444
29445
29446
29447
29448
29449
29450
29451
29452
29453
29454
29455
29456
29457
29458
29459
29460
29461
29462
29463
29464
29465
29466
29467
29468
29469
29470
29471
29472
29473
29474
29475
29476
29477
29478
29479
29480
29481
29482
29483
29484
29485
29486
29487
29488
29489
29490
29491
29492
29493
29494
29495
29496
29497
29498
29499
29500
29501
29502
29503
29504
29505
29506
29507
29508
29509
29510
29511
29512
29513
29514
29515
29516
29517
29518
29519
29520
29521
29522
29523
29524
29525
29526
29527
29528
29529
29530
29531
29532
29533
29534
29535
29536
29537
29538
29539
29540
29541
29542
29543
29544
29545
29546
29547
29548
29549
29550
29551
29552
29553
29554
29555
29556
29557
29558
29559
29560
29561
29562
29563
29564
29565
29566
29567
29568
29569
29570
29571
29572
29573
29574
29575
29576
29577
29578
29579
29580
29581
29582
29583
29584
29585
29586
29587
29588
29589
29590
29591
29592
29593
29594
29595
29596
29597
29598
29599
29600
29601
29602
29603
29604
29605
29606
29607
29608
29609
29610
29611
29612
29613
29614
29615
29616
29617
29618
29619
29620
29621
29622
29623
29624
29625
29626
29627
29628
29629
29630
29631
29632
29633
29634
29635
29636
29637
29638
29639
29640
29641
29642
29643
29644
29645
29646
29647
29648
29649
29650
29651
29652
29653
29654
29655
29656
29657
29658
29659
29660
29661
29662
29663
29664
29665
29666
29667
29668
29669
29670
29671
29672
29673
29674
29675
29676
29677
29678
29679
29680
29681
29682
29683
29684
29685
29686
29687
29688
29689
29690
29691
29692
29693
29694
29695
29696
29697
29698
29699
29700
29701
29702
29703
29704
29705
29706
29707
29708
29709
29710
29711
29712
29713
29714
29715
29716
29717
29718
29719
29720
29721
29722
29723
29724
29725
29726
29727
29728
29729
29730
29731
29732
29733
29734
29735
29736
29737
29738
29739
29740
29741
29742
29743
29744
29745
29746
29747
29748
29749
29750
29751
29752
29753
29754
29755
29756
29757
29758
29759
29760
29761
29762
29763
29764
29765
29766
29767
29768
29769
29770
29771
29772
29773
29774
29775
29776
29777
29778
29779
29780
29781
29782
29783
29784
29785
29786
29787
29788
29789
29790
29791
29792
29793
29794
29795
29796
29797
29798
29799
29800
29801
29802
29803
29804
29805
29806
29807
29808
29809
29810
29811
29812
29813
29814
29815
29816
29817
29818
29819
29820
29821
29822
29823
29824
29825
29826
29827
29828
29829
29830
29831
29832
29833
29834
29835
29836
29837
29838
29839
29840
29841
29842
29843
29844
29845
29846
29847
29848
29849
29850
29851
29852
29853
29854
29855
29856
29857
29858
29859
29860
29861
29862
29863
29864
29865
29866
29867
29868
29869
29870
29871
29872
29873
29874
29875
29876
29877
29878
29879
29880
29881
29882
29883
29884
29885
29886
29887
29888
29889
29890
29891
29892
29893
29894
29895
29896
29897
29898
29899
29900
29901
29902
29903
29904
29905
29906
29907
29908
29909
29910
29911
29912
29913
29914
29915
29916
29917
29918
29919
29920
29921
29922
29923
29924
29925
29926
29927
29928
29929
29930
29931
29932
29933
29934
29935
29936
29937
29938
29939
29940
29941
29942
29943
29944
29945
29946
29947
29948
29949
29950
29951
29952
29953
29954
29955
29956
29957
29958
29959
29960
29961
29962
29963
29964
29965
29966
29967
29968
29969
29970
29971
29972
29973
29974
29975
29976
29977
29978
29979
29980
29981
29982
29983
29984
29985
29986
29987
29988
29989
29990
29991
29992
29993
29994
29995
29996
29997
29998
29999
30000
30001
30002
30003
30004
30005
30006
30007
30008
30009
30010
30011
30012
30013
30014
30015
30016
30017
30018
30019
30020
30021
30022
30023
30024
30025
30026
30027
30028
30029
30030
30031
30032
30033
30034
30035
30036
30037
30038
30039
30040
30041
30042
30043
30044
30045
30046
30047
30048
30049
30050
30051
30052
30053
30054
30055
30056
30057
30058
30059
30060
30061
30062
30063
30064
30065
30066
30067
30068
30069
30070
30071
30072
30073
30074
30075
30076
30077
30078
30079
30080
30081
30082
30083
30084
30085
30086
30087
30088
30089
30090
30091
30092
30093
30094
30095
30096
30097
30098
30099
30100
30101
30102
30103
30104
30105
30106
30107
30108
30109
30110
30111
30112
30113
30114
30115
30116
30117
30118
30119
30120
30121
30122
30123
30124
30125
30126
30127
30128
30129
30130
30131
30132
30133
30134
30135
30136
30137
30138
30139
30140
30141
30142
30143
30144
30145
30146
30147
30148
30149
30150
30151
30152
30153
30154
30155
30156
30157
30158
30159
30160
30161
30162
30163
30164
30165
30166
30167
30168
30169
30170
30171
30172
30173
30174
30175
30176
30177
30178
30179
30180
30181
30182
30183
30184
30185
30186
30187
30188
30189
30190
30191
30192
30193
30194
30195
30196
30197
30198
30199
30200
30201
30202
30203
30204
30205
30206
30207
30208
30209
30210
30211
30212
30213
30214
30215
30216
30217
30218
30219
30220
30221
30222
30223
30224
30225
30226
30227
30228
30229
30230
30231
30232
30233
30234
30235
30236
30237
30238
30239
30240
30241
30242
30243
30244
30245
30246
30247
30248
30249
30250
30251
30252
30253
30254
30255
30256
30257
30258
30259
30260
30261
30262
30263
30264
30265
30266
30267
30268
30269
30270
30271
30272
30273
30274
30275
30276
30277
30278
30279
30280
30281
30282
30283
30284
30285
30286
30287
30288
30289
30290
30291
30292
30293
30294
30295
30296
30297
30298
30299
30300
30301
30302
30303
30304
30305
30306
30307
30308
30309
30310
30311
30312
30313
30314
30315
30316
30317
30318
30319
30320
30321
30322
30323
30324
30325
30326
30327
30328
30329
30330
30331
30332
30333
30334
30335
30336
30337
30338
30339
30340
30341
30342
30343
30344
30345
30346
30347
30348
30349
30350
30351
30352
30353
30354
30355
30356
30357
30358
30359
30360
30361
30362
30363
30364
30365
30366
30367
30368
30369
30370
30371
30372
30373
30374
30375
30376
30377
30378
30379
30380
30381
30382
30383
30384
30385
30386
30387
30388
30389
30390
30391
30392
30393
30394
30395
30396
30397
30398
30399
30400
30401
30402
30403
30404
30405
30406
30407
30408
30409
30410
30411
30412
30413
30414
30415
30416
30417
30418
30419
30420
30421
30422
30423
30424
30425
30426
30427
30428
30429
30430
30431
30432
30433
30434
30435
30436
30437
30438
30439
30440
30441
30442
30443
30444
30445
30446
30447
30448
30449
30450
30451
30452
30453
30454
30455
30456
30457
30458
30459
30460
30461
30462
30463
30464
30465
30466
30467
30468
30469
30470
30471
30472
30473
30474
30475
30476
30477
30478
30479
30480
30481
30482
30483
30484
30485
30486
30487
30488
30489
30490
30491
30492
30493
30494
30495
30496
30497
30498
30499
30500
30501
30502
30503
30504
30505
30506
30507
30508
30509
30510
30511
30512
30513
30514
30515
30516
30517
30518
30519
30520
30521
30522
30523
30524
30525
30526
30527
30528
30529
30530
30531
30532
30533
30534
30535
30536
30537
30538
30539
30540
30541
30542
30543
30544
30545
30546
30547
30548
30549
30550
30551
30552
30553
30554
30555
30556
30557
30558
30559
30560
30561
30562
30563
30564
30565
30566
30567
30568
30569
30570
30571
30572
30573
30574
30575
30576
30577
30578
30579
30580
30581
30582
30583
30584
30585
30586
30587
30588
30589
30590
30591
30592
30593
30594
30595
30596
30597
30598
30599
30600
30601
30602
30603
30604
30605
30606
30607
30608
30609
30610
30611
30612
30613
30614
30615
30616
30617
30618
30619
30620
30621
30622
30623
30624
30625
30626
30627
30628
30629
30630
30631
30632
30633
30634
30635
30636
30637
30638
30639
30640
30641
30642
30643
30644
30645
30646
30647
30648
30649
30650
30651
30652
30653
30654
30655
30656
30657
30658
30659
30660
30661
30662
30663
30664
30665
30666
30667
30668
30669
30670
30671
30672
30673
30674
30675
30676
30677
30678
30679
30680
30681
30682
30683
30684
30685
30686
30687
30688
30689
30690
30691
30692
30693
30694
30695
30696
30697
30698
30699
30700
30701
30702
30703
30704
30705
30706
30707
class Payload:
    """Class Payload is used to process Terrarium payload."""

    logger: logging.Logger = default_logger

    # _debug controls whether or not transport processing is
    # stopped if one transport fails:
    _debug: bool = False
    _otcs: OTCS
    _otcs_backend: OTCS
    _otcs_frontend: OTCS
    _otac: OTAC | None
    _otds: OTDS
    _otiv: OTIV | None
    _otpd: OTPD | None
    _otmm: OTMM | None
    _pht: None
    _nhc: None
    _otcs_source: OTCS | None
    _k8s: K8s | None
    _http_object: HTTP | None
    _m365: M365 | None
    _core_share: CoreShare | None
    _sap: SAP | None
    _successfactors: SuccessFactors | None
    _salesforce: Salesforce | None
    _servicenow: ServiceNow | None
    _guidewire_policy_center: Guidewire | None
    _guidewire_claims_center: Guidewire | None
    _custom_settings_dir = ""
    _otawp: OTAWP | None
    _otca: OTCA | None
    _otkd: OTKD | None
    _avts: AVTS | None

    # _payload_source (string): This is either path + filename of the yaml payload
    # or an path + filename of the Terraform HCL payload
    _payload_source = ""

    # _payload is a dict of the complete payload file.
    # It is initialized by the init_payload() method:
    _payload = {}

    """
    _payload_sections is a list of dicts with these keys:
    - name (str)
    - enabled (bool)
    - restart (bool)
    See below and method init_payload() for details of existing payload sections
    """
    _payload_sections = []

    # Declare options dict to read payloadOptions
    _payload_options = {}

    #
    # Initialize payload section variables. They are all list of dicts:
    #

    """
    _webhooks and webhooks_post: List of webHooks.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - description (str, optional)
    - url (str, mandatory)
    - method (str) - either POST, PUT, GET
    - payload (dict, optional, default = {})
    - headers (dict, optional, default = {})
    """
    _webhooks = []
    _webhooks_post = []

    """
    _resources: List of OTDS resources.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory)
    - description (str, optional)
    - display_name (str, optional)
    - activate (bool, optional, default = True) - if a secret is provided the resource will automatically be activated
    - allow_impersonation (bool, optional, default = True)
    - resource_id (str, optional, default = None) - a predefined resource ID. If specified, also secrethas to be provided
    - secret (string, optional, default = None) - a predefined secret. Should be 24 characters long and should end with '=='
    - additional_payload (dict, optional)
    """
    _resources = []

    """
    _partitions and _synchronized_partitions: Lists of OTDS partitions (for users and groups).
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory)
    - description (str, optional)
    - access_role (str, optional)
    - licenses (list, optional)
    """
    _partitions = []
    _synchronized_partitions = []

    """
    _licenses: Lists of OTDS Licenses to be added to a resource.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - path (str, mandatory)
    - product_name (str, mandatory)
    - resource (str, mandatory)
    - description (str, optional)
    """
    _licenses = []

    """
    _oauth_clients: List of OTDS OAuth Clients. Each element
    is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory)
    - description (str, optional, default = "")
    - confidential (bool, optional, default = True)
    - partition (str, optional, default = "Global")
    - redirect_urls (list, optional, default = [])
    - permission_scopes (list)
    - default_scopes (list)
    - allow_impersonation (bool, optional, default = True)
    - secret (str, optional, default = "") - option to provide a predefined secret
    """
    _oauth_clients = []

    """
    _oauth_handlers: List of OTDS OAuth handler. Each element
    is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory)
    - description (str, optional)
    - scope (str, optional, default = None)
    - type (str, mandatory) - handler type, like SAML, SAP, OAUTH
    - priority (int, optional)
    - active_by_default (bool, optional, default = False)
    - provider_name (str, mandatory for type = SAML and type = SAP)
    - auth_principal_attributes (list)
    - nameid_format (str, optional)
    - saml_url (str, mandatory for type = SAML)
    - otds_sp_endpoint (str, mandatory for type = SAML)
    - certificate_file (str, mandatory for type = SAP)
    - certificate_password (str, mandatory for type = SAP)
    - client_id (str, mandatory for type = OAUTH)
    - client_secret (str, mandatory for type = OAUTH)
    - authorization_endpoint (str, mandatory for type = OAUTH)
    - token_endpoint (str, optional for type = OAUTH)
    - scope_string (str, optional)
    """
    _auth_handlers = []

    """
    _trusted_sites: List of OTDS trasted sites.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - url (str, mandatory)
    """
    _trusted_sites = []

    """
    _system_attributes: List of OTDS System Attributes.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory)
    - value (str, mandatory)
    - description (str, optional)
    """
    _system_attributes = []

    """
    _system_attributes: List of OTPD Settings.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - description (str, optional)
    - name (str, mandatory)
    - value (str, mandatory)
    - tenant (str, optional)
    """
    _docgen_settings = []

    """
    _groups: List of groups.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory),
    - parent_groups (list, optional),
    - enable_o365 (bool, optional, default = False)
    - enable_salesforce (bool, optional, default = False)
    - enable_core_share (bool, optional, default = False)
    """
    _groups = []

    """
    _users: List of users.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory) (= login)
    - password (str, optional, will be generated if not provided)
    - firstname (str, optional, default = "")
    - lastname (str, optional, default = "")
    - title (str, optional, default = "")
    - email (str, optional, default = "")
    - base_group (str, optional, default = "DefaultGroup")
    - user_type (str, optional, default = "User") - possible values are "User" and "ServiceUser"
    - company (str, optional, default = "Innovate") - currently used for Salesforce users only
    - privileges (list, optional, default = ["Login", "Public Access"])
    - groups (list, optional)
    - favorites (list of str, optional)
    - security_clearance (int, optional)
    - supplemental_markings (list of str)
    - location (str, optional, default = "US") - only relevant for M365 users
    - enable_sap (bool, optional, default = False)
    - enable_successfactors (bool, optional, default = False)
    - enable_salesforce (bool, optional, default = False)
    - enable_o365 (bool, optional, default = False)
    - enable_core_share (bool, optional, default = False)
    - m365_skus (list of str) - only relevant for M365 users
    - extra_attributes (list of dict)
    """
    _users = []
    _user_customization = True

    """
    _admin_settings: list of admin settings (XML file to import).
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - description (str, optional)
    - filename (str, mandatory) - without path
    """
    _admin_settings = []
    _admin_settings_post = []

    """
    _exec_pod_commands: List of commands to be executed in the pods.
    list elements need to be dicts with pod name, command, etc.
    - enabled (bool, optional, default = True)
    - command (str, mandatory)
    - pod_name (str, mandatory)
    - description (str, optional)
    - interactive (bool, optional, default = False)
    """
    _exec_pod_commands = []

    """
    _exec_commands: List of commands to be executed in the customizer pod (as local process).
    Each list element need to be a dict with these keys:
    - enabled (bool, optional, default = True)
    - command (str, mandatory)
    - description (str, optional)
    """
    _exec_commands = []

    """
    _exec_database_commands: list of database command sets to be executed.
    Each list is a dict with these keys:
    - enabled (bool, optional, default = True)
    - db_connection (dict, mandatory) - supported dictionary keys:
        - db_name (str, mandatory) - name of the database
        - db_hostname (str, mandatory) - hostname of the database server
        - db_port (int, optional) - port to communicate to the database server; default is 5432
        - db_username (str, mandatory) - username
        - db_password (str, mandatory) - password
    - db_commands (list, mandatory) - each list item is a dictionary with these keys:
        - command (str, mandatory) - needs to have %s paceholder for each parameter
        - params (list, optional) - parameter values to be inserted into the %s postions in the command
    """
    _exec_database_commands = []

    """
    external_systems (list): List of external systems.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - external_system_type (str, mandatory) - possible values: SuccessFactors, SAP, Salesforce, Appworks Platform, Business Scenario Sample
    - external_system_name (str, mandatory) - for SAP this is the System ID
    - external_system_number (str)
    - description (str)
    - as_url (str, mandatory)
    - base_url (str, optional, default = "")
    - client (str, optional - only relevant for SAP, default = 100)
    - destination (str, optional - only relevant for SAP, default = "")
    - group (str, optional - only relevant for SAP, default = "PUBLIC")
    - username (str, optional - depends on external_system_type)
    - password (str, optional - depends on external_system_type)
    - certificate_file (str, optional - only relevant for SAP, used for Auth Handler)
    - certificate_password (str, optional - only relevant for SAP, used for Auth Handler)
    - external_system_hostname (str, mandatory - only relevant for SAP)
    - archive_logical_name (str, optional - only relevant for SAP)
    - archive_certificate_file (str, optional - only relevant for SAP)
    - oauth_client_id (str, optional)
    - oauth_client_secret (str, optional)
    - skip_connection_test (bool, optional, default = False)
    """
    _external_systems = []

    """
    _transport_packages (list): List of transport packages systems.
    Each list element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory)
    - url (str, mandatory)
    - description (str, optional, default = "")
    - replacements (list, optional, default = None)
    - extractions (list, optional, default = None)
    """
    _transport_packages = []
    _content_transport_packages = []
    _transport_packages_post = []

    # _business_object_types (list): Business object types are not in payload
    # but retrieved from transport package:
    _business_object_types = []

    # _workspace_types (list): Workspace types are not in payload but imported with transport package:
    _workspace_types = []

    """
    _workspace_templates (list): actually these are also imported via transport
    but used if we want to define standard members on template basis.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - type_name (str, mandatory)
    - template_name (str, mandatory)
    - members (list, optional)
      * role (str, mandatory)
      * users (list, optional, default = [])
      * groups (list, optional, default = [])
    - categories (list, optional)
      * nickname (str, optional) - the nickname of the category
      * path (list, optional) - the path to the category object in the category volume
      * inheritance (bool, optional) - should category inheritance be turned on on the workspace template node?
      * apply_to_sub_items (bool, optional) - should the category be inherited to existing sub-items?
    """
    _workspace_templates = []

    """
    _workspaces (list): list of Content Server business workspaces.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - id (str, mandatory) - logical ID of the workspace - used for cross-referencing inside the payload. This is NOT this node ID!
    - name (str, mandatory)
    - description (str, optional, default = "")
    - type_name (str, mandatory)
    - template_name (str, optional, default = just take first template)
    - business_objects (list, optional, default = [])
    - parent_id (str, optional, default = None) - this is a LOGICAL ID used in the payload - not the node ID!
    - parent_path (list, optional, default = None)
    - categories (list, optional, default = None)
      * name (str, mandatory)
      * set (str, default = "")
      * row (int, optional)
      * attribute (str, mandatory)
      * value (str, mandatory)
    - nickname (str, optional, default = ignore)
    - photo_nickname (str, optional, default = ignore)
    - rm_classification_path (list, optional, default = [])
    - classification_pathes (list of lists, optional, default = [])
    - members (list, optional, default = [])
      * role (name)
      * users (list, optional, default = [])
      * groups (list, optional, default = [])
    - relationships (list, optional, default = []) - list of related workspaces.
      The elements of the list can be:
      * string or integer with logical workspace ID
      * string with nickname of the related workspace
      * dictionaries with keys "type" and "name" of the related workspace
      * list of strings with the top-down path in the Enterprise volume
    """
    _workspaces = []

    """
    _sap_rfcs (list) and _sap_rfcs_post (list) include distinct lists of SAP RFC calls.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - description (str)
    - parameters (dict)
    - call_options (dict)
    """
    _sap_rfcs = []
    _sap_rfcs_post = []

    """
    _web_reports (list) and _web_reports_post (list) include all web report payload definitions.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - nickname (str, mandatory)
    - description (str, optional, default = "")
    - restart (bool, optional, default = False)
    - parameters (dict, optional, default = {}) - the dict keys are the parameter names and the dict values are the actual parameter values
    """
    _web_reports = []
    _web_reports_post = []

    """
    _cs_applications (list): List of Content Server Applications to deploy.
    Each list element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory)
    - descriptions (str, optional, default = "")
    """
    _cs_applications = []

    """
    _additional_group_members: List of memberships to establish.
    Each element is a dict with these keys:
    - parent_group (string)
    - user_name (string)
    - group_name (string)
    """
    _additional_group_members = []

    """
    _additional_access_role_members: List of memberships to establish.
    Each element is a dict with these keys:
    - access_role (string)
    - user_name (string)
    - group_name (string)
    - partition_name (string)
    """
    _additional_access_role_members = []

    """
    _additional_application_role_assignments: List of assignments to establish.
    Each element is a dict with these keys:
    - role (string)
    - user_name (string)
    - group_name (string)
    """
    _additional_application_role_assignments = []

    """
    _renamings (list). List of items to be renamed.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory)
    - nodeid (int, mandatory if no volume is specified) - this is the technical OTCS ID - typically only known for some preinstalled items
    - volume (int, mandatory if no nodeid is specified)
    - path (list, optional) - can be combined with volume - to specify a top-down path in the volume to the item to be renamed
    - nickname (str, optional) - the nickname of the node to rename - alternative to to volume/path or nodeid
    """
    _renamings = []

    """
    _items: List of items to create in Content Server
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - parent_nickname (str)
    - parent_path (list)
    - name (str)
    - description (str, optional, default = "")
    - type (str)
    - url (str) - "" means not set
    - original_nickname
    - original_path (list)
    """
    _items = []
    _items_post = []

    """
    _permissions: List of permissions changes to apply
    Each element is a dict with these keys:
    - path (list, optional)
    - volume (int, optional)
    - nickname (str, optional)
    - owner_permissions (list, optional)
    - owner_group_permissions (list, optional)
    - public_permissions (list, optional)
    - groups (list, optional)
      + name (str)
      + permissions (list)
    - users (list, optional)
      + name (str)
      + permissions (list)
    - apply_to (int)
    """
    _permissions = []
    _permissions_post = []

    """
    _workspace_permissions: List of workspace permissions changes to apply.
    Each element is a dict with these keys:
    - workspace_folder (str, optional)
    - workspace_type (str)
    - regex (bool, optional)
    - owner_permissions (list, optional)
    - owner_group_permissions (list, optional)
    - public_permissions (list, optional)
    - groups (list, optional)
      + name (str)
      + permissions (list)
    - users (list, optional)
      + name (str)
      + permissions (list)
    - apply_to (int)
    """
    _workspace_permissions = []

    """
    _assignments: List of assignments. Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - subject (str, mandatory)
    - instruction (str, optional)
    - workspace (str, optional if nickname is specified)
    - nickname (str, optional if workspace is specified)
    - groups (list)
    - users (list)
    """
    _assignments = []

    """
    _doc_generators: List of document generators that use the Document Template capabilities of Content Server.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - workspace_type (str, mandatory)
    - template_path (list, mandatory)
    - classification_path (list, mandatory)
    - category_name (str, optional, default = "")
    - workspace_folder_path (list, optional, default = []) - default puts the document in the workspace root
    - exec_as_user (str, optional, default = "")
    """
    _doc_generators = []

    """
    _workflows: List of workflow initiations inside workspace instances of a workspace type
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - worklow_nickname (str, mandatory) - the nickname of the workflow
    - initiate_as_user (str, mandatory) - user that initiates the workflow
    - workspace_type (str, mandatory) - for each instance of the given workspace type a workflow is started
    - workspace_folder_path (list, optional) - the subfolder that contains the document the workflow is started with
    - attributes (list, optional) - the list of attributes (name, value) the workflow is started with
    - steps (list)
      * action (Initiate, )
      * exec_as_user
      * attributes
        - name
        - value
        - type
    """
    _workflows = []

    """
    _browser_automations: List of browser automation for things that can only be
    automated via the web user interface. Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory)
    - description (str, optional)
    - base_url (str, mandatory)
    - user_name (str, optional)
    - password (str, optional)
    - wait_time (float, optional, default = 30.0) - wait time in seconds
    - wait_until (str, optional) - the page load / navigation `wait until` strategy. Possible values: `load`, `networkidle`, `domcontentloaded`
    - debug (bool, optional, default = False) - if True take screenshots and save to container
    - automations (list, mandatory)
      * dependent (bool, optional) - decide if current automation step is dependent on the previous step. Default is True. If dependent = True and previous step failed this step is skipped.
      * type (str, optional, default = "") - possible types: `login`, `get_page`, `click_elem`, `set_elem`, `check_elem`
      * page (str, optional, default = "") - the page-specific part of the URL. Will be concatenated with the `base_url`
      * selector (str, optional, default = "")
      * selector_type (str, optional, default = "id") - the find strategy - either `id`, `name`, `css`, `xpath`, `role`, `text`
      * role_type (str, optional, default = "") - the ARIA role of an element. Only relevant for selector_type = `role`.
      * scroll_to_element (bool, optional, default = true) - scroll to the element before clicking it - for type `click_elem` only. Should actually not be necessary as locators should scroll automatically.
      * value (str, optional, default = "") - the new value of element. Relevant for type = `set_elem`.
      * user_field (str, optional, default = "") - the name of the HTML field holding the user name - only for type `login`.
      * password_field (str, optional, default = "") - the name of the HTML field holding the password - only for type `login`.
      * iframe: name of the iframe if the elem is inside an iframe
      * press_enter: simulate pressing the "Enter" key after setting the elem value - for type set_elem only
      * typing (bool, optional) - deciding if to simulate keyboard input - this is required for many type-ahead fields to work - for type `set_elem` only
      * exact_match (bool, optional) - deciding if the element should be identified with an exact match
      * hover_only (bool, optional) - deciding if instead of clicking the element only a mouse over hovering should be simulated - for type `click_elem` only
      * regex (bool, optional) - deciding if the selector should be treated as a regular expression - for type `set_elem`
      * wait_until (str, optional, default = "") - an automation-step specific value for `wait_until` (see above)
      * wait_time (int, optional) - number of seconds to explicitly wait for an element to appear - for type `click_elem` only - for edge cases only
      * volume (int, optional, default = 141 (Enterprise Volume)) - the OTCS volume ID. Only relevant for type = `get_page`.
      * path (list), optional, default = []) - a top-down list of folder / workspace names. Only relevant for type = `get_page`.
      * navigation (bool, optional, default = False) - whether or not the click issues a nvigation event. Relevant only for type = `click_elem`.
      * popup_window (bool, optional) - deciding if the click will popup a new browser window - for type `click_elem` only
      * close_window (bool, optional) - deciding if the click will close the current window - for type `click_elem` only
      * checkbox_state (bool, optional, default = None) - defines the required state of a checkbox element (True = checked, False = unchecked)
      * attribute (str, optional, default = "") - the attribute name of an HTML element. Relevant only for type = `check_elem`.
      * substring (bool, optional, default = False) - with or not a string comparison should consider substrings. Relevant only for type = `check_elem`.
      * min_count (int, optional, default = 1) - defines how many elements should be found at a minimum by type = `check_elem`.
      * want_exist (bool, optional, default = True) - defines for type = `check_elem` if the existence or non-existence should be checked.
      * show_error (bool, optional) - deciding if an an error is logged
    """
    _browser_automations = []
    _browser_automations_post = []
    _test_automations = []

    """
    _security_clearances: List of Security Clearances. Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory)
    - level (str, mandatory)
    - description (str, optional, default = "")
    """
    _security_clearances = []

    """
    _supplemental_markings: List of supplemental markings. Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - code (str, mandatory)
    - description (str, optional, default = "")
    """
    _supplemental_markings = []

    _records_management_settings = []

    """
    _holds: List of Records Management holds. Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - name (str, mandatory)
    - type (str, mandatory)
    - group (str, optional)
    - comment (str, optional, default = "")
    - alternate_id (str, optional)
    - date_applied (str, optional, default = "")
    - date_to_remove (str, optional, default = "")
    """
    _holds = []

    """
    _bulk_datasources: list of bulk datasources.
    Each element is a dict with these keys:
    - data (Data object), this is not in payload but automatically filled
    - type (str, mandatory) - either excel, servicenow, otmm, otcs, pht, json, xml, nhc
    - csv_files (list, mandatory if type = csv)
    - csv_delimiter (str, optional) - value delimiter in the file - default is a comma
    - csv_header_index (int, optional) - if the file has a header line this parameter specifies the index
      (0 = first line, this is the default)
    - csv_column_names (list, optional) - if the file has no header line the column name can be
      specified as a list of strings
    - csv_use_columns (list, optional) - this list can either include integers (index of columns to keep), strings
      (names of columns to keep), or a list of boolean values (True = column is kept, False = column is dropped)
    - json_files (list, mandatory if type = json)
    - xml_files (list, optional, default = []) - only relevant for type = xml
    - xml_directories (list, optional, default = []) - only relevant for type = xml
    - xml_xpath (str, optional, default = None) - only relevant if xml_directories is set
    - xlsx_files (list, optional, default = [])
    - xlsx_sheets (list, optional, default = 0)
    - xlsx_columns (list, optional, default = None)
    - xlsx_skip_rows (int, optional, default = 0) - number of rows to skip on top of sheet
    - xlsx_na_values (list, optional, default = [])
    - pht_base_url (str, mandatory if type = pht)
    - pht_username (str, mandatory if type = pht)
    - pht_password (str, mandatory if type = pht)
    - pht_business_unit_exclusions (list, optional, default = [])
    - pht_business_unit_inclusions (list, optional, default = [])
    - pht_product_exclusions (list, optional, default = [])
    - pht_product_inclusions (list, optional, default = [])
    - pht_product_category_exclusions (list, optional, default = [])
    - pht_product_category_inclusions (list, optional, default = [])
    - pht_product_status_exclusions (list, optional, default = [])
    - pht_product_status_inclusions (list, optional, default = [])
    - pht_product_attributes (list, optional, default = []) - a list of attribute names that should be extracted and added as columns to the data frame
    - otmm_username (str, optional, default = "")
    - otmm_password (str, optional, default = "")
    - otmm_client_id (str, optional, default = None)
    - otmm_client_secret (str, optional, default = None)
    - otmm_thread_number (int, optional, default = BULK_THREAD_NUMBER)
    - otmm_download_dir (str, optional, default = "/data/mediaassets")
    - otmm_business_unit_exclusions (list, optional, default = [])
    - otmm_business_unit_inclusions (list, optional, default = [])
    - otmm_product_exclusions (list, optional, default = [])
    - otmm_product_inclusions (list, optional, default = [])
    - sn_base_url (str, mandatory if type = servicenow)
    - sn_auth_type (str, optional, default = "basic")
    - sn_username (str, optional, default = "")
    - sn_password (str, optional, default = "")
    - sn_client_id (str, optional, default = None)
    - sn_client_secret (str, optional, default = None)
    - sn_queries (list, mandatory if type = servicenow)
      * sn_table_name (str, mandatory) - name of the ServiceNow database table for the query
      * sn_query (str, mandatory) - query string
    - sn_thread_number (int, optional, default = BULK_THREAD_NUMBER)
    - sn_download_dir (str, optional, default = "/data/knowledgebase")
    - sn_skip_existing_downloads (bool, optional, default = True)
    - otcs_hostname (str, mandatory if type = otcs)
    - otcs_protocol (str, optional, default = "https")
    - otcs_port (str, optional, default = "443")
    - otcs_basepath (str, optional, default = "/cs/cs")
    - otcs_username (str, mandatory if type = otcs)
    - otcs_password (str, mandatory if type = otcs)
    - otcs_thread_number (int, optional, default = BULK_THREAD_NUMBER)
    - otcs_download_dir (str, optional, default = "/data/contentserver")
    - otcs_root_node_ids (list | int, mandatory if type = otcs)
    - otcs_include_workspaces (bool, optional, default = True)
    - otcs_include_items (bool, optional, default = True)
    - otcs_include_workspace_metadata (bool, optional, default = True)
    - otcs_include_item_metadata (bool, optional, default = True)
    - otcs_filter_workspace_depth (int, optional, default = 0)
    - otcs_filter_workspace_subtypes (int, optional, default = 0)
    - otcs_filter_workspace_category (str, optional, default = None) - name of the category the workspace needs to have
    - otcs_filter_workspace_attributes (dict | list, optional, default = None)
      * set (str, optional, default = None) - name of the attribute set
      * row (int, optional, default = None) - row number (starting with 1) - only required for multi-value sets
      * attribute (str, mandatory) - name of the attribute
      * value (str, mandatory) - value the attribute should have to pass the filter
    - otcs_filter_item_depth (int, optional, default = None)
    - otcs_filter_item_subtypes (int, optional, default = 0)
    - otcs_filter_item_category (str, optional, default = None) - name of the category the workspace needs to have
    - otcs_filter_item_attributes (dict | list, optional, default = None)
      * set (str, optional, default = None) - name of the attribute set
      * row (int, optional, default = None) - row number (starting with 1) - only required for multi-value sets
      * attribute (str, mandatory) - name of the attribute
      * value (str, mandatory) - value the attribute should have to pass the filter
    - cleansings (dict, optional, default = {}) - the keys of this dict are the field names! The values of the dict
      are sub-dicts with these keys:
      * upper (bool, optional, default = False)
      * lower (bool, optional, default = False)
      * capitalize (bool, optional, default = false) - first character upper case, rest lower-case
      * title (bool, optional, default = false) - first character of each word upper case
      * length (int, optional, default = None)
      * replacements (dict, optional, default = {}) - the keys are regular expressions and the values are
        replacement values
    - columns_to_drop (list, optional, default = [])
    - columns_to_keep (list, optional, default = [])
    - columns_to_add (list, optional, default = []) - elements are dicts with these keys:
      * source_column (str, mandatory)
      * name (str, mandatory)
      * reg_exp (str, optional, default = None)
      * prefix (str, optional, default = "")
      * suffix (str, optional, default = "")
      * length (int, optional, default = None)
      * group_chars (str, optional, default = None)
      * group_separator (str, optional, default =".")
    - columns_to_add_list (list, optional, default = []): add a new column with list values.
      Each payload item is a dictionary with these keys:
      * source_columns (str, mandatory) - names of the columns from which row values are taken from to create
        the list of string values
      * name (str, mandatory) - name of the new column
    - columns_to_add_concat (list, optional, default = []): add a new column with concatenated values.
      Each payload item is a dictionary with these keys:
      * source_columns (str, mandatory) - names of the columns from which row values are taken from to create
        the list of string values
      * name (str, mandatory) - name of the new column
      * concat_char (str, optional) - concatenation char e.g. "-", ".". Default is None = empty.
      * columns_to_add_concat_lower (bool,optional) - convert result to lower case
      * columns_to_add_concat_upper (bool,optional) - convert result to upper case
      * columns_to_add_concat_capitalize (bool,optional) - capitalize result
      * columns_to_add_concat_title (bool,optional) - convert result to title case
    - columns_to_add_table (list, optional, default = []): add a new column with table values.
      Each payload item is a dictionary with these keys:
      * source_columns (str, mandatory) - names of the columns from which row values are taken from to create a list
        of dictionary values. It is expected that the source columns already have list items or are strings with
        delimiter-separated values.
      * name (str, mandatory) - name of the new column
      * list_splitter (str, optional, default = ",")
    - conditions (list, optional, default = []) - each list item is a dict with these keys:
      * field (str, mandatory)
      * value (str | bool | list, optional, default = None)
      * equal (bool, optional): if True test for equality, if False test for non-equality
    - explosions (list, optional, default = []) - each list item is a dict with these keys:
      * explode_fields (str | list, mandatory)
      * flatten_fields (list, optional, default = [])
      * split_string_to_list (bool, optional, default = False)
      * list_splitter (str, optional, default = ",;") - string with characters that are used to split
        a string into list items.
    - name_column (str, optional, default = None)
    - synonyms_column (str, optional, default = None)
    """
    _bulk_datasources = []

    """
    _bulk_workspaces: List of bulk workspace definitions.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - type_name (str, mandatory) - type of the workspace
    - data_source (str, mandatory)
    - force_reload (bool, optional, default = True) - enforce a reload of the data source, e.g. useful if data source
      has been modified before by column operations or explosions
    - copy_data_source (bool, optional, default = False) - to avoid side-effects for repeative usage of the data source
    - operations (list, optional, default = ["create"]) - possible values: "create", "update", "delete", "recreate"
      (delete existing + create new)
    - update_operations (list, optional, default = ["name", "description", "categories", "nickname"]) - possible values:
      "name", "description", "categories", "nickname"
    - unique (list, optional, default = []) - list of fields (columns) that should be unique -> deduplication
    - sort (list, optional, default = []) - list of fields to sort the data frame by
    - name (str, mandatory) - name of the workspace - can include placeholder surrounded by {...}
    - name_alt (str, optional, default = None) - alternative name - can include placeholder surrounded by {...}
    - description (str, optional, default = "")
    - description_alt (str, optional, default = "") - alternative description (using different placeholders)
    - template_name (str, optional, default = take first template)
    - categories (list, optional, default = []) - each list item is a dict that may have these keys:
      * name (str, mandatory)
      * set (str, default = "")
      * row (int, optional)
      * attribute (str, mandatory)
      * value (str, optional if value_field is specified, default = None)
      * value_field (str, optional if value is specified, default = None) - can include placeholder surrounded by {...}
      * value_type (str, optional, default = "string") - possible values: "string", "date", "list" and "table".
        If list then string with comma-separated values will be converted to a list.
      * attribute_mapping (dict, optional, default = None) - only relevant for value_type = "table" -
        defines a mapping from the data frame column names to the category attribute names
      * value_mapping (dict, optional, default = None) - dictionary keys are the original values and dictionary values
        are the mapped values. This makes most sense for values with a limited / fixed domain of possible values
      * list_splitter (str, optional, default = ";,")
      * lookup_data_source (str, optional, default = None)
      * lookup_data_failure_drop (bool, optional, default = False) -
        should we clear / drop values that cannot be looked up?
      * is_key (bool,optional, default = False) - find document with old name. For this we expect a "key" value to be
        defined in the bulk workspace and one of the category / attribute item to be marked with "is_key" = True
    - workspaces (dict, dynamically bult up, default = {}) - list of already generated workspaces
    - external_create_date (str, optional, default = "")
    - external_modify_date (str, optional, default = "")
    - key (str, optional, default = None) - lookup key for workspaces other then the name
    - replacements (dict, optional, default = {}) - Each dictionary item has the field name as the dictionary key and a
      list of regular expressions as dictionary value
    - nickname (str, optional, default = None)
    - nickname_alt (str, optional, default = None) - alternative nickname
    - conditions (list, optional, default = [])
      * field (str, mandatory)
      * value (str | bool | list, optional, default = None)
    """
    _bulk_workspaces = []

    """
    _bulk_workspace_relationships: List of bulk workspace relationships.
    Each element is a dict with these keys:
    - enabled (bool, optional, default = True)
    - from_workspace (str, mandatory)
    - from_workspace_type (str, optional, default = None)
    - from_workspace_name (str, optional, default = None)
    - from_workspace_data_source (str, optional, default = None)
    - from_sub_workspace_name (str, optional, default = None) - if the related workspace is a sub-workspace
    - from_sub_workspace_path (list, optional, default = None) - the folder path under the main workspace where
      the sub-workspaces are located
    - to_workspace (str, mandatory)
    - to_workspace_type (str, optional, default = None)
    - to_workspace_name (str, optional, default = None)
    - to_workspace_data_source (str, optional, default = None)
    - to_sub_workspace_name (str, optional, default = None) - if the related workspace is a sub-workspace
    - to_sub_workspace_path (list, optional, default = None) - the folder path under the main workspace
      where the sub-workspaces are located
    - type (str, optional, default = "child") - type of the relationship (defines if the _from_ workspace
      is the parent or the child)
    - data_source (str, mandatory)
    - force_reload (bool, optional, default = True) - enforce a reload of the data source, e.g. useful if data source
      has been modified before by column operations or explosions
    - copy_data_source (bool, optional, default = False) - to avoid side-effects for repeative usage of the data source
    - explosions (list, optional, default = []) - each list item is a dict with these keys:
      * explode_fields (str | list, mandatory)
      * flatten_fields (list, optional, default = [])
      * split_string_to_list (bool, optional, default = False)
      * list_splitter (str, optional, default = ",;") - string with characters that are used to split
        a string into list items.
    - unique (list, optional, default = [])
    - sort (list, optional, default = [])
    - thread_number (int, optional, default = BULK_THREAD_NUMBER)
    - replacements (list, optional, default = None)
    - conditions (list, optional, default = None)
      * field (str, mandatory)
      * value (str | bool | list, optional, default = None)
    - from_workspace_lookup_error (bool, optional, default = True) - whether or not an error should be logged
      if a relationship endpoint cannot be looked up
    - to_workspace_lookup_error (bool, optional, default = True) - whether or not an error should be logged
      if a relationship endpoint cannot be looked up
    """
    _bulk_workspace_relationships = []

    """
    _bulk_documents (list): List of bulk document payload. Each element
    is a dict with these keys:
    - enabled (bool, optional, default = True)
    - data_source (str, mandatory)
    - force_reload (bool, optional, default = True) - enforce a reload of the data source, e.g. useful if data source
      has been modified before by column operations or explosions
    - copy_data_source (bool, optional, default = False) - to avoid side-effects for repeative usage of the data source
    - explosions (list of dicts, optional, default = [])
      * explode_fields (str | list, mandatory)
      * flatten_fields (list, optional, default = [])
      * split_string_to_list (bool, optional, default = False)
      * list_splitter (str, optional, default = ",;") - string with characters that are used to split a
        string into list items.
    - unique (list, optional, default = []) - list of column names which values should be unique -> deduplication
    - sort (list, optional, default = []) - list of fields to sort the data frame by
    - operations (list, optional, default = ["create"])
    - update_operations (list, optional, default = ["name", "description", "categories", "nickname", "version"]) - possible values:
      "name", "description", "categories", "nickname", "version", "purge"
    - name (str, mandatory) - can include placeholder surrounded by {...}
    - name_alt (str, optional, default = None) - can include placeholder surrounded by {...}
    - name_regex (str, optional, default = r"") - regex replacement for document names. The pattern and replacement are separated by pipe character |
    - description (str, optional, default = None) - can include placeholder surrounded by {...}
    - download_name (str, optional, default = name) - can include placeholder surrounded by {...}
    - download_name_wildcards (bool, optional, default = False) - defines if the download name includes wildcards,
      e.g. "*.pdf"
    - nickname (str, optional, default = None) - can include placeholder surrounded by {...}
    - download_url (str, optional, default = None)
    - download_url_alt (str, optional, default = None)
    - download_dir (str, optional, default = BULK_DOCUMENT_PATH)
    - delete_download (bool, optional, default = True)
    - file_extension (str, optional, default = "")
    - file_extension_alt (str, optional, default = "html")
    - mime_type (str, optional, default = "application/pdf")
    - mime_type_alt (str, optional, default = "text/html")
    - nickname (str, optional, default = None)
    - categories (list, optional, default = [])
      * name (str, mandatory)
      * set (str, default = "")
      * row (int, optional)
      * attribute (str, mandatory)
      * value (str, optional if value_field is specified, default = None)
      * value_field (str, optional if value is specified, default = None) - can include placeholder surrounded by {...}
      * value_type (str, optional, default = "string") - possible values: "string", "date", "list" and "table".
        If list then string with comma-separated values will be converted to a list.
      * attribute_mapping (dict, optional, default = None) - only relevant for value_type = "table" - defines a mapping
        from the data frame column names to the category attribute names
      * list_splitter (str, optional, default = ";,")
      * lookup_data_source (str, optional, default = None)
      * lookup_data_failure_drop (bool, optional, default = False) -
        should we clear / drop values that cannot be looked up?
      * is_key (bool, optional, default = False) - find document is old name. For this we expect a "key" value
        to be defined for the bulk document and one of the category / attribute item to be marked with "is_key" = True
    - thread_number (int, optional, default = BULK_THREAD_NUMBER)
    - external_create_date (str, optional, default = "")
    - external_modify_date (str, optional, default = "")
    - key (str, optional, default = None) - lookup key for documents other then the name
    - download_wait_time (int, optional, default = 30)
    - download_retries (int, optional, default = 2)
    - replacements (list, optional, default = [])
    - conditions (list, optional, default = []) - all conditions must evaluate to true
      * field (str, mandatory)
      * value (str | bool | list, optional, default = None)
    - workspaces (list, optional, default = [])
      * workspace_name (str, mandatory)
      * conditions (list, optional, default = [])
        + field (str, mandatory)
        + value (str | bool | list, optional, default = None)
      * workspace_type (str, mandatory)
      * data_source (str, optional, default = None)
      * workspace_folder (str, optional, default = "")
      * workspace_path (list, optional, default = [])
      * sub_workspace_type (str, optional, default = "")
      * sub_workspace_name (str, optional, default = "")
      * sub_workspace_template (str, optional, default = "")
      * sub_workspace_folder (str, optional, default = "")
      * sub_workspace_path (list, optional, default = [])
    """
    _bulk_documents = []

    """
    _bulk_items (list): List of bulk items payload. Each element
    is a dict with these keys:
    - enabled (bool, optional, default = True)
    - data_source (str, mandatory)
    - force_reload (bool, optional, default = True) - enforce a reload of the data source, e.g. useful if data source
      has been modified before by column operations or explosions
    - copy_data_source (bool, optional, default = False) - to avoid side-effects for repeative usage of the data source
    - explosions (list of dicts, optional, default = [])
      * explode_fields (str | list, mandatory)
      * flatten_fields (list, optional, default = [])
      * split_string_to_list (bool, optional, default = False)
      * list_splitter (str, optional, default = ",;") - string with characters that are used to split a
        string into list items.
    - unique (list, optional, default = []) - list of column names which values should be unique -> deduplication
    - sort (list, optional, default = []) - list of fields to sort the data frame by
    - operations (list, optional, default = ["create"])
    - update_operations (list, optional, default = ["name", "description", "categories", "nickname"]) - possible values:
      "name", "description", "categories", "nickname", "url"
    - name (str, mandatory) - can include placeholder surrounded by {...}
    - name_alt (str, optional, default = None) - can include placeholder surrounded by {...}
    - name_regex (str, optional, default = r"") - regex replacement for document names. The pattern and replacement are separated by pipe character |
    - description (str, optional, default = None) - can include placeholder surrounded by {...}
    - nickname (str, optional, default = None) - can include placeholder surrounded by {...}
    - nickname (str, optional, default = None)
    - categories (list, optional, default = [])
      * name (str, mandatory)
      * set (str, default = "")
      * row (int, optional)
      * attribute (str, mandatory)
      * value (str, optional if value_field is specified, default = None)
      * value_field (str, optional if value is specified, default = None) - can include placeholder surrounded by {...}
      * value_type (str, optional, default = "string") - possible values: "string", "date", "list" and "table".
        If list then string with comma-separated values will be converted to a list.
      * attribute_mapping (dict, optional, default = None) - only relevant for value_type = "table" - defines a mapping
        from the data frame column names to the category attribute names
      * list_splitter (str, optional, default = ";,")
      * lookup_data_source (str, optional, default = None)
      * lookup_data_failure_drop (bool, optional, default = False) -
        should we clear / drop values that cannot be looked up?
      * is_key (bool, optional, default = False) - find document is old name. For this we expect a "key" value
        to be defined for the bulk document and one of the category / attribute item to be marked with "is_key" = True
    - thread_number (int, optional, default = BULK_THREAD_NUMBER)
    - external_create_date (str, optional, default = "")
    - external_modify_date (str, optional, default = "")
    - key (str, optional, default = None) - lookup key for documents other then the name
    - replacements (list, optional, default = [])
    - conditions (list, optional, default = []) - all conditions must evaluate to true
      * field (str, mandatory)
      * value (str | bool | list, optional, default = None)
    - workspaces (list, optional, default = [])
      * workspace_name (str, mandatory)
      * conditions (list, optional, default = [])
        + field (str, mandatory)
        + value (str | bool | list, optional, default = None)
      * workspace_type (str, mandatory)
      * data_source (str, optional, default = None)
      * workspace_folder (str, optional, default = "")
      * workspace_path (list, optional, default = [])
      * sub_workspace_type (str, optional, default = "")
      * sub_workspace_name (str, optional, default = "")
      * sub_workspace_template (str, optional, default = "")
      * sub_workspace_folder (str, optional, default = "")
      * sub_workspace_path (list, optional, default = [])
    """
    _bulk_items = []

    _bulk_classifications = []

    _nifi_flows = []

    _placeholder_values = {}

    # Link to the method in customizer.py to restart the Content Server pods.
    _otcs_restart_callback: Callable

    # Link to the method in customizer.py to print a log header (separator line)
    _log_header_callback: Callable

    _aviator_enabled = False

    _transport_extractions: list = []
    _transport_replacements: list = []
    _appworks_configurations = []

    _avts_repositories: list = []

    _embeddings: list = []

    # Disable Status files
    upload_status_files: bool = True

    def __init__(
        self,
        payload_source: str,
        custom_settings_dir: str,
        k8s_object: K8s | None,
        otds_object: OTDS,
        otac_object: OTAC | None,
        otcs_backend_object: OTCS,
        otcs_frontend_object: OTCS,
        otcs_restart_callback: Callable,
        otiv_object: OTIV | None,
        otpd_object: OTPD | None,
        m365_object: M365 | None,
        core_share_object: CoreShare | None,
        placeholder_values: dict,
        log_header_callback: Callable,
        browser_headless: bool = True,
        stop_on_error: bool = False,
        aviator_enabled: bool = False,
        upload_status_files: bool = True,
        otawp_object: OTAWP | None = None,
        otca_object: OTCA | None = None,
        otkd_object: OTKD | None = None,
        avts_object: AVTS | None = None,
        logger: logging.Logger = default_logger,
    ) -> None:
        """Initialize the Payload object.

        Args:
            payload_source (str):
                The path or URL to payload source file.
            custom_settings_dir (str):
                Provide a custom settings directory.
            k8s_object (K8s | None):
                The Kubernetes object.
                Pass None if deployment is not running in Kubernetes.
            otds_object (OTDS):
                The OTDS object. This is mandatory.
            otac_object (OTAC | None):
                The optional OTAC object (Archive Center).
                Pass None if Archive Center is not part of the deployment.
            otcs_backend_object (OTCS):
                The OTCS backend object.
            otcs_frontend_object (OTCS):
                The OTCS frontend object.
            otcs_restart_callback (Callable):
                A function to call if OTCS service needs a restart.
            otiv_object (OTIV | None):
                The OTIV object (Intelligent Viewing).
                Pass None if Intelligent Viewing is not part of the deployment.
            otpd_object (OTPD | None):
                The OTPD object (PowerDocs).
                Pass None if PowerDocs is not part of the deployment.
            m365_object (object):
                The M365 object to talk to Microsoft Graph API.
            core_share_object (CoreShare | None):
                The Core Share object.
            placeholder_values (dict):
                A dictionary of placeholder values to be replaced in admin settings.
            log_header_callback:
                Method to print a section break / header line into the log.
            browser_headless (bool):
                If true, the Browser for the Automation will be started in Headless mode (default)
            stop_on_error (bool):
                This flag controls if transport deployment should stop
                if a transport deployment in OTCS fails.
            aviator_enabled (bool):
                Flag that indicates whether or not the Content Aviator is enabled.
            upload_status_files (bool, optional):
                Whether or not status file should be uploaded to the peronal workspace
                of the admin user in Content Server.
            otawp_object (OTAWP):
                An optional AppWorks Platform object.
            otca_object (OTCA):
                An optional Content Aviator object.
            otkd_object (OTKD):
                An optional Knowledge Discovery object.
            avts_object (AVTS):
                An optional Aviator Search object.
            logger (logging.Logger, optional):
                The logging object to use for all log messages. Defaults to default_logger.

        """

        self.logger = logger.getChild("payload")
        for logfilter in logger.filters:
            self.logger.addFilter(logfilter)

        self._stop_on_error = stop_on_error
        self._payload_source = payload_source
        self._k8s = k8s_object
        self._otds = otds_object
        self._otac = otac_object
        self._otcs = otcs_backend_object
        self._otcs_backend = otcs_backend_object
        self._otcs_frontend = otcs_frontend_object
        self._otiv = otiv_object
        self._otpd = otpd_object
        self._m365 = m365_object
        self._core_share = core_share_object
        # The SAP, SuccessFactors and Salesforce objects only exists after external systems have been processed
        self._sap = None
        self._successfactors = None
        self._salesforce = None
        self._servicenow = None
        self._guidewire_policy_center = None
        self._guidewire_claims_center = None
        self._otmm = None
        self._otcs_source = None
        self._pht = None  # the OpenText prodcut hierarchy
        self._nhc = None  # National Hurricane Center
        self._otca = otca_object  # Content Aviator
        self._otkd = otkd_object  # Knowledge Discovery
        self._avts = avts_object  # Aviator Search
        self._browser_headless = browser_headless
        self._custom_settings_dir = custom_settings_dir
        self._placeholder_values = placeholder_values
        self._otcs_restart_callback = otcs_restart_callback
        self._log_header_callback = log_header_callback
        self._aviator_enabled = aviator_enabled
        self._http_object = HTTP(logger=self.logger)
        self.upload_status_files = upload_status_files
        self._otawp = otawp_object

    # end method definition

    def thread_wrapper(self, target: Callable, *args: tuple, **kwargs: dict) -> None:
        """Wrap around threads to catch exceptions during exection.

        Args:
            target (callable):
                The method (callable) the Thread should run.
            args (tuple):
                The arguments for the method.
            kwargs (dict):
                Keyword arguments for the method.

        """

        try:
            target(*args, **kwargs)
        except Exception:
            thread_name = threading.current_thread().name
            self.logger.error(
                "Thread '%s' failed!",
                thread_name,
            )
            self.logger.error(traceback.format_exc())

    # end method definition

    def replace_placeholders(self, content: str) -> str:
        """Replace placeholders in file content.

        The content of the file is provided via a parameter.
        The replacements are defined in a object variable
        _placeholder_values (type = dictionary)
        The placeholder values are supposed to be surrounded by
        double % signs like %%OTAWP_RESOURCE_ID%%

        Args:
            content (str):
                The file content to replace placeholders in.

        Returns:
            str:
                Updated content with all defined replacements.

        """
        # https://stackoverflow.com/questions/63502218/replacing-placeholders-in-a-text-file-with-python

        # if no placeholders are defined we can return the
        # initial value:
        if not self._placeholder_values:
            return content

        try:
            # We do a dynamic replacement here. The replacement is calculated
            # by the lambda function that is basically a lookup of the replacement
            # key found in the settings file with the value defined in the list
            # of replacement values in self._placeholder_values
            return re.sub(
                r"%%(\w+?)%%",
                lambda match: self._placeholder_values[match.group(1)],
                content,
            )
        except KeyError:
            self.logger.error(
                "Found placeholder in settings file without a defined value!",
            )
            return content
        except re.error:
            self.logger.error("Regex substitution error!")
            return content

        # end method definition

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_payload")
    def init_payload(self) -> dict | None:
        """Read the YAML or Terraform HCL payload file.

        Args:
            None

        Returns:
            dict | None:
                The payload as a Python dict. Elements are the different payload sections.
                None in case the file couldn't be found or read.

        """

        self._payload = load_payload(self._payload_source, self.logger)
        if not self._payload:
            return None

        self._payload_sections = self._payload.get("payloadSections", None)
        self._payload_options = self._payload.get("payloadOptions", None)

        # Retrieve all the payload sections and store them in lists
        # (these are object variables that we want to initialize
        # even if payload is empty):
        self._webhooks = self.get_payload_section("webHooks")
        self._webhooks_post = self.get_payload_section("webHooksPost")
        self._resources = self.get_payload_section("resources")
        self._partitions = self.get_payload_section("partitions")
        self._licenses = self.get_payload_section("licenses")
        self._synchronized_partitions = self.get_payload_section(
            "synchronizedPartitions",
        )
        self._oauth_clients = self.get_payload_section("oauthClients")
        self._application_roles = self.get_payload_section("applicationRoles")
        self._auth_handlers = self.get_payload_section("authHandlers")
        self._trusted_sites = self.get_payload_section("trustedSites")
        self._system_attributes = self.get_payload_section("systemAttributes")
        self._docgen_settings = self.get_payload_section("docgenSettings")
        self._groups = self.get_payload_section("groups")
        self._users = self.get_payload_section("users")
        if self._users:
            # Check if multiple user instances should be created
            self.init_payload_user_instances()
        self._admin_settings = self.get_payload_section("adminSettings")
        self._admin_settings_post = self.get_payload_section("adminSettingsPost")
        self._exec_pod_commands = self.get_payload_section("execPodCommands")
        self._exec_commands = self.get_payload_section("execCommands")
        self._kubernetes = self.get_payload_section("kubernetes")
        self._exec_database_commands = self.get_payload_section("execDatabaseCommands")
        self._external_systems = self.get_payload_section("externalSystems")
        self._transport_packages = self.get_payload_section("transportPackages")
        self._content_transport_packages = self.get_payload_section(
            "contentTransportPackages",
        )
        self._transport_packages_post = self.get_payload_section(
            "transportPackagesPost",
        )
        self._workspace_templates = self.get_payload_section("workspaceTemplates")
        self._workspaces = self.get_payload_section("workspaces")
        self._bulk_datasources = self.get_payload_section("bulkDatasources")
        self._bulk_workspaces = self.get_payload_section("bulkWorkspaces")
        self._bulk_workspace_relationships = self.get_payload_section(
            "bulkWorkspaceRelationships",
        )
        self._bulk_documents = self.get_payload_section("bulkDocuments")
        self._bulk_items = self.get_payload_section("bulkItems")
        self._bulk_classifications = self.get_payload_section("bulkClassifications")
        self._sap_rfcs = self.get_payload_section("sapRFCs")
        self._sap_rfcs_post = self.get_payload_section("sapRFCsPost")
        self._web_reports = self.get_payload_section("webReports")
        self._web_reports_post = self.get_payload_section("webReportsPost")
        self._cs_applications = self.get_payload_section("csApplications")
        self._additional_group_members = self.get_payload_section(
            "additionalGroupMemberships",
        )
        self._additional_access_role_members = self.get_payload_section(
            "additionalAccessRoleMemberships",
        )
        self._additional_application_role_assignments = self.get_payload_section(
            "additionalApplicationRoleAssignments",
        )
        self._renamings = self.get_payload_section("renamings")
        self._items = self.get_payload_section("items")
        self._items_post = self.get_payload_section("itemsPost")
        self._permissions = self.get_payload_section("permissions")
        self._permissions_post = self.get_payload_section("permissionsPost")
        self._workspace_permissions = self.get_payload_section("workspacePermissions")
        self._assignments = self.get_payload_section("assignments")
        self._security_clearances = self.get_payload_section("securityClearances")
        self._supplemental_markings = self.get_payload_section("supplementalMarkings")
        self._records_management_settings = self.get_payload_section(
            "recordsManagementSettings",
        )
        self._holds = self.get_payload_section("holds")
        self._doc_generators = self.get_payload_section("documentGenerators")
        self._workflows = self.get_payload_section("workflows")
        self._browser_automations = self.get_payload_section("browserAutomations")
        self._browser_automations_post = self.get_payload_section(
            "browserAutomationsPost",
        )
        self._test_automations = self.get_payload_section("testAutomations")
        self._appworks_configurations = self.get_payload_section("appworks")
        self._avts_repositories = self.get_payload_section("avtsRepositories")
        self._avts_questions = self.get_payload_section("avtsQuestions")
        self._embeddings = self.get_payload_section("embeddings")
        self._nifi_flows = self.get_payload_section("nifi")

        return self._payload

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_payload_user_instances")
    def init_payload_user_instances(self) -> None:
        """Read setting for Multiple User instances."""

        for dic in self._payload_sections:
            if dic.get("name") == "users":
                users_payload = dic
                break
        user_instances = users_payload.get("additional_instances", 0)

        if user_instances == 0:
            self.logger.info("No additional user instances configured (default = 0).")
            return

        i = 0

        original_users = copy.deepcopy(self._users)
        while i <= user_instances:
            for user in copy.deepcopy(original_users):
                user["name"] = user["name"] + "-" + str(i).zfill(2)
                user["lastname"] = user["lastname"] + " " + str(i).zfill(2)
                user["enable_sap"] = False
                user["enable_o365"] = False
                user["enable_core_share"] = False
                user["enable_salesforce"] = False
                user["enable_successfactors"] = False

                self.logger.info(
                    "Creating additional user instance -> '%s'",
                    user["name"],
                )
                self.logger.debug("Create user instance -> %s", user)
                self._users.append(user)

            i = i + 1

        return

    # end method definition

    def get_payload_section(self, payload_section_name: str) -> list:
        """Get a specific section of the payload based on its name.

        The section is delivered as a list of settings.
        It deliveres an empty list if this payload section is disabled by the corresponding
        payload switch (this is read from the payloadSections dictionary of the payload)

        Args:
            payload_section_name (str):
                The name of the dict element in the payload structure.

        Returns:
            list:
                The section of the payload as a Python list. Empty list if section does not exist
                or section is disabled by the corresponding payload switch.

        """

        if not isinstance(self._payload, dict):
            return []

        # if the secton is not in the payload we return an empty list:
        if not self._payload.get(payload_section_name):
            return []

        # Return an empty list if the payload section does not exist or is disabled:
        sections = self._payload.get("payloadSections")
        if sections:
            section = next(
                (item for item in sections if item["name"] == payload_section_name),
                None,
            )
            if not section or not section.get("enabled", False):
                return []

        return self._payload[payload_section_name]

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_appworks_configurations")
    def process_appworks_configurations(self, section_name: str = "appworks") -> bool:
        """Process the configurations for AppWorks projects.

        This method is responsible for setting up the necessary configurations for AppWorks projects.
        If the payload contains a `appworks` section, it will execute the corresponding actions
        to process and apply the custom configuration.

        This includes:
        * Resource configuration
            - add the OTCS user partition to the OTDS access role for the AppWorks organization
            - add the OTCS admin partition to the OTDS access role for the AppWorks organization
            - add an OTAWP license
        * Solution configuration
            - create the AppWorks artifacts

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._appworks_configurations:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        #
        # 1: Loop to create the required OTDS resources for the AppWorks organization:
        #
        for appworks_configuration in self._appworks_configurations:
            organization = appworks_configuration.get("organization", None)
            if not organization:
                self.logger.error("AppWorks configuration is missing the organization name! Skipping...")
                success = False
                continue

            self._log_header_callback(
                text="Process AppWorks resource configuration for '{}'".format(organization), char="-"
            )

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not appworks_configuration.get("enabled", True):
                self.logger.info(
                    "Payload for AppWorks configuration -> '%s' is disabled. Skipping...",
                    organization,
                )
                continue

            if not appworks_configuration.get("resource_config", False):
                self.logger.info("AppWorks resource configuration is disabled for -> '%s'. Skipping...", organization)
                continue

            access_role_name = "Access to " + organization

            # make sure code is idempotent and only try to add ressource if it doesn't exist already:
            awp_resource = self._otds.get_resource(organization)
            if not awp_resource:
                self.logger.info(
                    "OTDS resource -> '%s' for AppWorks Platform does not yet exist. Creating...",
                    organization,
                )
                awp_resource = self._otds.add_resource(
                    name=organization,
                    description="AppWorks Platform - {}".format(organization),
                    display_name="AppWorks Platform - {}".format(organization),
                    additional_payload=OTAWP.resource_payload(
                        org_name=organization,
                        username=self._otawp.username(),
                        password=self._otawp.password(),
                    ),
                )
            else:
                self.logger.info(
                    "OTDS resource -> '%s' for AppWorks Platform does already exist.",
                    organization,
                )

            awp_resource_id = awp_resource["resourceID"]

            self.logger.info(
                "AppWorks Platform organization -> '%s' got OTDS resource ID -> %s",
                organization,
                awp_resource_id,
            )

            # Loop to wait for OTCS to create its OTDS user partition:
            otcs_partition = self._otds.get_partition(
                self._otcs.partition_name(),
                show_error=False,
            )
            while otcs_partition is None:
                self.logger.warning(
                    "OTDS user partition -> '%s' for Content Server does not exist yet. Waiting...",
                    self._otcs.partition_name(),
                )

                time.sleep(30)
                otcs_partition = self._otds.get_partition(
                    name=self._otcs.partition_name(),
                    show_error=False,
                )

            # Add the OTDS user partition for OTCS to the AppWorks Platform Access Role in OTDS.
            # This will effectvely sync all OTCS users with AppWorks Platform:
            self._otds.add_partition_to_access_role(
                access_role=access_role_name,
                partition=self._otcs.partition_name(),
            )

            # Add the OTDS admin partition to the AppWorks Platform Access Role in OTDS.
            self._otds.add_partition_to_access_role(
                access_role=access_role_name,
                partition=self._otds.admin_partition_name(),
            )

            # Set Group inclusion for Access Role for OTAWP to "True":
            self._otds.update_access_role_attributes(
                name=access_role_name,
                attribute_list=[{"name": "pushAllGroups", "values": ["True"]}],
            )

            # Add ResourceID User to 'otdsadmins' group to allow push
            self._otds.add_user_to_group(
                user=str(awp_resource_id) + "@otds.admin",
                group="otdsadmins@otds.admin",
            )

            # Allow impersonation for all users:
            self._otds.impersonate_resource(resource_name=organization)

            # Editing configmap
            config_map = self._k8s.get_config_map(config_map_name=self._otawp.config_map_name())
            if not config_map:
                self.logger.error(
                    "Failed to retrieve AppWorks Kubernetes config map -> '%s'!",
                    self._otawp.config_map_name(),
                )
                success = False
            else:
                self.logger.info(
                    "Update Kubernetes config map for AppWorks organization -> '%s' with OTDS resource IDs...",
                    organization,
                )
                solution = yaml.safe_load(
                    config_map.data[organization + ".yaml"],
                )

                solution["platform"]["organizations"][organization]["otds"]["resourceId"] = awp_resource_id
                solution["platform"]["organizations"][organization]["database"]["connections"]["sysConnection"][
                    "connectionString"
                ] = "jdbc:postgresql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}"

                solution["platform"]["organizations"][organization]["database"]["connections"]["sysConnection"][
                    "database"
                ] = "${DATABASE_NAME}"

                solution["platform"]["organizations"][organization]["database"]["connections"]["sysConnection"][
                    "password"
                ] = "${DATABASE_PASSWORD}"

                solution["platform"]["organizations"][organization]["content"]["ContentServer"]["contentServerUrl"] = (
                    self._otcs.cs_url()
                )
                solution["platform"]["organizations"][organization]["content"]["ContentServer"][
                    "contentServerSupportDirectoryUrl"
                ] = self._otcs.cs_support_url()
                solution["platform"]["organizations"][organization]["content"]["ContentServer"]["otdsResourceId"] = (
                    self._otcs.resource_id()
                )
                solution["platform"]["organizations"][organization]["authenticators"]["OTDSAuth"]["publicLoginUrl"] = (
                    self._otds.base_url() + "/otdsws/login"
                )

                config_map.data[organization + ".yaml"] = yaml.dump(solution)
                result = self._k8s.replace_config_map(
                    config_map_name=self._otawp.config_map_name(),
                    config_map_data=config_map.data,
                )
                if result:
                    self.logger.info(
                        "Successfully updated AppWorks solution YAML for organization -> '%s'.",
                        organization,
                    )
                else:
                    self.logger.error(
                        "Failed to update AppWorks solution YAML for organization -> '%s'!",
                        organization,
                    )
                self.logger.debug(
                    "Solution YAML for Appworks organization -> '%s': %s",
                    organization,
                    solution,
                )
            # Add SPS license for OTAWP
            license_name = self._otawp.product_name()
            product_name = self._otawp.product_name() + "_" + organization.upper()
            product_description = self._otawp.product_name() + organization
            if os.path.isfile(self._otawp.license_file()):
                self.logger.info(
                    "Found OTAWP license file -> '%s', assiging it to OTDS resource -> '%s'...",
                    self._otawp.license_file(),
                    organization,
                )

                otawp_license = self._otds.add_license_to_resource(
                    path_to_license_file=self._otawp.license_file(),
                    product_name=product_name,
                    product_description=product_description,
                    resource_id=awp_resource["resourceID"],
                )
                if not otawp_license:
                    self.logger.error(
                        "Couldn't apply license -> '%s' for product -> '%s' to OTDS resource -> '%s'!",
                        self._otawp.license_file(),
                        product_name,
                        awp_resource["resourceID"],
                    )
                else:
                    self.logger.info(
                        "Successfully applied license -> '%s' for product -> '%s' to OTDS resource -> '%s'.",
                        self._otawp.license_file(),
                        product_name,
                        awp_resource["resourceID"],
                    )

                # Assign AppWorks license to Content Server Members Partiton and otds.admin:
                for partition_name in ["otds.admin", self._otcs.partition_name()]:
                    if self._otds.is_partition_licensed(
                        partition_name=partition_name,
                        resource_id=awp_resource["resourceID"],
                        license_feature="USERS",
                        license_name=license_name,
                    ):
                        self.logger.info(
                            "Partition -> '%s' is already licensed for -> '%s' (%s).",
                            partition_name,
                            product_name,
                            "USERS",
                        )
                    else:
                        assigned_license = self._otds.assign_partition_to_license(
                            partition_name=partition_name,
                            resource_id=awp_resource["resourceID"],
                            license_feature="USERS",
                            license_name=license_name,
                        )
                        if not assigned_license:
                            self.logger.error(
                                "Partition -> '%s' could not be assigned to license -> '%s' (%s)!",
                                partition_name,
                                product_name,
                                "USERS",
                            )
                            success = False
                        else:
                            self.logger.info(
                                "Partition -> '%s' successfully assigned to license -> '%s' (%s).",
                                partition_name,
                                product_name,
                                "USERS",
                            )
                # end for partition_name in ["otds.admin", self._otcs.partition_name()]:
            # end if os.path.isfile(self._otawp.license_file()):

            self.logger.info("Restart AppWorks Kubernetes stateful set -> '%s'...", self._otawp.hostname())

            self._k8s.restart_stateful_set(sts_name=self._otawp.hostname(), force=True, wait=True)

            self._otawp.set_organization(organization)
            otawp_cookie = self._otawp.authenticate(revalidate=True)
            if not otawp_cookie:
                self.logger.error(
                    "Authentication at AppWorks failed! Cannot proceed with processing of AppWorks configuration -> '%s'!",
                    organization,
                )
                success = False
                continue
            self._otawp.create_cws_config(
                partition=self._otcs.partition_name(), resource_name=organization, otcs_url=self._otcs.cs_url()
            )
            self._otawp.assign_role_to_user(
                organization=organization, user_name=self._otawp.username(), role_name="Developer"
            )
        # end for appworks_configuration in self._appworks_configurations:

        #
        # 2: Loop to create the AppWorks workspaces, projects, and entities:
        #
        for appworks_configuration in self._appworks_configurations:
            organization = appworks_configuration.get("organization", None)
            if not organization:
                self.logger.error("AppWorks configuration is missing the organization name! Skipping...")
                success = False
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not appworks_configuration.get("enabled", True):
                self.logger.info(
                    "Payload for AppWorks configuration -> '%s' is disabled. Skipping...",
                    organization,
                )
                continue

            self._log_header_callback(
                text="Process AppWorks workspaces, project, entities for '{}'".format(organization), char="-"
            )

            self._otawp.set_organization(organization)
            otawp_cookie = self._otawp.authenticate(revalidate=True)
            if not otawp_cookie:
                self.logger.error(
                    "Authentication at AppWorks failed. Cannot proceed with processing of AppWorks configutation -> '%s'!",
                    organization,
                )
                success = False
                continue

            if "workspaces" not in appworks_configuration:
                self.logger.warning(
                    "No workspace information in AppWorks configuration -> '%s'. Skipping...", organization
                )
                continue

            for workspace in appworks_configuration["workspaces"]:
                workspace_id = workspace.get("workspace_id", None)
                workspace_name = workspace.get("name", None)
                workspace_path = workspace.get("path", None)
                if not workspace_id or not workspace_name or not workspace_path:
                    self.logger.error(
                        "AppWorks workspace configuration for -> '%s'%s requires 'workspace_id', 'name', and 'path' settings! Skipping...",
                        organization,
                        " (workspace name -> {})".format(workspace_name) if workspace_name else "",
                    )
                    success = False
                    continue

                response, created = self._otawp.create_workspace(
                    workspace_name=workspace_name, workspace_id=workspace_id
                )
                if not response:
                    self.logger.info("Failed to create workspace -> '%s' (%s)!", workspace_name, workspace_id)
                    success = False
                    continue

                if created:
                    self.logger.info("Setup new workspace -> '%s' (%s)...", workspace_name, workspace_id)
                    response = self._otawp.sync_workspace(
                        workspace_name=workspace_name,
                        workspace_id=workspace_id,
                    )
                    if not response:
                        self.logger.error("Failed to synchronize workspace -> '%s' (%s)!", workspace_name, workspace_id)
                        success = False
                        continue
                    self.logger.info(
                        "Copy projects artifacts to workspace -> '%s' (%s) in AppWorks pod -> '%s'...",
                        workspace_name,
                        workspace_id,
                        "appworks-0",
                    )
                    self._k8s.exec_pod_command(
                        pod_name="appworks-0",
                        command=[
                            "/bin/sh",
                            "-c",
                            f'cp -r "{workspace_path}/"* "/opt/appworks/cluster/shared/cws/sync/{organization}/{workspace_name}"',
                        ],
                        timeout=600,
                    )
                    self.logger.info(
                        "Copying of projects artifacts to workspace -> '%s' (%s) completed.",
                        workspace_name,
                        workspace_id,
                    )
                self.logger.info("Re-sync existing workspace -> '%s' (%s)...", workspace_name, workspace_id)
                response = self._otawp.sync_workspace(
                    workspace_name=workspace_name,
                    workspace_id=workspace_id,
                )
                if not response:
                    self.logger.error("Failed to synchronize workspace -> '%s' (%s)!", workspace_name, workspace_id)
                    success = False
                    continue

                if "projects" in workspace:
                    for project in workspace["projects"]:
                        if project.get("name") and project.get("documentId"):
                            if not self._otawp.publish_project(
                                workspace_name=workspace_name,
                                workspace_id=workspace_id,
                                project_name=project.get("name"),
                                project_id=project.get("documentId"),
                            ):
                                success = False
                                continue
                        else:
                            self.logger.error(
                                "Skipping project -> '%s' due to missing required project fields 'name' or 'documentId'!",
                                project.get("name"),
                            )
                            success = False
                            continue
                    # end for project in workspace["projects"]:
                # end if "projects" in workspace
            # end for workspace in value["cws"]["workspaces"]

            # Process the entities in the payload:
            entities = appworks_configuration.get("entities", [])
            if entities:
                self._log_header_callback(
                    text="Process AppWorks entities for organization -> '{}'".format(organization), char="-"
                )
                for entity in entities:
                    if not self.process_appworks_entity(entity=entity):
                        success = False
                self.logger.info("Entity processing completed for organization -> '%s'.", organization)
        # end for appworks_configuration in self._appworks_configurations:

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._appworks_configurations,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_appworks_entity")
    def process_appworks_entity(self, entity: dict) -> bool:
        """Process a single AppWorks entity payload.

        Args:
            entity (dict):
                Entity payload.
                Should have a selection of the following keys:
                * type
                * name
                * description
                * status
                * prefix
                * category
                * priority
                * customer
                * case_type
                * ...

        Returns:
            bool: True = success, False = failure.

        """

        entity_type = entity.get("type")
        if not entity_type:
            return False

        match entity_type:
            case "category":
                cat = self._otawp.get_category_by_name(name=entity.get("name"))
                if cat:
                    cat_id = self._otawp.get_entity_value(entity=cat, key="id")
                    self.logger.info(
                        "Category -> '%s' (%s) does already exist. Skipping...", entity.get("name"), str(cat_id)
                    )
                    self.logger.debug("Category -> %s", str(cat))
                else:
                    response = self._otawp.create_category(
                        case_prefix=entity.get("prefix"),
                        name=entity.get("name"),
                        description=entity.get("description", ""),
                        status=entity.get("status", 1),
                    )
                    if not response or "Identity" not in response:
                        self.logger.error("Failed to create category -> '%s'!", entity.get("name"))
                        return False
                    cat_id = response["Identity"].get("Id")
                    self.logger.info(
                        "Successfully created category -> '%s' (%s).",
                        entity.get("name"),
                        cat_id,
                    )
                    self.logger.debug("Response -> %s", str(response))
                if "sub_entities" in entity:
                    for sub_entity in entity["sub_entities"]:
                        if sub_entity["type"] != "subCategory":
                            self.logger.warning(
                                "Found a category sub-entities with wrong type -> '%s'!", sub_entity["type"]
                            )
                            continue
                        response = self._otawp.create_sub_category(
                            parent_id=cat_id,
                            name=sub_entity.get("name"),
                            description=sub_entity.get("description", ""),
                            status=sub_entity.get("status", 1),
                        )
                        if not response or "Identity" not in response:
                            self.logger.error("Failed to create sub-category -> '%s'!", sub_entity.get("name"))
                            return False
                        self.logger.info(
                            "Successfully created sub-category -> '%s' (%s) in parent category -> '%s' (%s).",
                            sub_entity.get("name"),
                            response["Identity"].get("Id"),
                            entity.get("name"),
                            cat_id,
                        )
                        self.logger.debug("Response -> %s", str(response))
                return True
            case "priority":
                priority = self._otawp.get_priority_by_name(name=entity.get("name"))
                if priority:
                    priority_id = self._otawp.get_entity_value(entity=priority, key="id")
                    (
                        self.logger.info(
                            "Priority -> '%s' (%s) does already exist. Skipping...",
                            entity.get("name"),
                            str(priority_id),
                        )
                    )
                    return True
                response = self._otawp.create_priority(
                    name=entity.get("name"),
                    description=entity.get("description", ""),
                    status=entity.get("status", 1),
                )
                if not response or "Identity" not in response:
                    self.logger.error("Failed to create priority -> '%s'!", entity.get("name"))
                    return False
                self.logger.info(
                    "Successfully created priority -> '%s' (%s).", entity.get("name"), response["Identity"].get("Id")
                )
                self.logger.debug("Response -> %s", str(response))
                return True
            case "caseType":
                case_type = self._otawp.get_case_type_by_name(name=entity.get("name"))
                if case_type:
                    case_type_id = self._otawp.get_entity_value(entity=case_type, key="id")
                    self.logger.info(
                        "Case type -> '%s' (%s) does already exist. Skipping...", entity.get("name"), str(case_type_id)
                    )
                    return True
                response = self._otawp.create_case_type(
                    name=entity.get("name"),
                    description=entity.get("description", ""),
                    status=entity.get("status", 1),
                )
                if not response or "Identity" not in response:
                    self.logger.error("Failed to case type -> '%s'!", entity.get("name"))
                    return False
                self.logger.info(
                    "Successfully created case type -> '%s' (%s).", entity.get("name"), response["Identity"].get("Id")
                )
                self.logger.debug("Response -> %s", str(response))
                return True
            case "customer":
                customer = self._otawp.get_customer_by_name(name=entity.get("name"))
                if customer:
                    customer_id = self._otawp.get_entity_value(entity=customer, key="id")
                    self.logger.info(
                        "Customer -> '%s' (%s) does already exist. Skipping...", entity.get("name"), str(customer_id)
                    )
                    return True
                response = self._otawp.create_customer(
                    customer_name=entity.get("name"),
                    legal_business_name=entity.get("legal_business_name", ""),
                    trading_name=entity.get("trading_name", ""),
                )
                if not response or "Identity" not in response:
                    self.logger.error("Failed to create customer -> '%s'!", entity.get("name"))
                    return False
                self.logger.info(
                    "Successfully created customer -> '%s' (%s).", entity.get("name"), response["Identity"].get("Id")
                )
                self.logger.debug("Response -> %s", str(response))
                return True
            case "case":
                if "subject" not in entity:
                    self.logger.error("Cannot create a case without a subject!")
                    return False

                category_name = entity.get("category")
                if category_name:
                    category = self._otawp.get_category_by_name(name=category_name)
                    category_id = self._otawp.get_entity_value(entity=category, key="id")
                    if not category_id:
                        self.logger.error(
                            "Cannot find case category -> '%s' to create case -> '%s'!",
                            category_name,
                            entity["subject"],
                        )
                        return False
                else:
                    self.logger.warning(
                        "Case entity -> '%s' does not have a category specified in its payload!", entity["subject"]
                    )
                    category_id = None

                sub_category_name = entity.get("sub_category")
                if category_id and sub_category_name:
                    sub_category_id = self._otawp.get_sub_category_id(name=sub_category_name, parent_id=category_id)
                else:
                    sub_category_id = None

                priority_name = entity.get("priority")
                if priority_name:
                    priority = self._otawp.get_priority_by_name(name=priority_name)
                    priority_id = self._otawp.get_entity_value(entity=priority, key="id")
                    if not priority_id:
                        self.logger.error(
                            "Cannot find case priority -> '%s' to create case -> '%s'!",
                            priority_name,
                            entity["subject"],
                        )
                        return False
                else:
                    self.logger.warning(
                        "Case entity -> '%s' does not have a priority specified in its payload!", entity["subject"]
                    )
                    priority_id = None

                case_type_name = entity.get("case_type")
                if case_type_name:
                    case_type = self._otawp.get_case_type_by_name(name=case_type_name)
                    case_type_id = self._otawp.get_entity_value(entity=case_type, key="id")
                    if not case_type_id:
                        self.logger.error(
                            "Cannot find case type -> '%s' to create case -> '%s'!",
                            case_type_name,
                            entity["subject"],
                        )
                        return False
                else:
                    self.logger.warning(
                        "Case entity -> '%s' does not have a case type specified in its payload!", entity["subject"]
                    )
                    case_type_id = None

                customer_name = entity.get("customer")
                if customer_name:
                    customer = self._otawp.get_customer_by_name(name=customer_name)
                    customer_id = self._otawp.get_entity_value(entity=customer, key="id")
                    if not customer_id:
                        self.logger.error(
                            "Cannot find customer -> '%s' to create case -> '%s'!",
                            customer_name,
                            entity["subject"],
                        )
                        return False
                else:
                    self.logger.warning(
                        "Case entity -> '%s' does not have a customer specified in its payload!", entity["subject"]
                    )
                    customer_id = None

                response = self._otawp.create_case(
                    subject=entity.get("subject"),
                    description=entity.get("description", ""),
                    loan_amount=entity.get("loan_amount", 1),
                    loan_duration_in_months=entity.get("loan_duration_in_month", 2),
                    category_id=category_id,
                    sub_category_id=sub_category_id,
                    priority_id=priority_id,
                    case_type_id=case_type_id,
                    customer_id=customer_id,
                )
                if not response:
                    self.logger.error("Failed to create case with subject -> '%s'!", entity.get("subject"))
                    return False
                self.logger.info(
                    "Successfully created case with subject -> '%s'%s.",
                    entity.get("subject"),
                    " for customer with ID -> '{}'".format(customer_id) if customer_id else "",
                )
                self.logger.debug("Response -> %s", str(response))
                return True
            case _:
                self.logger.error("Illegal entity type -> '%s'!", entity_type)
                return False

        return False

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_all_group_names")
    def get_all_group_names(self) -> list:
        """Construct a list of all group name.

        Returns:
            list:
                A list of all group names.

        """

        return [group.get("name") for group in self._groups]

    # end method definition
    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_status_file_name")
    def get_status_file_name(
        self,
        payload_section_name: str,
        payload_specific: bool = True,
        prefix: str = "success_",
    ) -> str:
        """Construct the name of the status file.

        Args:
            payload_section_name (str):
                The name of the payload section. This
                is used to construct the file name
            payload_specific (bool, optional):
                Whether or not the success should be specific for
                each payload file or if success is "global" - like for the deletion
                of the existing M365 teams (which we don't want to execute per
                payload file)
            prefix (str, optional):
                The prefix of the file name. Typically, either "success_" or "failure_".

        Returns:
            str:
                The constructed name of the payload section file.

        """

        # Some sections are actually not payload specific like teamsM365Cleanup
        # we don't want external payload runs to re-apply this processing:
        if payload_specific:
            file_name = os.path.basename(self._payload_source)  # remove directories
            # Split once at the first occurance of a dot
            # as the _payload_source may have multiple suffixes
            # such as .yml.gz.b64:
            file_name = file_name.split(".", 1)[0]
            file_name = prefix + file_name + "_" + payload_section_name + ".json"
        else:
            file_name = prefix + payload_section_name + ".json"

        return file_name

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="check_status_file")
    def check_status_file(
        self,
        payload_section_name: str,
        payload_specific: bool = True,
        prefix: str = "success_",
    ) -> bool:
        """Check if the payload section has been processed before.

        This is done by checking the existance of a text file in the Admin Personal
        workspace in Content Server with the name of the payload section.

        Args:
            payload_section_name (str):
                The name of the payload section. This
                is used to construct the file name
            payload_specific (bool, optional):
                Whether or not the success should be specific for
                each payload file or if success is "global" - like for the deletion
                of the existing M365 teams (which we don't want to execute per
                payload file)
            prefix (str, optional):
                The prefix of the file. Typically, either "success_" or "failure_".

        Returns:
            bool:
                True if the payload has been processed successfully before, False otherwise

        """

        message = "successfully" if prefix.startswith("success_") else "with failures"

        self.logger.info(
            "Check if payload section -> '%s' has been processed %s before...",
            payload_section_name,
            message,
        )

        while not self._otcs.is_ready():
            self.logger.info(
                "OTCS is not ready. Cannot check status file for -> '%s' in OTCS. Waiting 30 seconds and retry...",
                payload_section_name,
            )
            time.sleep(30)

        response = self._otcs.get_node_by_volume_and_path(
            volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
        )  # write to Personal Workspace of Admin
        target_folder_id = self._otcs.get_result_value(response=response, key="id")
        if not target_folder_id:
            target_folder_id = 2004  # use Personal Workspace of Admin as fallback

        file_name = self.get_status_file_name(
            payload_section_name=payload_section_name,
            payload_specific=payload_specific,
            prefix=prefix,
        )

        status_document = self._otcs.get_node_by_parent_and_name(
            parent_id=int(target_folder_id),
            name=file_name,
            show_error=False,
        )
        if status_document and status_document.get("results", []):
            name = self._otcs.get_result_value(response=status_document, key="name")
            if name == file_name:
                self.logger.info(
                    "Payload section -> '%s' has been processed %s before. %s...",
                    payload_section_name,
                    message,
                    "Skipping" if prefix.startswith("success_") else "Retrying",
                )
                return True
        self.logger.info(
            "Payload section -> '%s' has not been processed %s before. Processing...",
            payload_section_name,
            message,
        )
        return False

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="write_status_file")
    def write_status_file(
        self,
        success: bool,
        payload_section_name: str,
        payload_section: list,
        payload_specific: bool = True,
    ) -> bool:
        """Write a status file into the Admin Personal Workspace in Content Server.

        This is to indicate that the payload section has been deployed successfully.
        This speeds up the customizing process in case the customizer pod
        is restarted.

        Args:
            success (bool):
                True, if the section was processed successful, False otherwise.
            payload_section_name (str):
                The name of the payload section.
            payload_section (list):
                The payload section content - this is written as JSon into the file.
            payload_specific (bool, optional):
                Whether or not the success should be specific for
                each payload file or if success is "global" - like for the deletion
                of the existing M365 teams (which we don't want to execute per
                payload file)

        Returns:
            bool:
                True if the status file as been upladed to Content Server successfully, False otherwise

        """

        # Do we want status files to be uploaded?
        if not self.upload_status_files:
            return True

        if success:
            self.logger.info(
                "Payload section -> '%s' has been completed successfully.",
                payload_section_name,
            )
            prefix = "success_"
        else:
            self.logger.error(
                "Payload section -> '%s' had failures!",
                payload_section_name,
            )
            prefix = "failure_"

        while not self._otcs.is_ready():
            self.logger.info(
                "OTCS is not ready. Cannot write status file for -> '%s' to OTCS. Waiting 30 seconds and retry...",
                payload_section_name,
            )
            time.sleep(30)

        response = self._otcs.get_node_by_volume_and_path(
            volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
        )  # write to Personal Workspace of Admin (with Volume Type ID = 142)
        target_folder_id = self._otcs.get_result_value(response=response, key="id")
        if not target_folder_id:
            target_folder_id = 2004  # use Personal Workspace of Admin as fallback

        file_name = self.get_status_file_name(
            payload_section_name=payload_section_name,
            payload_specific=payload_specific,
            prefix=prefix,
        )

        full_path = os.path.join(tempfile.gettempdir(), file_name)

        with open(full_path, mode="w", encoding="utf-8") as localfile:
            localfile.write(json.dumps(payload_section, indent=2))

        # Check if the status file has been uploaded before.
        # This can happen if we re-run the python container.
        # In this case we add a version to the existing document:
        response = self._otcs.get_node_by_parent_and_name(
            parent_id=int(target_folder_id),
            name=file_name,
            show_error=False,
        )
        target_document_id = self._otcs.get_result_value(response=response, key="id")
        if target_document_id:
            response = self._otcs.add_document_version(
                node_id=int(target_document_id),
                file_url=full_path,
                file_name=file_name,
                mime_type="text/plain",
                description="Updated status file after re-run of customization",
            )
        else:
            response = self._otcs.upload_file_to_parent(
                file_url=full_path,
                file_name=file_name,
                mime_type="text/plain",
                parent_id=int(target_folder_id),
            )

        if response:
            self.logger.info(
                "Status file -> '%s' has been written to Personal Workspace of admin user.",
                file_name,
            )
            return True

        self.logger.error(
            "Failed to write status file -> '%s' to Personal Workspace of admin user!",
            file_name,
        )

        return False

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_status_file")
    def get_status_file(
        self,
        payload_section_name: str,
        payload_specific: bool = True,
        prefix: str = "success_",
    ) -> list | None:
        """Get the status file and read it into a list of dictionaries.

        Args:
            payload_section_name (str):
                The name of the payload section. This
                is used to construct the file name.
            payload_specific (bool, optional):
                Whether or not the success should be specific for
                each payload file or if success is "global" - like for the deletion
                of the existing M365 teams (which we don't want to execute per
                payload file)
            prefix (str, optional):
                The prefix of the file. Typically, either "success_" or "failure_".

        Returns:
            list:
                Content of the status file as a list of dictionaries or None in case of an error.

        """

        self.logger.info(
            "Get the status file of the payload section -> '%s'...",
            payload_section_name,
        )

        while not self._otcs.is_ready():
            self.logger.info(
                "OTCS is not ready. Cannot read status file for -> '%s' from OTCS. Waiting 30 seconds and retry...",
                payload_section_name,
            )
            time.sleep(30)

        response = self._otcs.get_node_by_volume_and_path(
            volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
        )  # read from Personal Workspace of Admin
        source_folder_id = self._otcs.get_result_value(response=response, key="id")
        if not source_folder_id:
            source_folder_id = 2004  # use Personal Workspace ID of Admin as fallback

        file_name = self.get_status_file_name(
            payload_section_name=payload_section_name,
            payload_specific=payload_specific,
            prefix=prefix,
        )

        status_document = self._otcs.get_node_by_parent_and_name(
            parent_id=int(source_folder_id),
            name=file_name,
            show_error=True,
        )
        status_file_id = self._otcs.get_result_value(response=status_document, key="id")
        if not status_file_id:
            self.logger.error("Cannot find status file -> '%s'!", file_name)
            return None

        return self._otcs.get_json_document(node_id=status_file_id)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_payload")
    def get_payload(self, drop_bulk_datasources_data: bool = False) -> dict:
        """Get the Payload as reference.

        Optional a copy of the payload can be delivered the does not include the
        "data" value of "bulkDatasource" (its content can be HUGE and many times we don't
        want it).

        Args:
            drop_bulk_datasources_data (bool, optional):
                If True, returns a !copy! of the payload without the bulkDatasources
                "data" in it. Defaults to False.

        Returns:
            dict: Returns the payload

        """

        if drop_bulk_datasources_data and "bulkDatasources" in self._payload:
            # Create a Copy of the payload, but without the bulkDatasources
            # using deepcopy would require significantly more memory, just to drop it directly after
            payload = {k: v for k, v in self._payload.items() if k != "bulkDatasources"}

            payload["bulkDatasources"] = [
                {k: v for k, v in data.items() if k != "data"} for data in self._payload.get("bulkDatasources", {})
            ]
            return payload

        return self._payload

    # end method definition

    def get_users(self) -> list:
        """Get all users in payload."""
        return self._users

    def get_groups(self) -> list:
        """Get all groups in payload."""
        return self._groups

    def get_workspaces(self) -> list:
        """Get all workspaces in payload."""
        return self._workspaces

    def get_otcs_frontend(self) -> object:
        """Get OTCS Frontend oject."""
        return self._otcs_frontend

    def get_otcs_backend(self) -> object:
        """Get OTCS Backend object."""
        return self._otcs_backend

    def get_otds(self) -> object:
        """Get the OTDS object."""
        return self._otds

    def get_k8s(self) -> object:
        """Get the K8s object."""
        return self._k8s

    def get_m365(self) -> object:
        """Get the M365 object."""
        return self._m365

    def generate_password(
        self,
        length: int,
        use_special_chars: bool = False,
        min_special: int = 1,
        min_numerical: int = 1,
        min_upper: int = 1,
        min_lower: int = 1,
        override_special: str | None = None,
    ) -> str | None:
        """Generate random passwords with a given specification.

        Args:
            length (int):
                Define password length.
            use_special_chars (bool, optional):
                Define if special characters should be used. Defaults to False.
            min_special (int, optional):
                Define min amount of special characters. Defaults to 1.
            min_numerical (int, optional):
                Define if numbers should be used. Defaults to 1.
            min_upper (int, optional):
                Define mininum number of upper case letters. Defaults to 1.
            min_lower (int, optional):
                Define minimum number of lower case letters. Defaults to 1.
            override_special (string | None, optional):
                Define special characters to be used, if not set: !@#$%^&*()_-+=<>?/{}[]. Defaults to None.

        Returns:
            str | None:
                The generated password. None in case of an error.

        """
        # Define character sets
        lowercase_letters = string.ascii_lowercase
        uppercase_letters = string.ascii_uppercase
        numerical_digits = string.digits
        special_characters = "!@#$%^&*()_-+=<>?/{}[]"

        if override_special:
            special_characters = override_special
        # Ensure minimum requirements are met

        if min_special + min_numerical + min_upper + min_lower > length:
            self.logger.error("Minimum requirements exceed password length! Cannot generate password.")
            return None

        # Initialize the password
        password = []

        # Add required characters
        password.extend(random.sample(lowercase_letters, min_lower))
        password.extend(random.sample(uppercase_letters, min_upper))
        password.extend(random.sample(numerical_digits, min_numerical))

        if use_special_chars:
            password.extend(random.sample(special_characters, min_special))

        # Fill the rest of the password with random characters
        remaining_length = length - len(password)
        all_chars = lowercase_letters + uppercase_letters + numerical_digits

        if use_special_chars:
            all_chars += special_characters

        password.extend(random.choices(all_chars, k=remaining_length))

        # Shuffle the password to ensure randomness
        random.shuffle(password)

        # Convert the password list to a string
        final_password = "".join(password)

        return final_password

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_group_id")
    def determine_group_id(self, group: dict) -> int:
        """Determine the id of a group - either from payload or from OTCS.

        If the group is found in OTCS write back the ID into the payload.

        Args:
            group (dict):
                The group payload element.

        Returns:
            int: group ID

        Side Effects:
            the group items are modified by adding an "id" dict element that
            includes the technical ID of the group in Content Server.

        """

        # Is the ID already known in payload? (if determined before)
        if "id" in group:
            return group["id"]

        if "name" not in group:
            self.logger.error("Group needs a name to lookup the ID!")
            return 0
        group_name = group["name"]

        existing_groups = self._otcs.get_group(name=group_name)
        # We use the lookup method here as get_group() could deliver more
        # then 1 result element (in edge cases):
        existing_group_id = self._otcs.lookup_result_value(
            response=existing_groups,
            key="name",
            value=group_name,
            return_key="id",
        )

        # Have we found an exact match?
        if existing_group_id:
            self.logger.debug(
                "Found existing group -> '%s' with ID -> %s. Update ID in payload...",
                group_name,
                existing_group_id,
            )
            # Write ID back into the payload:
            group["id"] = existing_group_id
            return group["id"]
        else:
            self.logger.debug(
                "Cannot find an existing group -> '%s'",
                group_name,
            )
            return 0

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_group_id_m365")
    def determine_group_id_m365(self, group: dict) -> str | None:
        """Determine the id of a M365 group - either from payload or from M365 via Graph API.

        If the group is found in M365 write back the M365 group ID into the payload.

        Args:
            group (dict):
                The group payload element.

        Returns:
            str | None:
                M365 group ID or None if the group is not found.

        Side Effects:
            The group items are modified by adding an "m365_id" dict item that
            includes the technical ID of the group in Microsoft 365.

        """

        # is the payload already updated with the M365 group ID?
        if "m365_id" in group:
            return group["m365_id"]

        if "name" not in group:
            self.logger.error("Group needs a name to lookup the M365 group ID!")
            return None
        group_name = group["name"]

        existing_group = self._m365.get_group(group_name=group_name)
        existing_group_id = self._m365.get_result_value(
            response=existing_group,
            key="id",
        )
        if existing_group_id:
            self.logger.debug(
                "Found existing Microsoft 365 group -> '%s' with ID -> %s. Update m365_id in payload...",
                group_name,
                existing_group_id,
            )
            # write back the M365 user ID into the payload
            group["m365_id"] = existing_group_id
            return group["m365_id"]
        else:
            self.logger.debug(
                "Cannot find an existing M365 group -> '%s'",
                group_name,
            )
            return None

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_group_id_core_share")
    def determine_group_id_core_share(self, group: dict) -> str | None:
        """Determine the id of a Core Share group.

        This can either be derived from payload or from Core Share directly.

        Args:
            group (dict):
                The payload dictionary of the group.

        Returns:
            str | None:
                Core Share Group ID or None.

        """

        # Is the ID already known in payload? (if determined before)
        if "core_share_id" in group:
            return group["core_share_id"]

        if "name" not in group:
            self.logger.error("Group needs a name to lookup the Core Share ID!")
            return None

        if not isinstance(self._core_share, CoreShare):
            self.logger.error(
                "Core Share connection not setup properly!",
            )
            return None

        core_share_group = self._core_share.get_group_by_name(name=group["name"])
        core_share_group_id = self._core_share.get_result_value(
            response=core_share_group,
            key="id",
        )

        # Have we found the group?
        if core_share_group_id:
            self.logger.debug(
                "Found existing Core Share group -> '%s' with ID -> %s. Update m365_id in payload...",
                group["name"],
                core_share_group_id,
            )
            # Write ID back into the payload:
            group["core_share_id"] = core_share_group_id
            return group["core_share_id"]
        else:
            self.logger.debug(
                "Cannot find an existing Core Share group -> '%s'",
                group["name"],
            )
            return None

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_user_id")
    def determine_user_id(self, user: dict) -> int:
        """Determine the id of a user - either from payload or from OTCS.

        If the user is found in OTCS write back the ID into the payload.

        Args:
            user (dict):
                The user payload element.

        Returns:
            int:
                The user ID in OTCS.

        Side Effects:
            The user items are modified by adding an "id" dict element that
            includes the technical ID of the user in Content Server

        """

        # Is the ID already known in payload? (if determined before)
        if "id" in user:
            return user["id"]

        user_name = user.get("name")
        if not user_name:
            self.logger.error("User needs a login name to lookup the ID!")
            return 0

        user_type = 17 if user.get("type", "User") == "ServiceUser" else 0

        response = self._otcs.get_user(name=user_name, user_type=user_type)

        # We use the lookup method here as get_user() could deliver more
        # then 1 result element (in edge cases):
        user_id = self._otcs.lookup_result_value(
            response=response,
            key="name",
            value=user_name,
            return_key="id",
        )

        # Have we found an exact match?
        if user_id:
            # Write ID back into the payload
            user["id"] = user_id
            return user["id"]
        else:
            self.logger.debug(
                "Cannot find an existing user -> '%s'!",
                user_name,
            )
            return 0

        # end method definition

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_user_id_m365")
    def determine_user_id_m365(self, user: dict) -> str | None:
        """Determine the id of a M365 user - either from payload or from M365 via Graph API.

        If the user is found in M365 write back the M365 user ID into the payload.

        Args:
            user (dict):
                The user payload element.

        Returns:
            str | None:
                The M365 user ID or None if the user is not found.

        Side Effects:
            the user items are modified by adding an "m365_id" dict element that
            includes the technical ID of the user in Microsoft 365

        """

        # is the payload already updated with the M365 user ID?
        if "m365_id" in user:
            return user["m365_id"]

        if "name" not in user:
            self.logger.error("User needs a login name to lookup the M365 user ID!")
            return None
        user_name = user["name"]

        m365_user_name = user_name + "@" + self._m365.config()["domain"]
        existing_user = self._m365.get_user(user_email=m365_user_name)
        if existing_user:
            self.logger.debug(
                "Found existing Microsoft 365 user -> '%s' (%s) with ID -> %s. Update m365_id in payload...",
                existing_user["displayName"],
                existing_user["userPrincipalName"],
                existing_user["id"],
            )
            # write back the M365 user ID into the payload
            user["m365_id"] = existing_user["id"]
            return user["m365_id"]
        else:
            self.logger.debug(
                "Did not find an existing M365 user -> '%s'",
                user_name,
            )
            return None

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_user_id_core_share")
    def determine_user_id_core_share(self, user: dict) -> str | None:
        """Determine the ID of a Core Share user.

        The ID is either taken from payload or from Core Share directly.

        Args:
            user (dict):
                The payload dictionary of the user.

        Returns:
            str | None:
                Core Share User ID or None.

        """

        # Is the ID already known in payload? (if determined before)
        if "core_share_id" in user:
            return user["core_share_id"]

        if not isinstance(self._core_share, CoreShare):
            self.logger.error(
                "Core Share connection not setup properly!",
            )
            return False

        core_share_user_id = None

        # Next try to lookup ID via the email address:
        if "email" in user:
            core_share_user = self._core_share.get_user_by_email(email=user["email"])
            core_share_user_id = self._core_share.get_result_value(
                response=core_share_user,
                key="id",
            )

        # Last resort is to lookup the ID via firstname + lastname.
        # This is handy in case the Email has changed:
        if not core_share_user_id and "lastname" in user and "firstname" in user:
            core_share_user = self._core_share.get_user_by_name(
                first_name=user["firstname"],
                last_name=user["lastname"],
            )
            core_share_user_id = self._core_share.get_result_value(
                response=core_share_user,
                key="id",
            )

        # Have we found the user?
        if core_share_user_id:
            # Write ID back into the payload:
            user["core_share_id"] = core_share_user_id
            return user["core_share_id"]
        else:
            if "email" in user:
                self.logger.debug(
                    "Did not find an existing Core Share user with email -> '%s'",
                    user["email"],
                )
            else:
                self.logger.debug(
                    "Cannot find an existing Core Share user -> '%s %s'",
                    user.get("firstname"),
                    user.get("lastname"),
                )
            return None

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_workspace_id")
    def determine_workspace_id(self, workspace: dict) -> int:
        """Determine the node ID of a workspace - either from payload or from OTCS.

        Args:
            workspace (dict):
                The workspace payload element.

        Returns:
            int:
                The workspace Node ID.

        Side Effects:
            The workspace items are modified by adding an "nodeId" dict element that
            includes the node ID of the workspace in Content Server.

        """

        if "nodeId" in workspace:
            return workspace["nodeId"]

        response = self._otcs.get_workspace_by_type_and_name(
            type_name=workspace["type_name"],
            name=workspace["name"],
        )
        workspace_id = self._otcs.get_result_value(response=response, key="id")
        if workspace_id:
            if not isinstance(workspace_id, int):
                self.logger.warning("Converting workspace ID -> %s to integer...", str(workspace_id))
                workspace_id = int(workspace_id)
            # Write nodeID back into the payload
            workspace["nodeId"] = workspace_id
            return workspace_id
        else:
            self.logger.info(
                "Workspace of type -> '%s' and name -> '%s' does not yet exist.",
                workspace["type_name"],
                workspace["name"],
            )
            return 0

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_workspace_type_and_template_id")
    def determine_workspace_type_and_template_id(
        self,
        workspace_type_name: str,
        workspace_template_name: str = "",
    ) -> tuple[int | None, int | None]:
        """Determine the IDs of type and template based on the provided names.

        This depends on the self._workspace_types list to be up to date
        (see process_workspace_types()).

        Args:
            workspace_type_name (str):
                Name of the workspace type.
            workspace_template_name (str, optional):
                Name of the workspace template. Defaults to "".

        Returns:
            tuple[int, int]:
                IDs of the workspace type (first) and workspace template (second).

        """

        # Check if the customizer has initialized the workspace type list
        if not self._workspace_types:
            self.logger.error(
                "Workspace type list is not initialized! This should never happen!",
            )
            return (None, None)

        # Find the workspace type with the name given in the payload:
        workspace_type = next(
            (item for item in self._workspace_types if item["name"] == workspace_type_name),
            None,
        )
        if workspace_type is None:
            self.logger.error(
                "Workspace type -> '%s' not found!",
                workspace_type_name,
            )
            return (None, None)

        workspace_type_id = workspace_type["id"]

        if not workspace_type.get("templates", []):
            self.logger.warning(
                "Workspace type -> '%s' does not have templates!",
                workspace_type_name,
            )
            return (workspace_type_id, None)

        if workspace_template_name:
            workspace_template = next(
                (item for item in workspace_type["templates"] if item["name"] == workspace_template_name),
                None,
            )
            if workspace_template:  # does this template exist?
                self.logger.info(
                    "Workspace template -> '%s' has been specified in payload and it does exist.",
                    workspace_template_name,
                )
            else:
                self.logger.error(
                    "Workspace template -> '%s' has been specified in payload but it doesn't exist!",
                    workspace_template_name,
                )
                self.logger.error(
                    "Workspace type -> '%s' has only these templates -> %s",
                    workspace_type_name,
                    workspace_type["templates"],
                )
                return (workspace_type_id, None)

        # template to be used is NOT specified in the payload - then we just take the first one:
        else:
            workspace_template = workspace_type["templates"][0]
            self.logger.info(
                "Workspace template has not been specified in payload - taking the first one (%s)...",
                workspace_template,
            )

        workspace_template_id = workspace_template["id"]

        return (workspace_type_id, workspace_template_id)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="add_transport_extractions")
    def add_transport_extractions(self, extractions: list) -> int:
        """Determine the number of extrations.

        Safe them in a global list self._transport_extractions.

        Args:
            extractions (list):
                A list of extractions from a single transport package.

        Returns:
            int:
                THE number of extractions that have actually extracted data.

        """

        counter = 0
        for extraction in extractions:
            if extraction.get("enabled", True) and "data" in extraction:
                self._transport_extractions.append(extraction)
                counter += 1
        self.logger.info("Added -> %s transport extractions.", str(counter))

        return counter

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_payload")
    def process_payload(self) -> None:
        """Process a payload file."""

        if not self._payload_sections:
            self.logger.warning("No payload sections defined!")
            return

        for payload_section in self._payload_sections:
            with tracer.start_as_current_span("process_payload-section") as t:
                t.set_attributes({"payload_section": payload_section["name"]})
                self.logger.debug("processing payload_section -> %s", payload_section)
                if not payload_section.get("enabled", True):
                    self.logger.info(
                        "Payload section -> '%s' is disabled. Skipping...",
                        payload_section.get("name", "Name not defined"),
                    )
                    continue

                match payload_section["name"]:
                    case "embeddings" | "feme":
                        self._log_header_callback(
                            text="Process additional Embeddings for Content Aviator",
                        )
                        self.process_embeddings()
                    case "avtsRepositories":
                        self._log_header_callback(
                            text="Process Aviator Search repositories",
                        )
                        self.process_avts_repositories()
                    case "avtsQuestions":
                        self._log_header_callback(
                            text="Process Aviator Search Sample Questions",
                        )
                        self.process_avts_questions()
                    case "appworks":
                        self._log_header_callback(text="Process AppWorks Configurations")
                        self.process_appworks_configurations()
                    case "webHooks":
                        self._log_header_callback(text="Process Web Hooks")
                        self.process_web_hooks(webhooks=self._webhooks)
                    case "webHooksPost":
                        self._log_header_callback(text="Process Web Hooks (post)")
                        self.process_web_hooks(
                            webhooks=self._webhooks_post,
                            section_name="webHooksPost",
                        )
                    case "resources":
                        self._log_header_callback(text="Process OTDS Resources")
                        self.process_resources()
                    case "partitions":
                        self._log_header_callback(text="Process OTDS Partitions")
                        self.process_partitions()
                    case "licenses":
                        self._log_header_callback(text="Process OTDS Licenses")
                        self.process_licenses()
                    case "synchronizedPartitions":
                        self._log_header_callback(
                            text="Process OTDS Synchronized Partitions",
                        )
                        self.process_synchronized_partitions()
                    case "oauthClients":
                        self._log_header_callback(text="Process OTDS OAuth Clients")
                        self.process_oauth_clients()
                    case "applicationRoles":
                        self._log_header_callback(text="Process OTDS Application Roles")
                        self.process_application_roles()
                    case "authHandlers":
                        self._log_header_callback(text="Process OTDS Auth Handlers")
                        self.process_auth_handlers()
                    case "trustedSites":
                        self._log_header_callback(text="Process OTDS Trusted Sites")
                        self.process_trusted_sites()
                    case "systemAttributes":
                        self._log_header_callback(text="Process OTDS System Attributes")
                        self.process_system_attributes()
                    case "docgenSettings":
                        if self._otpd and isinstance(self._otpd, OTPD):
                            self._log_header_callback(text="Process OTPD Settings")
                            self.process_docgen_settings()
                    case "groups":
                        self._log_header_callback(text="Process Groups")
                        self.process_groups()
                        # Add all groups with ID the a lookup dict for placeholder replacements
                        # in adminSetting. This also updates the payload with group IDs from OTCS
                        # if the group already exists in Content Server. This is important especially
                        # if the customizer pod is restarted / run multiple times:
                        self.process_group_placeholders()
                        if self._core_share and isinstance(self._core_share, CoreShare):
                            self._log_header_callback(text="Process Core Share Groups", char="-")
                            self.process_groups_core_share()
                        if self._m365 and isinstance(self._m365, M365):
                            self._log_header_callback(text="Cleanup existing M365 Teams", char="-")
                            self.cleanup_all_teams_m365()
                            self._log_header_callback(text="Process M365 Groups", char="-")
                            self.process_groups_m365()
                    case "users":
                        self._log_header_callback(text="Process Users")
                        self._user_customization = bool(
                            payload_section.get("user_customization", "True"),
                        )
                        self.process_users()
                        # Add all users with ID the a lookup dict for placeholder replacements
                        # in adminSetting. This also updates the payload with user IDs from OTCS
                        # if the user already exists in Content Server. This is important especially
                        # if the cutomizer pod is restarted / run multiple times:
                        self.process_user_placeholders()
                        self._log_header_callback(
                            text="Assign OTCS licenses to users",
                            char="-",
                        )
                        self.process_user_licenses(
                            resource_name=self._otcs.config()["resource"],
                            license_feature=self._otcs.config()["license"],
                            user_specific_payload_field="licenses",
                        )
                        self._log_header_callback(
                            text="Process OTDS user settings",
                            char="-",
                        )
                        self.process_user_settings()
                        if self._core_share and isinstance(self._core_share, CoreShare):
                            self._log_header_callback(text="Process Core Share users", char="-")
                            self.process_users_core_share()
                        if self._m365 and isinstance(self._m365, M365):
                            self._log_header_callback(text="Process M365 users", char="-")
                            self.process_users_m365()
                            # We need to do the MS Teams creation after the creation of
                            # the M365 users as we require Group Owners to create teams.
                            # Note: this is just for the teams of the top-level OTCS groups
                            # (departments), not the MS Teams for the Workspaces. These
                            # are created via the scheduled bots!
                            self._log_header_callback(text="Process M365 Teams for departmental groups", char="-")
                            self.process_teams_m365()
                    case "adminSettings":
                        self._log_header_callback(
                            text="Process OTCS administration settings",
                        )
                        restart_required = self.process_admin_settings(
                            admin_settings=self._admin_settings,
                        )
                        if restart_required:
                            self.logger.info(
                                "Admin Settings require a restart of OTCS services...",
                            )
                            # Restart OTCS frontend and backend pods:
                            self._otcs_restart_callback(
                                backend=self._otcs_backend,
                                frontend=self._otcs_frontend,
                            )
                    case "adminSettingsPost":
                        self._log_header_callback(
                            text="Process OTCS administration settings (post)",
                        )
                        restart_required = self.process_admin_settings(
                            self._admin_settings_post,
                            "adminSettingsPost",
                        )
                        if restart_required:
                            self.logger.info(
                                "Admin settings (Post) require a restart of OTCS services...",
                            )
                            # Restart OTCS frontend and backend pods:
                            self._otcs_restart_callback(
                                backend=self._otcs_backend,
                                frontend=self._otcs_frontend,
                            )
                    case "execPodCommands":
                        self._log_header_callback(text="Process Pod Commands")
                        self.process_exec_pod_commands()
                    case "kubernetes":
                        self._log_header_callback(text="Process Kubernetes Commands")
                        self.process_kubernetes()
                    case "execCommands":
                        self._log_header_callback(text="Process Commands in Customizer Pod")
                        self.process_exec_commands()
                    case "execDatabaseCommands":
                        self._log_header_callback(text="Process Database Commands")
                        self.process_exec_database_commands()
                    case "csApplications":
                        self._log_header_callback(text="Process CS Apps (backend)")
                        self.process_cs_applications(
                            otcs_object=self._otcs_backend,
                            section_name="csApplicationsBackend",
                        )
                        self._log_header_callback("Process CS Apps (frontend)")
                        self.process_cs_applications(
                            otcs_object=self._otcs_frontend,
                            section_name="csApplicationsFrontend",
                        )
                    case "externalSystems":
                        self._log_header_callback(
                            text="Process External System Connections",
                        )
                        self.process_external_systems()
                        # Now the SAP, SuccessFactors and Salesforce objects
                        # should be initialized and we can process users and groups
                        # in these external systems:
                        if self._sap and isinstance(self._sap, SAP):
                            self._log_header_callback(text="Process SAP Users")
                            self.process_users_sap()
                        if self._successfactors and isinstance(
                            self._successfactors,
                            SuccessFactors,
                        ):
                            self._log_header_callback(text="Process SuccessFactors Users")
                            self.process_users_successfactors()
                        if self._salesforce and isinstance(self._salesforce, Salesforce):
                            self._log_header_callback(text="Process Salesforce Groups")
                            self.process_groups_salesforce()
                            self._log_header_callback(text="Process Salesforce Users")
                            self.process_users_salesforce()
                    case "transportPackages":
                        self._log_header_callback(text="Process Transport Packages")
                        self.process_transport_packages(self._transport_packages)
                        # Right after the transport that create the business object types
                        # and the workspace types we extract them and put them into
                        # generated payload lists:
                        self._log_header_callback(text="Process Business Object Types")
                        self.process_business_object_types()
                        self._log_header_callback(text="Process Workspace Types")
                        self.process_workspace_types()
                        if self._m365 and isinstance(self._m365, M365):
                            # Right after the transport that creates the top level folders
                            # we can add the M365 Teams apps for Extended ECM as its own tab:
                            self._log_header_callback(text="Process M365 Teams Apps")
                            self.process_teams_m365_apps()
                            # The SharePoint sites require the top-level departmental folder.
                            # So we can do this only after the transport packages have been
                            # processed:
                            self._log_header_callback(text="Process M365 SharePoint sites for departmental groups")
                            self.process_sites_m365()
                    case "contentTransportPackages":
                        self._log_header_callback("Process Content Transport Packages")
                        self.process_transport_packages(
                            transport_packages=self._content_transport_packages,
                            section_name="contentTransportPackages",
                        )
                        # Process workspace permissions after content has been added:
                        # (this is a workaround for a flaw in transport warehouse that don't
                        # set workspace role permissions for content transported into workspaces)
                        self._log_header_callback(
                            text="Process Workspace Member Permissions",
                            char="-",
                        )
                        self.process_workspace_member_permissions()
                    case "transportPackagesPost":
                        self._log_header_callback("Process Transport Packages (post)")
                        self.process_transport_packages(
                            transport_packages=self._transport_packages_post,
                            section_name="transportPackagesPost",
                        )
                    case "workspaceTemplates":
                        # If a payload file (e.g. additional ones) does not have
                        # transportPackages then it can happen that the
                        # self._business_object_types and self._workspace_types are
                        # not yet initialized. As we need these structures for
                        # workspaceTemplates we initialize them here:
                        if not self._business_object_types:
                            self._log_header_callback("Process Business Object Types")
                            self.process_business_object_types()
                        if not self._workspace_types:
                            self._log_header_callback("Process Workspace Types")
                            self.process_workspace_types()

                        self._log_header_callback(
                            "Process Workspace Templates (Template Role Assignments)",
                        )
                        self.process_workspace_templates()
                    case "workspaces":
                        # If a payload file (e.g. additional ones) does not have
                        # transportPackages then it can happen that the self._business_object_types and
                        # self._workspace_types are not yet initialized. As we need
                        # these structures for workspaces we initialize it here:
                        if not self._business_object_types:
                            self._log_header_callback("Process Business Object Types")
                            self.process_business_object_types()
                        if not self._workspace_types:
                            self._log_header_callback("Process Workspace Types")
                            self.process_workspace_types()

                        self._log_header_callback("Process Workspaces")
                        self.process_workspaces()
                        self._log_header_callback("Process Workspace Relationships")
                        self.process_workspace_relationships()
                        self._log_header_callback("Process Workspace Memberships")
                        self.process_workspace_members()

                        # This has to run after the processing of webReports that are
                        # used to enable Content Aviator in KINI database table:
                        if self._aviator_enabled:
                            self._log_header_callback("Process Workspace Aviators")
                            self.process_workspace_aviators()
                    case "bulkDatasources":
                        # this is here just to avoid an error in catch all below
                        # the bulkDatasources dictionary will be processed in
                        # the other bulk* sections
                        pass
                    case "bulkWorkspaces":
                        if not self._workspace_types:
                            self._log_header_callback("Process Workspace Types")
                            self.process_workspace_types()
                        self._log_header_callback("Process Bulk Workspaces")
                        self.process_bulk_workspaces()
                    case "bulkWorkspaceRelationships":
                        if not self._workspace_types:
                            self._log_header_callback("Process Workspace Types")
                            self.process_workspace_types()
                        self._log_header_callback("Process Bulk Workspace Relationships")
                        self.process_bulk_workspace_relationships()
                    case "bulkDocuments":
                        if not self._workspace_types:
                            self._log_header_callback("Process Workspace Types")
                            self.process_workspace_types()
                        self._log_header_callback("Process Bulk Documents")
                        self.process_bulk_documents()
                    case "bulkItems":
                        if not self._workspace_types:
                            self._log_header_callback("Process Workspace Types")
                            self.process_workspace_types()
                        self._log_header_callback("Process Bulk Items")
                        self.process_bulk_items()
                    case "bulkClassifications":
                        self._log_header_callback("Process Bulk Classifications")
                        self.process_bulk_classifications()
                    case "sapRFCs":
                        if self._sap and isinstance(self._sap, SAP):
                            self._log_header_callback("Process SAP RFCs")
                            self.process_sap_rfcs(sap_rfcs=self._sap_rfcs)
                        else:
                            self.logger.warning(
                                "SAP RFC in payload but SAP external system is not configured or not enabled! RFCs will not be processed.",
                            )
                    case "sapRFCsPost":
                        if self._sap and isinstance(self._sap, SAP):
                            self._log_header_callback("Process SAP RFCs (post)")
                            self.process_sap_rfcs(
                                sap_rfcs=self._sap_rfcs_post,
                                section_name="sapRFCsPost",
                            )
                        else:
                            self.logger.warning(
                                "SAP RFC in payload but SAP external system is not configured or not enabled! RFCs will not be processed.",
                            )
                    case "webReports":
                        self._log_header_callback(text="Process Web Reports")
                        restart_required = self.process_web_reports(
                            web_reports=self._web_reports,
                        )
                        if restart_required:
                            self.logger.info(
                                "Web Reports require a restart of OTCS services...",
                            )
                            # Restart OTCS frontend and backend pods:
                            self._otcs_restart_callback(
                                backend=self._otcs_backend,
                                frontend=self._otcs_frontend,
                            )
                    case "webReportsPost":
                        self._log_header_callback(text="Process Web Reports (post)")
                        restart_required = self.process_web_reports(
                            web_reports=self._web_reports_post,
                            section_name="webReportsPost",
                        )
                        if restart_required:
                            self.logger.info(
                                "WebReports (Post) require a restart of OTCS services...",
                            )
                            # Restart OTCS frontend and backend pods:
                            self._otcs_restart_callback(
                                backend=self._otcs_backend,
                                frontend=self._otcs_frontend,
                            )
                    case "additionalGroupMemberships":
                        self._log_header_callback(
                            text="Process additional group members for OTDS",
                        )
                        self.process_additional_group_members()
                    case "additionalAccessRoleMemberships":
                        self._log_header_callback(
                            text="Process additional access role members for OTDS",
                        )
                        self.process_additional_access_role_members()
                    case "additionalApplicationRoleAssignments":
                        self._log_header_callback(
                            text="Process additional application role assignments for OTDS",
                        )
                        self.process_additional_application_role_assignments()
                    case "renamings":
                        self._log_header_callback(text="Process Node Renamings")
                        self.process_renamings()
                    case "items":
                        self._log_header_callback(text="Process Items")
                        self.process_items(items=self._items)
                    case "itemsPost":
                        self._log_header_callback(text="Process Items (post)")
                        self.process_items(items=self._items_post, section_name="itemsPost")
                    case "permissions":
                        self._log_header_callback(text="Process Permissions")
                        self.process_permissions(permissions=self._permissions)
                    case "permissionsPost":
                        self._log_header_callback(text="Process Permissions (post)")
                        self.process_permissions(
                            permissions=self._permissions_post,
                            section_name="permissionsPost",
                        )
                    case "workspacePermissions":
                        self._log_header_callback(text="Process Workspace Permissions")
                        self.process_workspace_permissions()
                    case "assignments":
                        self._log_header_callback(text="Process Assignments")
                        self.process_assignments()
                    case "securityClearances":
                        self._log_header_callback(text="Process Security Clearances")
                        self.process_security_clearances()
                    case "supplementalMarkings":
                        self._log_header_callback(text="Process Supplemental Markings")
                        self.process_supplemental_markings()
                    case "recordsManagementSettings":
                        self._log_header_callback(
                            text="Process Records Management Settings",
                        )
                        self.process_records_management_settings()
                    case "holds":
                        self._log_header_callback(text="Process Records Management Holds")
                        self.process_holds()
                    case "documentGenerators":
                        # If a payload file (e.g. additional ones) does not have
                        # transportPackages then it can happen that the
                        # self._workspace_types is not yet initialized. As we need
                        # this structure for documentGenerators we initialize it here:
                        if not self._workspace_types:
                            self._log_header_callback(text="Process Workspace Types")
                            self.process_workspace_types()

                        self._log_header_callback(text="Process Document Generators")
                        self.process_document_generators()
                    case "workflowInitiations":
                        # If a payload file (e.g. additional ones) does not have
                        # transportPackages then it can happen that the
                        # self._workspace_types is not yet initialized. As we need
                        # this structure for workflowInitiations we initialize it here:
                        if not self._workspace_types:
                            self._log_header_callback(text="Process Workspace Types")
                            self.process_workspace_types()

                        self._log_header_callback(text="Process Workflow Initiations")
                        self.process_workflows()
                    case "browserAutomations":
                        self._log_header_callback(text="Process Browser Automations")
                        self.process_browser_automations(
                            browser_automations=self._browser_automations,
                        )
                    case "browserAutomationsPost":
                        self._log_header_callback(text="Process Browser Automations (post)")
                        self.process_browser_automations(
                            browser_automations=self._browser_automations_post,
                            section_name="browserAutomationsPost",
                        )
                    case "testAutomations":
                        self._log_header_callback(text="Process Test Automations")
                        self.process_browser_automations(
                            browser_automations=self._test_automations, section_name="testAutomations"
                        )
                    case "workspaceTypes":
                        if not self._workspace_types:
                            self._log_header_callback(text="Process Workspace Types")
                            self.process_workspace_types()
                    case "businessObjectTypes":
                        if not self._business_object_types:
                            self._log_header_callback(text="Process Business Object Types")
                            self.process_business_object_types()
                    case "nifi":
                        self._log_header_callback("Process Knowledge Discovery Nifi Flows")
                        self.process_nifi_flows()
                    case _:
                        self.logger.error(
                            "Illegal payload section name -> '%s' in payloadSections!",
                            payload_section["name"],
                        )
                payload_section_restart = payload_section.get("restart", False)
                if payload_section_restart:
                    self.logger.info(
                        "Payload section -> '%s' requests a restart of OTCS services...",
                        payload_section["name"],
                    )
                    # Restart OTCS frontend and backend pods:
                    self._otcs_restart_callback(
                        backend=self._otcs_backend,
                        frontend=self._otcs_frontend,
                    )
                # Avoid out of cycle message for bulkDatasources if it is
                # passed in the payload:
                elif payload_section["name"] != "bulkDatasources":
                    self.logger.info(
                        "Payload section -> '%s' does not require a restart of OTCS services.",
                        payload_section["name"],
                    )

        with tracer.start_as_current_span("process_payload_users"):
            if self._users and self._user_customization:
                self._log_header_callback("Process User Profile Photos")
                self.process_user_photos()
                if self._m365 and isinstance(self._m365, M365):
                    self._log_header_callback("Process M365 User Profile Photos")
                    self.process_user_photos_m365()
                if self._salesforce and isinstance(self._salesforce, Salesforce):
                    self._log_header_callback("Process Salesforce User Profile Photos")
                    self.process_user_photos_salesforce()
                if self._core_share and isinstance(self._core_share, CoreShare):
                    self._log_header_callback("Process Core Share User Profile Photos")
                    self.process_user_photos_core_share()
                self._log_header_callback("Process User Favorites and Profiles")
                self.process_user_favorites_and_profiles()
                self._log_header_callback("Process User Security")
                self.process_user_security()

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_web_hooks")
    def process_web_hooks(self, webhooks: list, section_name: str = "webHooks") -> bool:
        """Process Web Hooks in payload and do HTTP requests.

        Args:
            webhooks (list):
                The list of web hook payload settings.
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not webhooks:
            self.logger.info(
                "Payload section -> %s is empty. Skipping...",
                section_name,
            )
            return True

        # Even if this payload section has been processed successfully before we
        # want to call the webhook.

        success: bool = True

        for webhook in webhooks:
            url = webhook.get("url")

            # Check if element has been disabled in payload (enabled = false).
            # In this case we skip the element:
            enabled = webhook.get("enabled", True)

            if not enabled and not url:
                self.logger.info("Payload for Web Hook is disabled. Skipping...")
                continue
            if not url:
                self.logger.info("Web Hook does not have a url. Skipping...")
                success = False
                continue
            if not enabled:
                self.logger.info(
                    "Payload for Web Hook -> '%s' is disabled. Skipping...",
                    url,
                )
                continue

            description = webhook.get("description")

            method = webhook.get("method", "POST")

            payload = webhook.get("payload", {})

            headers = webhook.get("headers", {})

            if description:
                self.logger.info(
                    "Calling Web Hook -> %s: %s (%s)...",
                    method,
                    url,
                    description,
                )
            else:
                self.logger.info("Calling Web Hook -> %s: %s...", method, url)

            response = self._http_object.http_request(
                url=url,
                method=method,
                payload=payload,
                headers=headers,
                retries=webhook.get("retries", 0),
                wait_time=webhook.get("wait_time", 0),
            )
            if not response or not response.ok:
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=webhooks,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_resources")
    def process_resources(self, section_name: str = "resources") -> bool:
        """Process OTDS resources in payload and create them in OTDS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._resources:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for resource in self._resources:
            resource_name = resource.get("name")
            if not resource_name:
                self.logger.error("OTDS resource payload does not have a name! Skipping...")
                success = False
                continue

            # Check if element has been disabled in payload (enabled = false).
            # In this case we skip the element (default is enabled):
            if not resource.get("enabled", True):
                self.logger.info(
                    "Payload for OTDS Resource -> '%s' is disabled. Skipping...",
                    resource_name,
                )
                continue

            resource_description = resource.get("description", "")
            display_name = resource.get("display_name", "")
            additional_payload = resource.get("additional_payload", {})
            activate_resource = resource.get("activate", True)
            resource_id = resource.get("resource_id", None)
            allow_impersonation = resource.get("allow_impersonation", True)
            secret = resource.get("secret", None)

            # Check if Partition does already exist
            # (in an attempt to make the code idem-potent)
            self.logger.info(
                "Check if OTDS resource -> '%s' does already exist...",
                resource_name,
            )
            response = self._otds.get_resource(name=resource_name, show_error=False)
            if response:
                self.logger.info(
                    "OTDS Resource -> '%s' does already exist. Skipping...",
                    resource_name,
                )
                continue

            # Only continue if Partition does not exist already
            self.logger.info(
                "Resource -> '%s' does not exist. Creating...",
                resource_name,
            )

            response = self._otds.add_resource(
                name=resource_name,
                description=resource_description,
                display_name=display_name,
                allow_impersonation=allow_impersonation,
                resource_id=resource_id,
                secret=secret,
                additional_payload=additional_payload,
            )
            if response:
                self.logger.info("Added OTDS resource -> '%s'.", resource_name)
            else:
                self.logger.error("Failed to add OTDS resource -> '%s'!", resource_name)
                success = False
                continue

            # If resource_id and secret are provided then the resource will
            # automatically be activated.
            if activate_resource and not secret:
                resource_id = response["resourceID"]
                self.logger.info(
                    "Activate OTDS resource -> '%s' with ID -> %s...",
                    resource_name,
                    resource_id,
                )
                response = self._otds.activate_resource(resource_id=resource_id)

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._resources,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_synchronized_partitions")
    def process_synchronized_partitions(
        self,
        section_name: str = "synchronizedPartitions",
    ) -> bool:
        """Process OTDS synchronized partitions in payload and create them in OTDS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        # check if section present, if not return True
        if not self._synchronized_partitions:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True
        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success = True

        for partition in self._synchronized_partitions:
            partition_name = partition["spec"].get("profileName") if "spec" in partition else None
            if not partition_name:
                self.logger.error(
                    "Synchronized partition does not have a profile name! Skipping...",
                )
                success = False
                continue

            # Check if partition has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not partition.get("enabled", True):
                self.logger.info(
                    "Payload for synchronized partition -> '%s' is disabled. Skipping...",
                    partition_name,
                )
                continue

            partition_description = partition["spec"].get("description", "")

            # Check if Partition does already exist
            # (in an attempt to make the code idem-potent)
            self.logger.info(
                "Check if OTDS synchronized partition -> '%s' does already exist...",
                partition_name,
            )
            response = self._otds.get_partition(name=partition_name, show_error=False)
            if response:
                self.logger.info(
                    "Synchronized partition -> '%s' does already exist.",
                    partition_name,
                )
            else:
                # Only continue if synchronized Partition does not exist already
                self.logger.info(
                    "Synchronized partition -> '%s' does not yet exist. Creating...",
                    partition_name,
                )

                response = self._otds.add_synchronized_partition(
                    name=partition_name,
                    description=partition_description,
                    data=partition["spec"],
                )
                if response:
                    self.logger.info(
                        "Added synchronized partition -> '%s' to OTDS.",
                        partition_name,
                    )
                else:
                    self.logger.error(
                        "Failed to add synchronized partition -> '%s' to OTDS!",
                        partition_name,
                    )
                    success = False
                    continue

            result = self._otds.import_synchronized_partition_members(
                name=partition_name,
            )
            if result:
                self.logger.info(
                    "Successfully imported members to OTDS synchronized partition -> '%s'.",
                    partition_name,
                )
            else:
                self.logger.error(
                    "Failed to import members to OTDS synchronized partition -> '%s'!",
                    partition_name,
                )
                success = False

            access_role = partition.get("access_role")
            if access_role:
                response = self._otds.add_partition_to_access_role(
                    access_role=access_role,
                    partition=partition_name,
                )
                if response:
                    self.logger.info(
                        "Added OTDS synchronized partition -> '%s' to access role -> '%s'.",
                        partition_name,
                        access_role,
                    )
                else:
                    self.logger.error(
                        "Failed to add OTDS synchronized partition -> '%s' to access role -> '%s'!",
                        partition_name,
                        access_role,
                    )
                    success = False
            # end if access_role

            # Partions may have an optional list of licenses in
            # the payload. Assign the partition to all these licenses:
            partition_specific_licenses = partition.get("licenses")
            if partition_specific_licenses:
                # We assume these licenses are Extended ECM licenses!
                otcs_resource_name = self._otcs.config()["resource"]
                otcs_resource = self._otds.get_resource(name=otcs_resource_name)
                if not otcs_resource:
                    self.logger.error(
                        "Cannot find OTCS resource -> '%s'! Skipping...",
                        otcs_resource_name,
                    )
                    success = False
                    continue
                otcs_resource_id = otcs_resource["resourceID"]
                otcs_license_name = "EXTENDED_ECM"
                for license_item in partition_specific_licenses:
                    if isinstance(license_item, dict):
                        license_feature = license_item.get("feature")
                        license_name = license_item.get("name", "EXTENDED_ECM")
                        if "enabled" in license_item and not license_item["enabled"]:
                            self.logger.info(
                                "Payload for License '%s' -> '%s' is disabled. Skipping...",
                                license_name,
                                license_feature,
                            )
                            continue
                        if "resource" in license_item:
                            try:
                                resource_id = self._otds.get_resource(name=license_item["resource"])["resourceID"]
                            except Exception:
                                self.logger.error(
                                    "Error getting resource ID from resource -> %s", license_item["resource"]
                                )
                        else:
                            resource_id = otcs_resource_id

                    elif isinstance(license_item, str):
                        license_feature = license_item
                        resource_id = otcs_resource_id
                        license_name = otcs_license_name
                    else:
                        self.logger.error("Invalid License feature specified -> %s!", license_item)
                        success = False
                        continue

                    if self._otds.is_partition_licensed(
                        partition_name=partition_name,
                        resource_id=resource_id,
                        license_feature=license_feature,
                        license_name=license_name,
                    ):
                        self.logger.info(
                            "Partition -> '%s' is already licensed for -> '%s' (%s).",
                            partition_name,
                            license_name,
                            license_feature,
                        )
                        continue

                    assigned_license = self._otds.assign_partition_to_license(
                        partition_name=partition_name,
                        resource_id=resource_id,
                        license_feature=license_feature,
                        license_name=license_name,
                    )

                    if not assigned_license:
                        self.logger.error(
                            "Failed to assign synchronized partition -> '%s' to license feature -> '%s' of license -> '%s'!",
                            partition_name,
                            license_feature,
                            license_name,
                        )
                        success = False
                    else:
                        self.logger.info(
                            "Successfully assigned synchronized partition -> '%s' to license feature -> '%s' of license -> '%s'.",
                            partition_name,
                            license_feature,
                            license_name,
                        )
            # end if partition_specific_licenses

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._synchronized_partitions,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_partitions")
    def process_partitions(self, section_name: str = "partitions") -> bool:
        """Process OTDS partitions in payload and create them in OTDS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise

        """

        if not self._partitions:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for partition in self._partitions:
            partition_name = partition.get("name")
            if not partition_name:
                self.logger.error("Partition does not have a name in payload! Skipping...")
                success = False
                continue

            # Check if partition has explicitly been disabled in payload
            # (enabled = false). In this case we skip the element:
            if not partition.get("enabled", True):
                self.logger.info(
                    "Payload for partition -> '%s' is disabled. Skipping...",
                    partition_name,
                )
                continue

            partition_description = partition.get("description", "")

            # Check if Partition does already exist (to make the code idem-potent):
            self.logger.info(
                "Check if OTDS partition -> '%s' does already exist...",
                partition_name,
            )
            response = self._otds.get_partition(name=partition_name, show_error=False)
            if response:
                self.logger.info(
                    "Partition -> '%s' does already exist.",
                    partition_name,
                )
            else:
                # Only continue if Partition does not exist already
                self.logger.info(
                    "Partition -> '%s' does not yet exist. Creating...",
                    partition_name,
                )

                response = self._otds.add_partition(
                    name=partition_name,
                    description=partition_description,
                )
                if response:
                    self.logger.info(
                        "Added OTDS partition -> '%s'%s.",
                        partition_name,
                        " ({})".format(partition_description) if partition_description else "",
                    )
                else:
                    self.logger.error(
                        "Failed to add OTDS partition -> '%s'!",
                        partition_name,
                    )
                    success = False
                    continue

            access_role = partition.get("access_role")
            if access_role:
                response = self._otds.add_partition_to_access_role(
                    access_role=access_role,
                    partition=partition_name,
                )
                if response:
                    self.logger.info(
                        "Added OTDS partition -> '%s' to access role -> '%s'.",
                        partition_name,
                        access_role,
                    )
                else:
                    self.logger.error(
                        "Failed to add OTDS partition -> '%s' to access role -> '%s'!",
                        partition_name,
                        access_role,
                    )
                    success = False
            # end if access_role

            # Partions may have an optional list of licenses in
            # the payload. Assign the partition to all these licenses:
            partition_specific_licenses = partition.get("licenses")
            if partition_specific_licenses:
                # We assume these licenses are Extended ECM licenses!
                otcs_resource_name = self._otcs.config()["resource"]
                otcs_resource = self._otds.get_resource(name=otcs_resource_name)
                if not otcs_resource:
                    self.logger.error(
                        "Cannot find OTCS resource -> '%s'!",
                        otcs_resource_name,
                    )
                    success = False
                    continue
                otcs_resource_id = otcs_resource["resourceID"]
                otcs_license_name = "EXTENDED_ECM"
                for license_item in partition_specific_licenses:
                    if isinstance(license_item, dict):
                        license_feature = license_item.get("feature")
                        license_name = license_item.get("name", "EXTENDED_ECM")
                        if "enabled" in license_item and not license_item["enabled"]:
                            self.logger.info(
                                "Payload for License '%s' -> '%s' is disabled. Skipping...",
                                license_name,
                                license_feature,
                            )
                            continue

                        if "resource" in license_item:
                            try:
                                resource_id = self._otds.get_resource(name=license_item["resource"])["resourceID"]
                            except Exception:
                                self.logger.error(
                                    "Error getting resource ID from resource -> '%s'!", license_item["resource"]
                                )
                        else:
                            resource_id = otcs_resource_id

                    elif isinstance(license_item, str):
                        license_feature = license_item
                        resource_id = otcs_resource_id
                        license_name = otcs_license_name
                    else:
                        self.logger.error("Invalid License feature specified -> %s!", license_item)
                        success = False
                        continue

                    if self._otds.is_partition_licensed(
                        partition_name=partition_name,
                        resource_id=resource_id,
                        license_feature=license_feature,
                        license_name=license_name,
                    ):
                        self.logger.info(
                            "Partition -> '%s' is already licensed for -> '%s' (%s).",
                            partition_name,
                            license_name,
                            license_feature,
                        )
                        continue

                    assigned_license = self._otds.assign_partition_to_license(
                        partition_name=partition_name,
                        resource_id=resource_id,
                        license_feature=license_feature,
                        license_name=license_name,
                    )

                    if not assigned_license:
                        self.logger.error(
                            "Failed to assign partition -> '%s' to license feature -> '%s' of license -> '%s'!",
                            partition_name,
                            license_feature,
                            license_name,
                        )
                        success = False
                    else:
                        self.logger.info(
                            "Successfully assigned partition -> '%s' to license feature -> '%s' of license -> '%s'.",
                            partition_name,
                            license_feature,
                            license_name,
                        )
            # end if partition_specific_licenses:
        # end for partition in self._partitions:

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._partitions,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_licenses")
    def process_licenses(self, section_name: str = "licenses") -> bool:
        """Process OTDS licenses in payload and create them in OTDS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise

        """

        if not self._licenses:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for lic in self._licenses:
            self.logger.debug("Start processing License -> '%s'", lic)
            if not lic.get("enabled", True):
                self.logger.info(
                    "Payload for License -> '%s' is disabled. Skipping...",
                    lic,
                )
                continue

            path = lic.get("path")
            if not path:
                self.logger.error("Required attribute path not specified (%s)! Skipping ...", lic)
                success = False
                continue

            product_name = lic.get("product_name")
            if not product_name:
                self.logger.error("Required attribute 'product_name' not specified (%s)! Skipping ...", lic)
                success = False
                continue

            if "resource" in lic:
                try:
                    resource_id = self._otds.get_resource(name=lic["resource"])["resourceID"]
                except Exception:
                    self.logger.error("Error getting resource ID from resource -> '%s'!", lic["resource"])
                    success = False
                    continue
            else:
                self.logger.error("Required attribute 'resource' not specified (%s)! Skipping ...", lic)
                success = False
                continue

            self.logger.info(
                "Adding license -> '%s' for product -> '%s' to resource -> '%s'...", path, product_name, resource_id
            )

            add_license = self._otds.add_license_to_resource(
                path_to_license_file=path,
                product_name=product_name,
                product_description=lic.get("description", ""),
                resource_id=resource_id,
                update=lic.get("update", True),
            )

            if not add_license:
                success = False
                continue

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._licenses,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_oauth_clients")
    def process_oauth_clients(self, section_name: str = "oauthClients") -> bool:
        """Process OTDS OAuth clients in payload and create them in OTDS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._oauth_clients:
            self.logger.info("Payload section -> %s is empty. Skipping...", section_name)
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for oauth_client in self._oauth_clients:
            client_name = oauth_client.get("name")
            if not client_name:
                self.logger.error("OAuth client does not have a name in payload. Skipping...")
                success = False
                continue

            # Check if oauth client has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not oauth_client.get("enabled", True):
                self.logger.info(
                    "Payload for OAuth client -> '%s' is disabled in payload. Skipping...",
                    client_name,
                )
                continue

            client_description = oauth_client.get("description", "")
            client_confidential = oauth_client.get("confidential", True)
            client_partition = oauth_client.get("partition", "Global")
            if client_partition == "Global":
                client_partition = []
            client_redirect_urls = oauth_client.get("redirect_urls", [])
            client_permission_scopes = oauth_client.get("permission_scopes")
            client_default_scopes = oauth_client.get("default_scopes")
            client_allow_impersonation = oauth_client.get("allow_impersonation", True)
            client_secret = oauth_client.get("secret", "")

            # Check if OAuth client does already exist
            # (in an attempt to make the code idem-potent)
            self.logger.info(
                "Check if OTDS OAuth client -> '%s' does already exist...",
                client_name,
            )
            response = self._otds.get_oauth_client(
                client_id=client_name,
                show_error=False,
            )
            if response:
                self.logger.info(
                    "OAuth client -> '%s' does already exist. Skipping...",
                    client_name,
                )
                continue
            self.logger.info(
                "OAuth client -> '%s' does not exist. Creating...",
                client_name,
            )

            response = self._otds.add_oauth_client(
                client_id=client_name,
                description=client_description,
                redirect_urls=client_redirect_urls,
                allow_impersonation=client_allow_impersonation,
                confidential=client_confidential,
                auth_scopes=client_partition,
                allowed_scopes=client_permission_scopes,
                default_scopes=client_default_scopes,
                secret=client_secret,
            )
            if response:
                self.logger.info("Added OTDS OAuth client -> '%s'.", client_name)
            else:
                self.logger.error(
                    "Failed to add OTDS OAuth client -> '%s'!",
                    client_name,
                )
                success = False
                continue

            # in case the secret has not been provided in the payload we retrieve
            # the automatically created secret:
            client_secret = response.get("secret")
            if not client_secret:
                self.logger.error(
                    "OAuth client -> '%s' does not have a secret!",
                    client_name,
                )
                continue

            client_description += " Client Secret: " + str(client_secret)
            response = self._otds.update_oauth_client(
                client_id=client_name,
                updates={"description": client_description},
            )
            # Write the secret back into the payload
            oauth_client["secret"] = client_secret

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._oauth_clients,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_application_roles")
    def process_application_roles(self, section_name: str = "applicationRoles") -> bool:
        """Process OTDS application rolesin payload and create them in OTDS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._application_roles:
            self.logger.info("Payload section -> %s is empty. Skipping...", section_name)
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for role in self._application_roles:
            role_name = role.get("name")
            if not role_name:
                self.logger.error("Application role does not have a name in payload! Skipping...")
                success = False
                continue

            # Check if application role has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not role.get("enabled", True):
                self.logger.info(
                    "Payload for application roles -> '%s' is disabled. Skipping...",
                    role_name,
                )
                continue

            role_description = role.get("description")
            role_partition = role.get("partition", "OAuthClients")

            # Check if application role does already exist
            # (in an attempt to make the code idem-potent)
            self.logger.info(
                "Check if application role -> '%s' does already exist in partition -> '%s'...",
                role_name,
                role_partition,
            )
            response = self._otds.get_application_role(
                name=role,
                partition=role_partition,
                show_error=False,
            )
            if response:
                self.logger.info(
                    "Application role -> '%s' does already exist in partition -> '%s'. Skipping...",
                    role_name,
                    role_partition,
                )
                continue
            self.logger.info(
                "Application role -> '%s' does not exist in partition -> '%s'. Creating...", role_name, role_partition
            )

            response = self._otds.add_application_role(
                name=role_name,
                partition_id=role_partition,
                description=role_description,
                values=role.get("values", None),
                custom_attributes=role.get("custom_attributes", None),
            )
            if response:
                self.logger.info(
                    "Successfully added OTDS Application role -> '%s' to partition -> '%s'.", role_name, role_partition
                )
            else:
                self.logger.error(
                    "Failed to add OTDS Application role -> '%s' to partition -> '%s'!", role_name, role_partition
                )
                success = False
                continue

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._application_roles,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_auth_handlers")
    def process_auth_handlers(self, section_name: str = "authHandlers") -> bool:
        """Process OTDS authorization handlers in payload and create them in OTDS.

        An authorization handler defined the connection to an Identity Provider (IdP).

        The payload section is a list of dicts with these items:
        {
            enabled (bool):
                True or False to enable or disable the payload item
            name (str):
                Name of the authorization handler. This is shown in the first
                column of the Auth Handler list in OTDS.
            description (str):
                Description of the handler. This is shown in the second
                column of the Auth Handler
            type (str):
                Type of the handler. Possible values are SALM, SAP, OAUTH
            priority (int):
                A numeric value to order different handlers in OTDS by priority
            active_by_default (bool):
                Whether to activate this handler for any request to the
                OTDS login page. If True, any login request to the OTDS
                login page will be redirected to the IdP. If false, the
                user has to select the provider on the login page.
            provider_name:
                The name of the identity provider. This should be a single word
                since it will be part of the metadata URL. This is what is
                shown as a button on the OTDS login page.
            auth_principal_attributes:
                Authentication principal attributes (list)
            nameid_format:
                Specifies which NameID format supported by the identity provider
                contains the desired user identifier. The value in this identifier
                must correspond to the value of the user attribute specified for the
                authentication principal attribute.
            saml_url:
                Required for SAML Authentication Handler. The URL for the IdP's federation metadata.
            otds_sp_endpoint:
                Used for SAML Authentication Handler. Specifies the service provider URL that will
                be used to identify OTDS to the identity provider.
            certificate_file:
                Required for SAP Authentication Handler (SAPSSOEXT).
                Fully qualified file name (with path) to the certificate file (URI)
            certificate_password:
                Required for SAP Authentication Handler (SAPSSOEXT).
                Password of the certificate file.
            client_id:
                Client ID. Required for OAUTH authentication handler.
            client_secret:
                Client Secret. Required for OAUTH authentication handler.
            authorization_endpoint:
                Required for OAUTH authentication handler.
                The URL to redirect the browser to for authentication.
                It is used to retrieve the authorization code or an OIDC id_token.
            token_endpoint:
                Used for OAUTH authentication handler. The URL from which to retrieve the access token.
                Not strictly required with OpenID Connect if using the implicit flow.
            scope_string:
                Used for OAUTH authentication handler. Space delimited scope values to send.
                Include 'openid' to use OpenID Connect.
        }

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._auth_handlers:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for auth_handler in self._auth_handlers:
            handler_name = auth_handler.get("name")

            if not handler_name:
                self.logger.error("Auth handler does not have a name in payload. Skipping...")
                success = False
                continue

            # Check if Auth Handler does already exist (e.g. after a restart of
            # the customizer pod):
            if self._otds.get_auth_handler(name=handler_name, show_error=False):
                self.logger.info(
                    "Auth handler -> '%s' does already exist. Skipping...",
                    handler_name,
                )
                continue

            handler_description = auth_handler.get("description")

            # Check if auth handler has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not auth_handler.get("enabled", True):
                self.logger.info(
                    "Payload for OTDS authorization handler -> '%s' is disabled. Skipping...",
                    handler_name,
                )
                continue

            handler_scope = auth_handler.get("scope")
            if not handler_scope:
                # Make sure to pass None also if scope is empty string
                handler_scope = None

            handler_type = auth_handler.get("type")
            if not handler_type:
                self.logger.error(
                    "OTDS authorization handler does not have a type in payload! Skipping...",
                )
                success = False
                continue

            priority = auth_handler.get("priority")
            active_by_default = auth_handler.get("active_by_default")
            if not active_by_default:
                active_by_default = False

            match handler_type:
                case "SAML":
                    provider_name = auth_handler.get("provider_name")
                    if not provider_name:
                        self.logger.error(
                            "SAML authorization handler needs a provider name in payload! Skipping...",
                        )
                        success = False
                        continue
                    saml_url = auth_handler.get("saml_url")
                    if not saml_url:
                        self.logger.error(
                            "SAML authorization handler needs a SAML URL in payload! Skipping...",
                        )
                        success = False
                        continue
                    otds_sp_endpoint = auth_handler.get("otds_sp_endpoint")
                    if not otds_sp_endpoint:
                        self.logger.error(
                            "SAML authorization handler needs a OTDS SP endpoint in payload! Skipping...",
                        )
                        success = False
                        continue
                    auth_principal_attributes = auth_handler.get(
                        "auth_principal_attributes",
                    )
                    nameid_format = auth_handler.get("nameid_format")
                    response = self._otds.add_auth_handler_saml(
                        name=handler_name,
                        description=handler_description,
                        scope=handler_scope,
                        provider_name=provider_name,
                        saml_url=saml_url,
                        otds_sp_endpoint=otds_sp_endpoint,
                        priority=priority,
                        active_by_default=active_by_default,
                        auth_principal_attributes=auth_principal_attributes,
                        nameid_format=nameid_format,
                    )
                case "SAP":
                    certificate_file = auth_handler.get("certificate_file")
                    if not certificate_file:
                        self.logger.error(
                            "SAP Authorization handler -> '%s' (%s) requires a certificate file name in payload! Skipping...",
                            handler_name,
                            handler_type,
                        )
                        success = False
                        continue
                    certificate_password = auth_handler.get("certificate_password")
                    if not certificate_password:
                        # This is not an error - we canhave this key with empty string!
                        self.logger.info(
                            "SAP Authorization handler -> '%s' (%s) does not have a certificate password - this can be OK.",
                            handler_name,
                            handler_type,
                        )
                    response = self._otds.add_auth_handler_sap(
                        name=handler_name,
                        description=handler_description,
                        scope=handler_scope,
                        certificate_file=certificate_file,
                        certificate_password=certificate_password,
                        priority=priority,
                    )
                case "OAUTH":
                    provider_name = auth_handler.get("provider_name")
                    if not provider_name:
                        self.logger.error(
                            "OAUTH Authorization handler -> '%s' (%s) requires a provider name in payload! Skipping...",
                            handler_name,
                            handler_type,
                        )
                        success = False
                        continue
                    client_id = auth_handler.get("client_id")
                    if not client_id:
                        self.logger.error(
                            "OAUTH Authorization handler -> '%s' (%s) requires a client ID in payload! Skipping...",
                            handler_name,
                            handler_type,
                        )
                        success = False
                        continue
                    client_secret = auth_handler.get("client_secret")
                    if not client_secret:
                        self.logger.error(
                            "OAUTH Authorization handler -> '%s' (%s) requires a client secret in payload! Skipping...",
                            handler_name,
                            handler_type,
                        )
                        success = False
                        continue
                    authorization_endpoint = auth_handler.get("authorization_endpoint")
                    if not authorization_endpoint:
                        self.logger.error(
                            "OAUTH Authorization handler -> '%s' (%s) requires a authorization endpoint in payload! Skipping...",
                            handler_name,
                            handler_type,
                        )
                        success = False
                        continue
                    token_endpoint = auth_handler.get("token_endpoint")
                    if not token_endpoint:
                        self.logger.warning(
                            "OAUTH Authorization handler -> '%s' (%s) does not have a token endpoint!",
                            handler_name,
                            handler_type,
                        )
                    scope_string = auth_handler.get("scope_string")
                    response = self._otds.add_auth_handler_oauth(
                        name=handler_name,
                        description=handler_description,
                        scope=handler_scope,
                        provider_name=provider_name,
                        client_id=client_id,
                        client_secret=client_secret,
                        priority=priority,
                        active_by_default=active_by_default,
                        authorization_endpoint=authorization_endpoint,
                        token_endpoint=token_endpoint,
                        scope_string=scope_string,
                    )
                case _:
                    self.logger.error(
                        "Unsupported authorization handler type -> '%s'!",
                        handler_type,
                    )
                    return False

            if response:
                self.logger.info(
                    "Successfully added OTDS authorization handler -> '%s' (%s).",
                    handler_name,
                    handler_type,
                )
            else:
                self.logger.error(
                    "Failed to add OTDS authorization handler -> '%s' (%s)!",
                    handler_name,
                    handler_type,
                )
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._auth_handlers,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_trusted_sites")
    def process_trusted_sites(self, section_name: str = "trustedSites") -> bool:
        """Process OTDS trusted sites in payload and create them in OTDS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise

        """

        if not self._trusted_sites:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for trusted_site in self._trusted_sites:
            # old payload may still have trusted sites as list of string
            # we changed also the trusted sites to dict with 23.3
            # We want to be backwards compatible so we handle both cases:
            url = None
            if isinstance(trusted_site, dict):
                url = trusted_site.get("url")
            elif isinstance(trusted_site, str):
                url = trusted_site

            # Check if element has been disabled in payload (enabled = false).
            # In this case we skip the element:
            if isinstance(trusted_site, dict) and "enabled" in trusted_site and not trusted_site["enabled"]:
                self.logger.info(
                    "Payload for OTDS Trusted Site -> '%s' is disabled. Skipping...",
                    url if url else "<undefined>",
                )
                continue

            if not url:
                self.logger.error("OTDS Trusted site does not have a URL! Skipping...")
                success = False
                continue

            response = self._otds.add_trusted_site(trusted_site=url)
            if response:
                self.logger.info("Added OTDS trusted site -> '%s'.", url)
            else:
                self.logger.error("Failed to add trusted site -> '%s'!", url)
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._trusted_sites,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_system_attributes")
    def process_system_attributes(self, section_name: str = "systemAttributes") -> bool:
        """Process OTDS system attributes in payload and create them in OTDS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._system_attributes:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for system_attribute in self._system_attributes:
            attribute_name = system_attribute.get("name")
            if not attribute_name:
                self.logger.error("OTDS system attribute needs a name in payload! Skipping...")
                success = False
                continue

            # Check if system attribute has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not system_attribute.get("enabled", True):
                self.logger.info(
                    "Payload for OTDS system attribute -> '%s' is disabled. Skipping...",
                    attribute_name,
                )
                continue

            attribute_value = system_attribute.get("value")
            if not attribute_value:
                self.logger.error(
                    "OTDS system attribute -> '%s' needs a value in payload! Skipping...",
                    attribute_name,
                )
                continue

            attribute_description = system_attribute.get("description", "")

            # Add the system attribute to OTDS:
            response = self._otds.add_system_attribute(
                name=attribute_name,
                value=attribute_value,
                description=attribute_description,
            )
            if response:
                self.logger.info(
                    "Added OTDS system attribute -> '%s' with value -> '%s'.",
                    attribute_name,
                    attribute_value,
                )
            else:
                self.logger.error(
                    "Failed to add OTDS system attribute -> '%s' with value -> '%s'!",
                    attribute_name,
                    attribute_value,
                )
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._system_attributes,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_docgen_settings")
    def process_docgen_settings(self, section_name: str = "docgenSettings") -> bool:
        """Process OTPD settings in payload and configure them in OTPD.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._docgen_settings:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for docgen_setting in self._docgen_settings:
            # Check if the setting has a proper name in the payload:
            setting_name = docgen_setting.get("name")
            if not setting_name:
                self.logger.error(
                    "OTPD document generation setting needs a name in payload! Skipping...",
                )
                success = False
                continue

            # Check if the document geneneration has been explicitly disabled in payload
            # (enabled = false). In this case we skip the payload element:
            if not docgen_setting.get("enabled", True):
                self.logger.info(
                    "Payload for OTPD setting -> '%s' is disabled. Skipping...",
                    setting_name,
                )
                continue

            setting_value = docgen_setting.get("value")
            if not setting_value:
                self.logger.error(
                    "OTPD setting -> '%s' needs a value in payload! Skipping...",
                    setting_name,
                )
                continue

            tenant_name = docgen_setting.get("tenant", None)
            description = docgen_setting.get("description", "")

            # Apply the setting to PowerDocs (OTPD):
            response = self._otpd.apply_setting(
                setting_name=setting_name,
                setting_value=setting_value,
                tenant_name=tenant_name,
            )
            if response:
                self.logger.info(
                    "Added OTPD setting -> '%s' with value -> '%s'%s%s.",
                    setting_name,
                    setting_value,
                    " for tenant -> '{}'".format(tenant_name) if tenant_name else "",
                    " ({})".format(description) if description else "",
                )
            else:
                self.logger.error(
                    "Failed to configure OTPD setting -> '%s' with value -> '%s'%s%s!",
                    setting_name,
                    setting_value,
                    " for tenant -> '{}'".format(tenant_name) if tenant_name else "",
                    " ({})".format(description) if description else "",
                )
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._docgen_settings,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_group_placeholders")
    def process_group_placeholders(self) -> None:
        """Replace a group placeholder (sourrounded by %%...%%) with the actual ID of the Content Server group.

        For this we prepare a lookup dict. The dict self._placeholder_values already includes
        lookups for the OTCS and OTAWP OTDS resource IDs (see main.py)
        """

        for group in self._groups:
            group_name = group.get("name")
            if not group_name:
                self.logger.error(
                    "Group needs a name for placeholder definition in payload! Skipping...",
                )
                continue

            # Check if group has been explicitly disabled in payload
            # (enabled = false). In this case we skip the payload element:
            if not group.get("enabled", True):
                self.logger.info(
                    "Payload for group -> '%s' is disabled. Skipping...",
                    group_name,
                )
                continue

            # Now we determine the ID. Either it is in the payload section from
            # the current customizer run or we try to look it up in the system.
            # The latter case may happen if the customizer pod got restarted.
            group_id = self.determine_group_id(group=group)
            if not group_id:
                self.logger.warning(
                    "Group needs an ID for placeholder definition. Skipping...",
                )
                continue

            # Add Group with its ID to the dict self._placeholder_values:
            self._placeholder_values["OTCS_GROUP_ID_" + group_name.upper().replace(" & ", "_").replace(" ", "_")] = str(
                group_id,
            )

        self.logger.debug(
            "Placeholder values after group processing = %s",
            self._placeholder_values,
        )

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_placeholders")
    def process_user_placeholders(self) -> None:
        """Replace a user placeholder (sourrounded by %%...%%) with the ID of the Content Server user.

        For this we prepare a lookup dict. The dict self._placeholder_values already includes
        lookups for the OTCS and OTAWP OTDS resource IDs (see customizer.py).
        """

        for user in self._users:
            user_name = user.get("name")
            if not user_name:
                self.logger.error(
                    "User needs a name for placeholder definition in payload! Skipping...",
                )
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the payload element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            # Now we determine the ID. Either it is in the payload section from
            # the current customizer run or we try to look it up in the system.
            # The latter case may happen if the customizer pod got restarted.
            user_id = self.determine_user_id(user=user)
            if not user_id:
                self.logger.warning(
                    "User needs an ID for placeholder definition. Skipping...",
                )
                continue

            # Add user with its ID to the dict self._placeholder_values:
            self._placeholder_values["OTCS_USER_ID_%s", user_name.upper()] = str(
                user_id,
            )

        self.logger.debug(
            "Placeholder values after user processing = %s",
            str(self._placeholder_values),
        )

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_groups")
    def process_groups(self, section_name: str = "groups") -> bool:
        """Process groups in payload and create them in Content Server.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        Side Effects:
            The group items are modified by adding an "id" dict element that
            includes the technical ID of the group in Content Server

        """

        if not self._groups:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # First run through groups: create all groups in payload
        # and store the IDs of the created groups:
        for group in self._groups:
            group_name = group.get("name")
            if not group_name:
                self.logger.error("Group needs a name inb payload! Skipping...")
                success = False
                continue

            # Check if group has been explicitly disabled in payload
            # (enabled = false). In this case we skip the payload element:
            if not group.get("enabled", True):
                self.logger.info(
                    "Payload for group -> '%s' is disabled. Skipping...",
                    group_name,
                )
                continue

            # Check if the group does already exist (e.g. if job is restarted)
            group_id = self.determine_group_id(group=group)
            if group_id:
                self.logger.info(
                    "Found existing group -> '%s' (%s). Skipping to next group...",
                    group_name,
                    group_id,
                )
                continue

            # Now we know it is a new group...
            new_group = self._otcs.add_group(name=group_name)
            if new_group:
                new_group_id = self._otcs.get_result_value(response=new_group, key="id")
                self.logger.info(
                    "Successfully created group -> '%s' (%s).",
                    group_name,
                    new_group_id,
                )
                # Remember the OTCS group ID in the payload structure:
                group["id"] = new_group_id
            else:
                self.logger.error("Failed to create group -> '%s'!", group_name)
                success = False
                continue

            # Assign usage privileges to the new group:
            usage_privileges = group.get("usage_privileges", [])
            for usage_privilege in usage_privileges:
                response = self._otcs.assign_usage_privilege(
                    usage_privilege=usage_privilege,
                    member_id=new_group_id,
                )
                if response:
                    self.logger.info(
                        "Successfully assigned usage privilege -> '%s' to group -> '%s' (%s).",
                        usage_privilege,
                        group_name,
                        new_group_id,
                    )
                else:
                    self.logger.info(
                        "Failed to assign usage privilege -> '%s' to group -> '%s' (%s)!",
                        usage_privilege,
                        group_name,
                        new_group_id,
                    )

            # Assign usage privileges to the new group:
            object_privileges = group.get("object_privileges", [])
            for object_type in object_privileges:
                response = self._otcs.assign_object_privilege(
                    object_type=object_type,
                    member_id=new_group_id,
                )
                if response:
                    self.logger.info(
                        "Successfully assigned object privilege -> '%s' to group -> '%s' (%s).",
                        object_type,
                        group_name,
                        new_group_id,
                    )
                else:
                    self.logger.info(
                        "Failed to assign object privilege -> '%s' to group -> '%s' (%s)!",
                        object_type,
                        group_name,
                        new_group_id,
                    )

        # end for group in self._groups: (first run)

        # Second run through groups: create all group memberships
        # (nested groups) based on the IDs created in first run:
        for group in self._groups:
            group_name = group.get("name")
            group_id = group.get("id")  # this should have been set in the first loop
            if not group_id:
                self.logger.error(
                    "Group -> '%s' does not have an ID! Creation may have failed before. Skipping...", group_name
                )
                success = False
                continue
            parent_group_names = group.get("parent_groups", [])
            for parent_group_name in parent_group_names:
                # First, try to find parent group in payload by parent group name:
                parent_group = next(
                    (item for item in self._groups if item["name"] == parent_group_name),
                    None,
                )
                if parent_group is None:
                    # If this didn't work, try to get the parent group from OTCS. This covers
                    # cases where the parent group is system-generated or part
                    # of a former payload processing run (or part of another payload):
                    parent_group = self._otcs.get_group(name=parent_group_name)
                    parent_group_id = self._otcs.get_result_value(
                        response=parent_group,
                        key="id",
                    )
                    if not parent_group_id:
                        self.logger.error(
                            "Parent group -> '%s' not found! Skipping...",
                            parent_group_name,
                        )
                        success = False
                        continue
                elif "id" not in parent_group:
                    self.logger.error(
                        "Parent group -> '%s' does not have an ID! Cannot establish group nesting. Skipping...",
                        parent_group["name"],
                    )
                    success = False
                    continue
                else:  # we can read the ID from the
                    parent_group_id = parent_group["id"]

                # retrieve all members of the parent group (1 = get only groups)
                # to check if the current group is already a member in the parent group:
                members = self._otcs.get_group_members(
                    group=parent_group_id,
                    member_type=1,
                )
                if self._otcs.exist_result_item(
                    response=members,
                    key="id",
                    value=group["id"],
                ):
                    self.logger.info(
                        "Group -> '%s' (%s) is already a member of parent group -> '%s' (%s). Skipping to next parent group...",
                        group_name,
                        group_id,
                        parent_group_name,
                        parent_group_id,
                    )
                else:
                    self.logger.info(
                        "Add group -> '%s' (%s) to parent group -> '%s' (%s)...",
                        group_name,
                        group_id,
                        parent_group_name,
                        parent_group_id,
                    )
                    self._otcs.add_group_member(
                        member_id=group["id"],
                        group_id=parent_group_id,
                    )
            # end for parent_group_name in parent_group_names:

            # Assign application roles to the new group:
            application_roles = group.get("application_roles", [])
            if application_roles:
                self.logger.info(
                    "Group -> '%s' has application roles -> %s. Assigning...", group_name, str(application_roles)
                )
            for role in application_roles:
                group_partition = self._otcs.config()["partition"]
                if not group_partition:
                    self.logger.error("Group partition not found! Skipping application role -> %s...", str(role))
                    success = False
                    continue

                # Split role at the @ sign to identify Partitions
                role_parts = role.split("@", 1)
                role_name = role_parts[0]
                role_partition = role_parts[1] if len(role_parts) > 1 else "OAuthClients"

                # This is on OTDS method (not OTCS) thuis the group ID is the group name!
                response = self._otds.assign_group_to_application_role(
                    group_id=group_name,
                    group_partition=group_partition,
                    role_name=role_name,
                    role_partition=role_partition,
                )

                if response:
                    self.logger.info(
                        "Successfully assigned application role -> '%s' in partition -> '%s' to group -> '%s' (%s) in partition -> '%s'.",
                        role_name,
                        role_partition,
                        group_name,
                        group_id,
                        group_partition,
                    )
                else:
                    self.logger.info(
                        "Failed to assign application role -> '%s' in partition -> %s to group -> '%s' (%s) in partition -> '%s'!",
                        role_name,
                        role_partition,
                        group_name,
                        group_id,
                        group_partition,
                    )
            # end for role in application_roles:
        # end for group in self._groups: (second run)

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._groups,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_groups_m365")
    def process_groups_m365(self, section_name: str = "groupsM365") -> bool:
        """Process groups in payload and create them in Microsoft 365.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not isinstance(self._m365, M365):
            self.logger.error(
                "Microsoft 365 connection not setup properly. Skipping payload section '%s'...",
                section_name,
            )
            return False

        if not self._groups:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # First run through groups: create all groups in payload
        # and store the IDs of the created groups:
        for group in self._groups:
            group_name = group.get("name")
            if not group_name:
                self.logger.error("Group needs a name. Skipping...")
                success = False
                continue

            # Check if group has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not group.get("enabled", True):
                self.logger.info(
                    "Payload for group -> '%s' is disabled. Skipping...",
                    group_name,
                )
                continue
            # M365 is disabled per default. There needs to be "enable_o365" in payload
            # and it needs to be True:
            if not group.get("enable_o365", False):
                self.logger.info(
                    "Microsoft 365 is not enabled in payload for group -> '%s'. Skipping...",
                    group_name,
                )
                continue

            # Check if the group does already exist (e.g. if job is restarted)
            # as this is a pattern search it could return multiple groups:
            existing_groups = self._m365.get_group(group_name=group_name)

            if existing_groups and existing_groups["value"]:
                self.logger.debug(
                    "Found existing Microsoft 365 groups -> %s",
                    existing_groups["value"],
                )
                # Get list of all matching groups:
                existing_groups_list = existing_groups["value"]
                # Find the group with the exact match of the name:
                existing_group = next(
                    (item for item in existing_groups_list if item["displayName"] == group_name),
                    None,
                )
                # Have we found an exact match?
                if existing_group is not None:
                    self.logger.info(
                        "Found existing Microsoft 365 group -> '%s' (%s) - skip creation of group...",
                        existing_group["displayName"],
                        existing_group["id"],
                    )
                    # Write M365 group ID back into the payload (for the success file)
                    group["m365_id"] = existing_group["id"]
                    continue

            self.logger.info(
                "Creating a new Microsoft 365 group -> '%s'...",
                group_name,
            )

            # Now we know it is a new group...
            new_group = self._m365.add_group(name=group_name)
            if new_group is not None:
                # Store the Microsoft 365 group ID in payload:
                group["m365_id"] = new_group["id"]
                self.logger.info(
                    "New Microsoft 365 group -> '%s' with ID -> %s has been created",
                    group_name,
                    group["m365_id"],
                )
            else:
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._groups,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_groups_salesforce")
    def process_groups_salesforce(self, section_name: str = "groupsSalesforce") -> bool:
        """Process groups in payload and create them in Salesforce.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not isinstance(self._salesforce, Salesforce):
            self.logger.error(
                "Salesforce connection not setup properly. Skipping payload section '%s'...",
                section_name,
            )
            return False

        if not self._groups:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # First run through groups: create all groups in payload
        # and store the IDs of the created groups:
        for group in self._groups:
            group_name = group.get("name")
            if not group_name:
                self.logger.error("Group needs a name. Skipping...")
                success = False
                continue

            # Check if group has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not group.get("enabled", True):
                self.logger.info(
                    "Payload for group -> '%s' is disabled. Skipping...",
                    group_name,
                )
                continue

            # Salesforce is disabled per default. There needs to be "enable_salesforce" in payload
            # and it needs to be True:
            if not group.get("enable_salesforce", False):
                self.logger.info(
                    "Salesforce is not enabled in payload for group -> '%s'. Skipping...",
                    group_name,
                )
                continue

            # Check if the group does already exist (e.g. if job is restarted)
            existing_group_id = self._salesforce.get_group_id(group_name=group_name)
            if existing_group_id:
                self.logger.info(
                    "Found existing Salesforce group -> '%s' (%s). Skipping...",
                    group_name,
                    existing_group_id,
                )
                # Write M365 group ID back into the payload (for the success file)
                group["salesforce_id"] = existing_group_id
                continue

            self.logger.info(
                "Creating a new Salesforce group -> '%s'...",
                group_name,
            )

            # Now we know it is a new group...
            new_group = self._salesforce.add_group(group_name=group_name)
            new_group_id = self._salesforce.get_result_value(
                response=new_group,
                key="id",
            )
            if new_group_id:
                # Store the Microsoft 365 group ID in payload:
                group["salesforce_id"] = new_group_id
                self.logger.info(
                    "Successfully created Salesforce group -> '%s' with ID -> %s.",
                    group_name,
                    new_group_id,
                )
            else:
                self.logger.error(
                    "Failed to create Salesforce group -> '%s'!",
                    group_name,
                )
                success = False

        # Second run through groups: create all group memberships
        # (nested groups) based on the IDs created in first run:
        for group in self._groups:
            if "salesforce_id" not in group:
                self.logger.info(
                    "Group -> '%s' does not have an Salesforce ID. Skipping...",
                    group["name"],
                )
                # Not all groups may be enabled for Salesforce. This is not an error.
                continue
            group_id = group["salesforce_id"]
            parent_group_names = group["parent_groups"]
            for parent_group_name in parent_group_names:
                # First, try to find parent group in payload by parent group name:
                parent_group = next(
                    (item for item in self._groups if item["name"] == parent_group_name),
                    None,
                )
                if not parent_group:
                    self.logger.error(
                        "Parent group -> '%s' not found. Cannot establish group nesting. Skipping...",
                        parent_group["name"],
                    )
                    success = False
                    continue
                if "salesforce_id" not in parent_group:
                    self.logger.info(
                        "Parent group -> '%s' does not have a Salesforce ID. Cannot establish group nesting. Parent group may not be enabled for Salesforce. Skipping...",
                        parent_group["name"],
                    )
                    # We don't treat this as an error - there may be payload groups which are not enabled for Salesforce!
                    continue

                parent_group_id = parent_group["salesforce_id"]

                # retrieve all members of the parent group
                # to check if the current group is already a member in the parent group:
                members = self._salesforce.get_group_members(group_id=parent_group_id)
                if self._salesforce.exist_result_item(
                    members,
                    "UserOrGroupId",
                    group_id,
                ):
                    self.logger.info(
                        "Salesforce group -> '%s' (%s) is already a member of parent Salesforce group -> '%s' (%s). Skipping to next parent group...",
                        group["name"],
                        group["id"],
                        parent_group_name,
                        parent_group_id,
                    )
                    continue
                self.logger.info(
                    "Add Salesforce group -> '%s' (%s) to parent Salesforce group -> '%s' (%s)...",
                    group["name"],
                    group_id,
                    parent_group_name,
                    parent_group_id,
                )
                self._salesforce.add_group_member(
                    group_id=parent_group_id,
                    member_id=group_id,
                )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._groups,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_groups_core_share")
    def process_groups_core_share(self, section_name: str = "groupsCoreShare") -> bool:
        """Process groups in payload and create them in Core Share.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not isinstance(self._core_share, CoreShare):
            self.logger.error(
                "Core Share connection not setup properly. Skipping payload section '%s'...",
                section_name,
            )
            return False

        if not self._groups:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # Create all groups specified in payload
        # and store the IDs of the created Core Share groups:
        for group in self._groups:
            group_name = group.get("name")
            if not group_name:
                self.logger.error("Group needs a name. Skipping...")
                success = False
                continue

            # Check if group has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not group.get("enabled", True):
                self.logger.info(
                    "Payload for group -> '%s' is disabled. Skipping...",
                    group_name,
                )
                continue

            # Core Share is disabled per default. There needs to be "enable_core_share" in payload
            # and it needs to be True:
            if not group.get("enable_core_share", False):
                self.logger.info(
                    "Group -> '%s' is not enabled for Core Share. Skipping...",
                    group_name,
                )
                continue

            # Check if the group does already exist (e.g. if job is restarted)
            core_share_group = self._core_share.get_group_by_name(name=group_name)
            core_share_group_id = self._core_share.get_result_value(
                response=core_share_group,
                key="id",
            )
            if core_share_group_id:
                self.logger.info(
                    "Found existing Core Share group -> '%s' (%s). Just do cleanup of potential left-overs...",
                    group_name,
                    core_share_group_id,
                )
                # Write Core Share group ID back into the payload (for the success file)
                group["core_share_id"] = core_share_group_id

                # For existing users we want to cleanup possible left-overs form old deployments
                self.logger.info(
                    "Cleanup existing file shares of Core Share group -> '%s' (%s)...",
                    group_name,
                    core_share_group_id,
                )
                response = self._core_share.cleanup_group_shares(
                    group_id=core_share_group_id,
                )
                if not response:
                    self.logger.error("Failed to cleanup group shares!")

                continue

            self.logger.info(
                "Creating a new Core Share group -> '%s'...",
                group_name,
            )

            # Now we know it is a new group...
            new_group = self._core_share.add_group(group_name=group_name)
            new_group_id = self._core_share.get_result_value(
                response=new_group,
                key="id",
            )
            if new_group_id:
                # Store the Microsoft 365 group ID in payload:
                group["core_share_id"] = new_group_id
                self.logger.info(
                    "Successfully created Core Share group -> '%s' with ID -> %s.",
                    group_name,
                    new_group_id,
                )
            else:
                self.logger.error(
                    "Failed to create Core Share group -> %s!",
                    group_name,
                )
                success = False

        # Core Share groups cannot be nested. So we are fone here.

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._groups,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users")
    def process_users(self, section_name: str = "users") -> bool:
        """Process users in payload and create them in Content Server.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        Side Effects:
            The user items are modified by adding an "id" dict element that
            includes the technical ID of the user in Content Server

        """

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # Even if this payload section has been processed successfully before we
        # to process it once more. So we are NOT checking the status file.

        success: bool = True

        # Add all users in payload and establish membership in
        # specified groups:
        for user in self._users:
            user_name = user.get("name")
            if not user_name:
                self.logger.error("User is missing a login. Skipping to next user...")
                success = False
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            # Check if the user does already exist (e.g. if job is restarted)
            # determine_user_id() also writes back the user ID into the payload
            # if it has gathered it from OTCS.
            user_id = self.determine_user_id(user=user)
            if user_id:
                self.logger.info(
                    "Found existing user -> '%s' (%s). Skipping to next user...",
                    user_name,
                    user_id,
                )
                continue
            self.logger.info(
                "Cannot find an existing user -> '%s' - creating a new user...",
                user_name,
            )

            # Sanity checks:
            if "password" not in user or user["password"] is None or user["password"] == "":
                self.logger.info(
                    "User -> '%s' no password defined in payload, generating random password...",
                    user_name,
                )
                user["password"] = self.generate_password(
                    length=10,
                    use_special_chars=True,
                )

                description_attribue = {
                    "name": "description",
                    "value": "initial password: " + user["password"],
                }

                try:
                    user["extra_attributes"].append(description_attribue)
                except KeyError:
                    user["extra_attributes"] = [description_attribue]

            # Sanity checks:
            if "base_group" not in user or not user["base_group"]:
                self.logger.warning(
                    "User -> '%s' is missing a base group - setting to default group.",
                    user_name,
                )
                user["base_group"] = "DefaultGroup"

            # Find the base group of the user. Assume 'Default Group' (= 1001) if not found:
            base_group = next(
                (item["id"] for item in self._groups if item["name"] == user["base_group"] and item.get("id")),
                1001,
            )

            # Get type to be able to create ServiceUsers, default to User -> 0
            user_type = 17 if user.get("type", "User") == "ServiceUser" else 0

            # Now we know it is a new user...
            new_user = self._otcs.add_user(
                name=user_name,
                password=user["password"],
                first_name=user.get("firstname", ""),  # be careful - can be empty
                last_name=user.get("lastname", ""),  # be careful - can be empty
                email=user.get("email", ""),  # be careful - can be empty
                title=user.get("title", ""),  # be careful - can be empty
                base_group=base_group,
                phone=user.get("phone", ""),
                privileges=user.get("privileges", ["Login", "Public Access"]),
                user_type=user_type,
            )

            new_user_id = self._otcs.get_result_value(response=new_user, key="id")
            if not new_user_id:
                self.logger.error(
                    "Failed to create new user -> '%s'!",
                    user_name,
                )
                success = False
                continue

            self.logger.info(
                "Successfully created user -> '%s' with ID -> %s.",
                user_name,
                new_user_id,
            )
            # Write back user ID into payload
            user["id"] = new_user_id

            # Assign usage privileges to the new user:
            usage_privileges = user.get("usage_privileges", [])
            for usage_privilege in usage_privileges:
                response = self._otcs.assign_usage_privilege(
                    usage_privilege=usage_privilege,
                    member_id=new_user_id,
                )
                if response:
                    self.logger.info(
                        "Assigned usage privilege -> '%s' to user -> '%s' (%s).",
                        usage_privilege,
                        user_name,
                        new_user_id,
                    )
                else:
                    self.logger.info(
                        "Failed to assign usage privilege -> '%s' to user -> '%s' (%s)!",
                        usage_privilege,
                        user_name,
                        new_user_id,
                    )

            # Assign usage privileges to the new user:
            object_privileges = user.get("object_privileges", [])
            for object_type in object_privileges:
                response = self._otcs.assign_object_privilege(
                    object_type=object_type,
                    member_id=new_user_id,
                )
                if response:
                    self.logger.info(
                        "Assigned object privilege -> '%s' to user -> '%s' (%s).",
                        object_type,
                        user_name,
                        new_user_id,
                    )
                else:
                    self.logger.info(
                        "Failed to assign object privilege -> '%s' to user -> '%s' (%s)!",
                        object_type,
                        user_name,
                        new_user_id,
                    )

            # Process group memberships of new user:
            user_groups = user.get("groups", [])  # list of groups the user is in
            for user_group in user_groups:
                # Try to find the group dictionary item in the payload
                # for user group name:
                group = next(
                    (item for item in self._groups if item["name"] == user_group),
                    None,
                )
                if group:
                    group_id = group.get("id")  # Careful ID may not exist
                    group_name = group["name"]
                else:
                    # if group is not in payload try to find group in OTCS
                    # in case it is a pre-existing group:
                    group = self._otcs.get_group(name=user_group)
                    group_id = self._otcs.get_result_value(response=group, key="id")
                    if group_id is None:
                        self.logger.error(
                            "Group -> '%s' not found. Skipping...",
                            user_group,
                        )
                        success = False
                        continue
                    group_name = self._otcs.get_result_value(
                        response=group,
                        key="name",
                    )

                if group_id is None:
                    self.logger.error(
                        "Group -> '%s' does not have an ID. Cannot add user -> '%s' to this group. Skipping...",
                        group_name,
                        user["name"],
                    )
                    success = False
                    continue

                self.logger.info(
                    "Add user -> '%s' (%s) to group -> '%s' (%s)...",
                    user["name"],
                    user["id"],
                    group_name,
                    group_id,
                )
                response = self._otcs.add_group_member(
                    member_id=user["id"],
                    group_id=group_id,
                )
                if not response:
                    success = False
            # end for user_group in user_groups:

            # For some unclear reason the user is not added to its base group in OTDS
            # so we do this explicitly:
            self.logger.info(
                "Add user -> '%s' to its base group -> '%s'...",
                user["name"],
                user["base_group"],
            )
            response = self._otds.add_user_to_group(
                user["name"],
                user["base_group"],
            )
            if not response:
                success = False

            # Extra OTDS attributes for the user can be provided in "extra_attributes"
            # as part of the user payload.
            if "extra_attributes" in user:
                extra_attributes = user["extra_attributes"]
                for extra_attribute in extra_attributes:
                    attribute_name = extra_attribute.get("name")
                    attribute_value = extra_attribute.get("value")
                    if not attribute_name or not attribute_value:
                        self.logger.error(
                            "User attribute is missing a name or value. Skipping...",
                        )
                        success = False
                        continue
                    if "password" in attribute_value:
                        self.logger.info(
                            "Set user attribute -> '%s' to -> '*******' (sensitive password)",
                            attribute_name,
                        )
                    else:
                        self.logger.info(
                            "Set user attribute -> '%s' to -> '%s'.",
                            attribute_name,
                            attribute_value,
                        )
                    user_partition = self._otcs.config()["partition"]
                    if not user_partition:
                        self.logger.error("User partition not found!")
                        success = False
                        continue
                    self._otds.update_user(
                        user_partition,
                        user["name"],
                        attribute_name,
                        attribute_value,
                    )
            # end if "extra_attributes" in user

            # Assign application roles to the new user:
            application_roles = user.get("application_roles", [])

            for role in application_roles:
                user_partition = self._otcs.config()["partition"]
                if not user_partition:
                    self.logger.error("User partition not found!")
                    success = False
                    continue

                # Split role at the @ sign to identify Partitions
                role_parts = role.split("@", 1)
                role_name = role_parts[0]
                role_partition = role_parts[1] if len(role_parts) > 1 else "OAuthClients"

                response = self._otds.assign_user_to_application_role(
                    user_id=user["name"],
                    user_partition=user_partition,
                    role_name=role_name,
                    role_partition=role_partition,
                )

                if response:
                    self.logger.info(
                        "Successfully assigned application role -> '%s' (%s) to user -> '%s' (%s).",
                        role_name,
                        role_partition,
                        user_name,
                        user_partition,
                    )
                else:
                    self.logger.info(
                        "Failed to assign application role -> '%s' (%s) to user -> '%s' (%s) in OTDS!",
                        role_name,
                        role_partition,
                        user_name,
                        user_partition,
                    )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users_sap")
    def process_users_sap(self, section_name: str = "usersSAP") -> bool:
        """Process users in payload and sync them with SAP (passwords only for now).

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool: True if payload has been processed without errors, False otherwise

        Side Effects:
            The user items are modified by adding an "sap_sync_result" dict element
            that documents if the user password was properly synced with SAP.

        """

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        if not self._sap:
            self.logger.error("SAP connection is not initialized. Bailing out...")
            return False

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        rfc_name = "ZFM_TERRA_RFC_CHNG_USR_PW"
        rfc_description = "RFC to update the SAP user password"
        rfc_call_options = ()

        # Update SAP password for all users in payload:
        for user in self._users:
            user_name = user.get("name")
            if not user_name:
                self.logger.error("User is missing a login. Skipping to next user...")
                success = False
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            # Check if the user is explicitly enabled for SAP:
            if not user.get("enable_sap", False):
                self.logger.info(
                    "User -> '%s' is not enabled for SAP. Skipping...",
                    user_name,
                )
                continue

            # Lookup mandatory password in payload:
            user_password = user.get("password")
            if not user_password:
                self.logger.error(
                    "User -> '%s' is missing a password. Cannot sync with SAP. Skipping to next user...",
                    user_name,
                )
                success = False
                continue

            rfc_params = {
                "USERNAME": user_name,
                "PASSWORD": user_password,
            }

            self.logger.info(
                "Updating password of user -> '%s' in SAP. Calling SAP RFC -> '%s' (%s) with parameters -> %s ...",
                user_name,
                rfc_name,
                rfc_description,
                str(rfc_params),
            )

            result = self._sap.call(
                rfc_name=rfc_name,
                options=rfc_call_options,
                rfc_parameters=rfc_params,
            )
            if result is None:
                self.logger.error(
                    "Failed to call SAP RFC -> '%s' to update password of user -> '%s'!",
                    rfc_name,
                    user_name,
                )
                success = False
            elif result.get("RESULT") != "OK":
                self.logger.error(
                    "Result of SAP RFC -> '%s' is not OK, it returned -> %s failed items in result -> %s",
                    rfc_name,
                    str(result.get("FAILED")),
                    str(result),
                )
                success = False
                # Save result for status file content
                user["sap_sync_result"] = result
            else:
                self.logger.info(
                    "Successfully called RFC -> '%s'. Result -> %s.",
                    rfc_name,
                    str(result),
                )
                # Save result for status file content
                user["sap_sync_result"] = result

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users_successfactors")
    def process_users_successfactors(
        self,
        section_name: str = "usersSuccessFactors",
    ) -> bool:
        """Process users in payload and sync them with SuccessFactors (passwords and email).

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        Side Effects:
            The user items are modified by adding an "successfactors_user_id" dict element that
            includes the personIdExternal of the user in SuccessFactors

        """

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        if not self._successfactors:
            self.logger.error(
                "SuccessFactors connection is not initialized. Bailing out...",
            )
            return False

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # traverse all users in payload:
        for user in self._users:
            user_name = user.get("name")
            if not user_name:
                self.logger.error("User is missing a login. Skipping to next user...")
                success = False
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            # Check if the user is explicitly enabled for SuccessFactors:
            if not user.get("enable_successfactors", False):
                self.logger.info(
                    "User -> '%s' is not enabled for SuccessFactors. Skipping...",
                    user_name,
                )
                continue

            # Lookup password and email in payload:
            user_password = user.get("password", "")
            user_email = user.get("email", "")

            # first we need to get the SuccessFactors user account object
            # to determine the personIdExternal that we need to update the
            # SuccessFactors user.
            response = self._successfactors.get_user_account(username=user_name)
            user_id = self._successfactors.get_result_value(
                response=response,
                key="personIdExternal",
            )
            if user_id is None:
                self.logger.error(
                    "Failed to get personIDExternal of SuccessFactors user -> '%s'!",
                    user_name,
                )
                success = False
                continue
            self.logger.info(
                "SuccessFactors user -> '%s' has external user ID -> %s.",
                user_name,
                str(user_id),
            )

            # Now let's update the user password and email address:
            update_data = {}
            if user_password:
                self.logger.info(
                    "Updating password of SuccessFactors user -> '%s' (%s)...",
                    user_name,
                    str(user_id),
                )
                update_data["password"] = user_password
            if user_email:
                update_data["email"] = user_email

            response = self._successfactors.update_user(
                user_id=user_id,
                update_data=update_data,
            )
            if response:
                self.logger.info(
                    "Successfully updated SuccessFactors user -> '%s'.",
                    str(user_name),
                )
                # Save result for status file content
                user["successfactors_user_id"] = user_id
            else:
                self.logger.error(
                    "Failed to update SuccessFactors user -> '%s'! Skipping...",
                    user_name,
                )
                success = False
                continue

            if not user_email:
                continue

            self.logger.info(
                "Updating email of SuccessFactors user -> '%s' to -> %s...",
                user_name,
                user_email,
            )
            response = self._successfactors.update_user_email(
                user_id=user_id,
                email_address=user_email,
            )
            if response:
                self.logger.info(
                    "Successfully updated email address of SuccessFactors user -> '%s' to -> '%s'.",
                    user_name,
                    user_email,
                )
            else:
                self.logger.error(
                    "Failed to update email address of SuccessFactors user -> '%s' to -> '%s'!",
                    user_name,
                    user_email,
                )
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users_salesforce")
    def process_users_salesforce(
        self,
        section_name: str = "usersSalesforce",
    ) -> bool:
        """Process users in payload and sync them with Salesforce (passwords and email).

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        Side Effects:
            The user items are modified by adding "salesforce_user_id", "salesforce_user_login"
            dict element that includes the ID of the user in Salesforce. This will be written
            into the status file.

        """

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        if not self._salesforce:
            self.logger.error(
                "Salesforce connection is not initialized. Bailing out...",
            )
            return False

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # traverse all users in payload:
        for user in self._users:
            user_name = user.get("name")
            if not user_name:
                self.logger.error("User is missing a login. Skipping to next user...")
                success = False
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            # Check if the user is explicitly enabled for Salesforce:
            if not user.get("enable_salesforce", False):
                self.logger.info(
                    "User -> '%s' is not enabled for Salesforce. Skipping...",
                    user_name,
                )
                continue

            extra_attributes = user.get("extra_attributes", None)
            if not extra_attributes or len(extra_attributes) == 0:
                self.logger.info(
                    "User -> '%s' does not have the extra attributes for Salesforce. Cannot determine the Salesforce login for user. Skipping...",
                    user_name,
                )
                continue
            user_login = extra_attributes[0].get("value", "")
            if not user_login:
                self.logger.info(
                    "User -> '%s' does not have the extra attributes value for Salesforce. Skipping...",
                    user_name,
                )
                continue

            user_email = user.get("email", "")
            need_email_verification = False

            #
            # 1. Check if user does already exist in Salesforce:
            #

            salesforce_user_id = self._salesforce.get_user_id(username=user_login)

            #
            # 2: Create or Update user in Salesforce:
            #

            if salesforce_user_id is None:
                self.logger.info(
                    "Salesforce user -> '%s' does not exist. Creating a new Salesforce user...",
                    user_name,
                )
                response = self._salesforce.add_user(
                    username=user_login,
                    email=user.get("email", ""),
                    firstname=user.get("firstname", ""),
                    lastname=user.get("lastname", ""),
                    department=user.get("base_group", ""),
                    title=user.get("title", ""),
                    company_name=user.get("company", "Innovate"),
                )
                salesforce_user_id = self._salesforce.get_result_value(
                    response=response,
                    key="id",
                )
                if not salesforce_user_id:
                    self.logger.error(
                        "Failed to create Salesforce user -> '%s'! Skipping...",
                        user_name,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Successfully created Salesforce user -> '%s' with ID -> %s.",
                    user_name,
                    salesforce_user_id,
                )
                # We need email verification for new users (unclear if this is really the case...)
                need_email_verification = True
            # The user does already exist in Salesforce and we just update it:
            else:
                update_data = {
                    "FirstName": user.get("firstname", ""),
                    "LastName": user.get("lastname", ""),
                    "Department": user.get("base_group", ""),
                    "Title": user.get("title", ""),
                    "CompanyName": user.get("company", ""),
                }
                self.logger.info(
                    "Salesforce user -> '%s' does already exist. Updating Salesforce user with -> %s...",
                    user_name,
                    str(update_data),
                )

                # Check if the mail address has really changed. Otherwise we
                # don't need to set it again and can avoid email verification:
                salesforce_user = self._salesforce.get_user(user_id=salesforce_user_id)
                salesforce_user_email = self._salesforce.get_result_value(
                    response=salesforce_user,
                    key="Email",
                )
                if user_email != salesforce_user_email:
                    self.logger.info(
                        "Email for Salesforce user -> '%s' has changed from -> '%s' to -> '%s'.",
                        user_name,
                        salesforce_user_email,
                        user_email,
                    )
                    # Additional email payload for user update:
                    update_data["Email"] = user_email
                    # OK, email has changed - we need the email verification below...
                    need_email_verification = True

                # Update the existing Salesforce user with new / changed data:
                response = self._salesforce.update_user(
                    user_id=salesforce_user_id,
                    update_data=update_data,
                )
                if not response:
                    self.logger.error(
                        "Failed to update Salesforce user -> '%s'! Skipping...",
                        user_login,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Successfully updated Salesforce user -> '%s'.",
                    user_login,
                )

            # Save result for status file content
            user["salesforce_user_id"] = salesforce_user_id
            user["salesforce_user_login"] = user_login

            #
            # 3: Update user password in Salesforce (we need to do this also for new users!):
            #

            # Lookup password in payload:
            user_password = user.get("password", "")

            if user_password:
                response = self._salesforce.update_user_password(
                    user_id=salesforce_user_id,
                    password=user_password,
                )
                if response:
                    self.logger.info(
                        "Successfully updated password of Salesforce user -> '%s' (%s).",
                        user_login,
                        salesforce_user_id,
                    )
                else:
                    self.logger.error(
                        "Failed to update Salesforce password for user -> '%s' (%s)! Skipping...",
                        user_login,
                        salesforce_user_id,
                    )
                    success = False
                    continue

            #
            # 4: Handle Email verification:
            #

            # We now need to wait for the verification mail from Salesforce,
            # get it from the M365 Outlook inbox of the user (or the admin
            # if the user does not have its own inbox) and click the
            # verification link...

            if need_email_verification and user.get("enable_o365", False):
                self.logger.info(
                    "Processing Email verification for user -> '%s' (%s). Wait a few seconds to make sure verification mail in user's inbox...",
                    user_name,
                    user_email,
                )
                time.sleep(20)

                # Process verification mail sent by Salesforce.
                # This has some hard-coded value. We may want to optimize it further in the future:
                result = self._m365.email_verification(
                    user_email=user_email,
                    sender="QA_SUPPORT@salesforce.com",
                    subject="Finish changing your Salesforce",
                    url_search_pattern="setup/emailverif",
                )
                if not result:
                    # Email verification was not successful
                    self.logger.warning(
                        "Salesforce email verification failed. No verification mail received in user's inbox using email address -> '%s'!",
                        user_email,
                    )
                    # don't treat as error nor do "continue" here - we still want to process the user groups...
                else:
                    self.logger.info(
                        "Successfully verified new email address -> '%s'.",
                        user_email,
                    )
            # end if need_email_verification

            #
            # 5: Add users into groups in Salesforce:
            #

            self.logger.info(
                "Processing group memberships of Salesforce user -> '%s' (%s)...",
                user_name,
                user_email,
            )
            user_groups = user.get("groups", [])
            base_group = user.get("base_group", None)
            if base_group and base_group not in user_groups:
                user_groups.append(base_group)  # list of groups the user is in

            for user_group in user_groups:
                # "Business Administrators" is a OTCS generated group that we won't find
                # in payload - skip this group.
                if user_group == "Business Administrators":
                    continue
                # Try to find the group dictionary item in the payload
                # for user group name:
                group = next(
                    (item for item in self._groups if item["name"] == user_group),
                    None,
                )
                if not group:
                    self.logger.error(
                        "Cannot find group -> '%s'. Cannot establish membership in Salesforce. Skipping to next group...",
                        user_group,
                    )
                    success = False
                    continue

                group_id = group.get("salesforce_id")  # Careful ID may not exist
                group_name = group["name"]
                if group_id is None:
                    self.logger.info(
                        "Group -> '%s' does not have a Salesforce ID. Cannot add user -> '%s' to this Salesforce group (group may not be enabled for Salesforce). Skipping...",
                        group_name,
                        user_name,
                    )
                    # We don't treat this as an error - there may be payload groups which are not enabled for Salesforce!
                    continue

                existing_members = self._salesforce.get_group_members(group_id=group_id)
                if not existing_members or not self._salesforce.exist_result_item(
                    response=existing_members,
                    key="UserOrGroupId",
                    value=salesforce_user_id,
                ):
                    self.logger.info(
                        "Add Salesforce user -> '%s' (%s) to Salesforce group -> '%s' (%s)...",
                        user_name,
                        salesforce_user_id,
                        group_name,
                        group_id,
                    )
                    response = self._salesforce.add_group_member(
                        group_id=group_id,
                        member_id=salesforce_user_id,
                    )
                    member_id = self._salesforce.get_result_value(
                        response=response,
                        key="id",
                    )
                    if not member_id:
                        self.logger.error(
                            "Failed to add Salesforce user -> '%s' (%s) as member to Salesforce group -> '%s' (%s)!",
                            user_name,
                            salesforce_user_id,
                            group_name,
                            group_id,
                        )
                        success = False
                        continue
                else:
                    self.logger.info(
                        "Salesforce user -> '%s' (%s) does already exist in Salesforce group -> '%s' (%s). Skipping...",
                        user_name,
                        salesforce_user_id,
                        group_name,
                        group_id,
                    )
            # end for loop user groups
        # end for loop users

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users_core_share")
    def process_users_core_share(
        self,
        section_name: str = "usersCoreShare",
    ) -> bool:
        """Process users in payload and sync them with Core Share (passwords and email).

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        Side Effects:
            The user items are modified by adding "core_share_user_id"
            dict element that includes the ID of the user in Core Share. This will be written
            into the status file.

        """

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        if not self._core_share:
            self.logger.error(
                "Core Share connection is not initialized. Bailing out...",
            )
            return False

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # traverse all users in payload:
        for user in self._users:
            user_last_name = user.get("lastname", "")  # Default is important here
            user_first_name = user.get("firstname", "")
            user_name = " ".join(filter(None, [user_first_name, user_last_name]))
            user_enabled = user.get("enabled", True) and user.get("enable_core_share", False)
            if not user_name and user_enabled:  # Avoid a warning if not enbaled
                self.logger.error(
                    "User is missing last name and first name. Skipping to next user...",
                )
                success = False
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            # Check if the user is enabled for Core Share.
            # Core Share is disabled per default for the users.
            # There needs to be "enable_core_share" in user payload
            # and it needs to be True:
            if not user.get("enable_core_share", False):
                self.logger.info(
                    "User -> '%s' is not enabled for Core Share. Skipping...",
                    user_name,
                )
                continue

            user_email = user.get("email", "")
            user_password = user.get("password", "")

            # Initialize variables:
            need_email_verification = False
            subject = None
            url_search_pattern = None

            #
            # 1. Check if user does already exist in Core Share:
            #

            core_share_user_id = self.determine_user_id_core_share(user=user)

            #
            # 2: Create or Update user in Core Share:
            #

            # Check if we need to create a new Core Share user:
            if core_share_user_id is None:
                self.logger.info(
                    "Core Share user -> '%s' does not exist. Creating a new Core Share user...",
                    user_name,
                )
                response = self._core_share.add_user(
                    first_name=user_first_name,
                    last_name=user_last_name,
                    email=user_email,
                    title=user.get("title", None),
                    company=user.get("company", "Innovate"),
                    password=user.get("password", None),
                )
                core_share_user_id = self._core_share.get_result_value(
                    response=response,
                    key="id",
                )
                if not core_share_user_id:
                    self.logger.error(
                        "Failed to create Core Share user -> '%s'! Skipping...",
                        user_name,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Successfully created Core Share user -> '%s' (%s).",
                    user_name,
                    core_share_user_id,
                )

                # Check if the user is still in pending state:
                is_confirmed = self._core_share.get_result_value(
                    response=response,
                    key="isConfirmed",
                )
                # we need to differentiate False an None here - don't simplify to "if not is_confirmed"!
                if is_confirmed is False:
                    self.logger.info(
                        "New Core Share user -> '%s' is not yet confirmed and in a 'pending' state!",
                        user_name,
                    )
                elif is_confirmed is True:
                    self.logger.info(
                        "New Core Share user -> '%s' is already confirmed!",
                        user_name,
                    )

                # We write the user password in addition to the "Other" Address field
                # to determine in a subsequent deployment the "old" password:
                update_data = {
                    "addresses": [
                        {"type": "other", "value": user.get("password", None)},
                    ],
                }
                response = self._core_share.update_user(
                    user_id=core_share_user_id,
                    update_data=update_data,
                )

                # We need email verification for new users:
                need_email_verification = True
                url_search_pattern = "verify-email"
                subject = "Welcome to OpenText Core Share"

                # For new users the old password is equal to the new password:
                old_password = user.get("password", None)
            # The user does already exist in Core Share:
            else:
                update_data = {
                    "firstName": user.get("firstname", ""),
                    "lastName": user.get("lastname", ""),
                    "title": user.get("title", ""),
                    "company": user.get("company", ""),
                }
                self.logger.info(
                    "Core Share user -> '%s' does already exist. Updating Core Share user with -> %s...",
                    user_name,
                    str(update_data),
                )

                # Fetch the existing user:
                core_share_user = self._core_share.get_user_by_id(
                    user_id=core_share_user_id,
                )

                # Check if the user is still in pending state:
                is_confirmed = self._core_share.get_result_value(
                    response=core_share_user,
                    key="isConfirmed",
                )
                # we need to differentiate False an None here - don't simplify to "if not is_confirmed"!
                if is_confirmed is False:
                    self.logger.warning(
                        "Core Share user -> '%s' has not yet confirmed the email invitation and is in 'pending' state! Resend invite...",
                        user_name,
                    )
                    # We try the email verification once more...
                    self._core_share.resend_user_invite(core_share_user_id)
                    need_email_verification = True
                    url_search_pattern = "confirm-account"
                    subject = "Invitation to OpenTextâ„¢ Core Share"

                # Check if we have the old password of the user in the "Other" address field:
                core_share_user_addresses = self._core_share.get_result_value(
                    core_share_user,
                    "addresses",
                )
                if core_share_user_addresses and len(core_share_user_addresses) > 0:
                    old_password = core_share_user_addresses[0]["value"]
                    self.logger.info(
                        "Found old password for Core Share user -> '%s' (%s).",
                        user_name,
                        core_share_user_id,
                    )
                else:
                    self.logger.info(
                        "No old password found for Core Share user -> '%s'. Cannot set a new password.",
                        user_name,
                    )
                    old_password = ""

                # We store the current password into the address field (this adds to the update dictionary
                # defined above and used below): THIS IS CURRENTLY NOT WORKING!
                update_data["addresses"] = [{"type": "other", "value": user_password}]

                # Check if the mail address has really changed. Otherwise we
                # don't need to set it again and can avoid email verification:
                core_share_user_email = self._core_share.get_result_value(
                    core_share_user,
                    "email",
                )
                if user_email != core_share_user_email:
                    self.logger.info(
                        "Email for Core Share user -> '%s' has changed from -> '%s' to -> '%s'. We need to verify this via email.",
                        user_name,
                        core_share_user_email,
                        user_email,
                    )
                    # Additional email payload for user update:
                    update_data["email"] = user_email
                    # If email is changed this needs to be confirmed by passing
                    # the current (old) password:
                    update_data["password"] = old_password if old_password else user_password
                    # As email has changed - we need the email verification below...
                    need_email_verification = True
                    url_search_pattern = "verify-email"
                    subject = "OpenText Core Share: Email Updated"

                # Update the existing Core Share user with new / changed data:
                response = self._core_share.update_user(
                    user_id=core_share_user_id,
                    update_data=update_data,
                )
                if not response:
                    self.logger.error(
                        "Failed to update Core Share user -> '%s'! Skipping...",
                        user_name,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Successfully updated Core Share user -> '%s'.",
                    user_name,
                )

                # Now update the password:
                if user_password and old_password and user_password != old_password:
                    response = self._core_share.update_user_password(
                        user_id=core_share_user_id,
                        password=old_password,
                        new_password=user_password,
                    )
                    if response:
                        self.logger.info(
                            "Successfully updated password of Core Share user -> '%s' (%s).",
                            user_name,
                            core_share_user_id,
                        )
                    else:
                        self.logger.error(
                            "Failed to update Core Share password for user -> '%s' (%s)! Skipping...",
                            user_name,
                            core_share_user_id,
                        )
                        success = False
                        continue
                elif not old_password:
                    self.logger.warning(
                        "Cannot change Core Share user password for -> '%s' (%s). Need both, old and new passwords.",
                        user_name,
                        core_share_user_id,
                    )
                else:
                    self.logger.info(
                        "Core Share user password for -> '%s' (%s) is unchanged.",
                        user_name,
                        core_share_user_id,
                    )

                # For existing users we want to cleanup possible left-overs form old deployments
                self.logger.info(
                    "Cleanup existing file shares of Core Share user -> '%s' (%s)...",
                    user_name,
                    core_share_user_id,
                )
                response = self._core_share.cleanup_user_files(
                    user_id=core_share_user_id,
                    user_login=core_share_user_email,
                    user_password=user_password,
                )
                if not response:
                    self.logger.error(
                        "Failed to cleanup user files for user -> '%s' (%s)!", user_name, core_share_user_id
                    )

            # Save result for status file content
            user["core_share_user_id"] = core_share_user_id

            #
            # 3: Handle Email verification:
            #

            # We now need to wait for the verification mail from Core Share,
            # get it from the M365 Outlook inbox of the user (or the admin
            # if the user does not have its own inbox) and click the
            # verification link...

            if need_email_verification and user.get("enable_o365", False):
                self.logger.info(
                    "Processing Email verification for user -> '%s' (%s). Wait 20 seconds to make sure verification mail in user's inbox...",
                    user_name,
                    user_email,
                )
                time.sleep(20)

                # Process verification mail sent by Core Share.
                # TODO: This has some hard-coded value. We may want to optimize it further in the future:
                result = self._m365.email_verification(
                    user_email=user_email,
                    sender="noreply@opentext.cloud",
                    subject=subject,
                    url_search_pattern=url_search_pattern,
                    line_end_marker="=",
                    multi_line=True,
                    multi_line_end_marker="%3D",
                    replacements=None,
                    max_retries=6,
                    use_browser_automation=True,
                    password=user_password,
                    password_field_id="passwordInput",
                    password_confirmation_field_id="confirmResetPassword",
                    password_submit_xpath="//button[@type='submit']",
                    terms_of_service_xpath="//div[@id = 'termsOfService']//button[@type='submit']",
                )
                if not result:
                    # Email verification was not successful
                    self.logger.warning(
                        "Core Share email verification failed. No verification mail received in user's inbox.",
                    )
                    # don't treat as error nor do "continue" here - we still want to process the user groups...
                else:
                    self.logger.info(
                        "Successfully verified new email address -> '%s'.",
                        user_email,
                    )
            # end if need_email_verification

            #
            # 4: Add users into groups in Core Share:
            #

            self.logger.info(
                "Processing group memberships of Core Share user -> '%s' (%s)...",
                user_name,
                user_email,
            )
            user_groups = user.get("groups", [])
            base_group = user.get("base_group", None)
            if base_group and base_group not in user_groups:
                user_groups.append(base_group)  # list of groups the user is in

            for user_group in user_groups:
                # "Business Administrators" is a OTCS generated group that we won't find
                # in payload - skip this group.
                if user_group == "Business Administrators":
                    # Users that are Business Administrators in Content Server
                    # become Content Manager (role = 5) in Core Share:
                    self.logger.info(
                        "User -> '%s' is a business administrator in Content Server and becomes a 'Content Manager' (access role 5) in Core Share",
                        user_name,
                    )
                    self._core_share.add_user_access_role(
                        user_id=core_share_user_id,
                        role_id=5,
                    )
                    continue
                # Try to find the group dictionary item in the payload
                # for user group name:
                group = next(
                    (item for item in self._groups if item["name"] == user_group),
                    None,
                )
                if not group:
                    self.logger.error(
                        "Cannot find group -> '%s'. Cannot establish membership in Core Share. Skipping to next group...",
                        user_group,
                    )
                    success = False
                    continue

                group_name = group["name"]
                group_id = self.determine_group_id_core_share(
                    group=group,
                )  # Careful ID may not exist
                if group_id is None:
                    self.logger.info(
                        "Group -> '%s' does not have a Core Share ID. Cannot add user -> '%s' to this Core Share group (group may not be enabled for Core Share). Skipping...",
                        group_name,
                        user_name,
                    )
                    # We don't treat this as an error - there may be payload groups which are not enabled for Core Share!
                    continue

                existing_members = self._core_share.get_group_members(group_id)

                # Only add user as new member if not yet a member or a 'pending' member:
                is_member = self._core_share.exist_result_item(
                    response=existing_members,
                    key="id",
                    value=core_share_user_id,
                    results_marker="groupMembers",
                )
                is_pending_member = self._core_share.exist_result_item(
                    response=existing_members,
                    key="email",
                    value=user_email,
                    results_marker="pending",
                )

                if not is_member and not is_pending_member:
                    self.logger.info(
                        "Add Core Share user -> '%s' (%s) to Core Share group -> '%s' (%s)...",
                        user_name,
                        core_share_user_id,
                        group_name,
                        group_id,
                    )
                    # We make users that have this group as base_group
                    # to Admins of the Core Share group:
                    is_group_admin = user_group == base_group
                    response = self._core_share.add_group_member(
                        group_id=group_id,
                        user_id=core_share_user_id,
                        is_group_admin=is_group_admin,
                    )
                    # the add_group_member() has a special return value
                    # which is a list (not a dict). It has mostly 1 element
                    # which is a dict with a "success" item. This (and not response.ok)
                    # determines if the call was successful!
                    success: bool = self._core_share.get_result_value(
                        response,
                        "success",
                    )
                    if not success:
                        errors = self._core_share.get_result_value(
                            response=response,
                            key="errors",
                        )
                        self.logger.error(
                            "Failed to add Core Share user -> '%s' (%s) as member to Core Share group -> '%s' (%s)! Error -> %s",
                            user_name,
                            core_share_user_id,
                            group_name,
                            group_id,
                            str(errors),
                        )
                        success = False
                        continue
                else:
                    self.logger.info(
                        "Core Share user -> '%s' (%s) is already a %s of Core Share group -> '%s' (%s). Skipping...",
                        user_name,
                        core_share_user_id,
                        "member" if is_member else "pending member",
                        group_name,
                        group_id,
                    )
            # end for loop user groups
        # end for loop users

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users_m365")
    def process_users_m365(self, section_name: str = "usersM365") -> bool:
        """Process users in payload and create them in Microsoft 365 via MS Graph API.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not isinstance(self._m365, M365):
            self.logger.error(
                "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
                section_name,
            )
            return False

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # Add all users in payload and establish membership in
        # specified groups:
        for user in self._users:
            user_name = user.get("name")
            if not user_name:
                self.logger.error("User is missing a login. Skipping to next user...")
                success = False
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            # Check if the user is enabled for Microsoft 365.
            # M365 is disabled per default for the users.
            # There needs to be "enable_o365" in user payload
            # and it needs to be True:
            if not user.get("enable_o365", False):
                self.logger.info(
                    "Microsoft 365 is not enabled in payload for user -> '%s'. Skipping...",
                    user_name,
                )
                continue

            # Sanity checks:
            if "password" not in user:
                self.logger.error(
                    "User -> '%s' is missing a password. Skipping to next user...",
                    user_name,
                )
                success = False
                continue
            user_password = user["password"]
            # be careful with the following fields - they could be empty
            user_department = user.get("base_group", "")
            user_first_name = user.get("firstname", "")
            user_last_name = user.get("lastname", "")
            user_location = user.get("location", "US")
            user_email = user.get("email", user_name)

            # Check if the user does already exist in M365 (e.g. if job is restarted)
            m365_user_id = self.determine_user_id_m365(user=user)
            if not m365_user_id:
                self.logger.info(
                    "Did not find existing Microsoft 365 user - creating user -> '%s'...",
                    user_email,
                )

                # Now we know it is a new user...
                # We are not 1:1 using the email address from the
                # payload as this could by an alias address using the "+" syntax:
                m365_user_name = user_name + "@" + self._m365.config()["domain"]

                new_user = self._m365.add_user(
                    email=m365_user_name,
                    password=user_password,
                    first_name=user_first_name,
                    last_name=user_last_name,
                    location=user_location,
                    department=user_department,
                )
                if new_user is not None:
                    # Store the Microsoft 365 user ID in payload:
                    user["m365_id"] = new_user["id"]
                    m365_user_id = new_user["id"]
                    self.logger.info(
                        "New Microsoft 365 user -> '%s' with ID -> %s has been created.",
                        user_name,
                        m365_user_id,
                    )
                else:
                    self.logger.error(
                        "Failed to create new Microsoft 365 user -> '%s'! Skipping...",
                        user_name,
                    )
                    success = False
                    continue
            else:
                # if the user exists we just set the password according
                # the the payload definition to allow to bulk
                # update existing M365 users with new passwords:
                self.logger.info(
                    "Found existing Microsoft 365 user -> '%s' - updating password...",
                    user_name,
                )
                new_password_settings = {
                    "passwordProfile": {
                        "forceChangePasswordNextSignIn": False,
                        "password": user_password,
                    },
                }
                response = self._m365.update_user(
                    user_id=m365_user_id,
                    updated_settings=new_password_settings,
                )
                if not response:
                    self.logger.error(
                        "Failed to update password of M365 user -> '%s' (%s)!",
                        user_name,
                        m365_user_id,
                    )

            # Now we assign a license to the new M365 user.
            # First we see if there's a M365 SKU list in user
            # payload - if not we wrap the default SKU configured
            # for the m365 object into a single item list:
            existing_user_licenses = self._m365.get_user_licenses(user_id=m365_user_id)
            sku_list = user.get("m365_skus", [self._m365.config()["skuId"]])
            for sku_id in sku_list:
                # Check if the M365 user already has this license:
                if not self._m365.exist_result_item(
                    existing_user_licenses,
                    "skuId",
                    sku_id,
                ):
                    response = self._m365.assign_license_to_user(m365_user_id, sku_id)
                    if not response:
                        self.logger.error(
                            "Failed to assign license -> '%s' to Microsoft 365 user -> '%s'!",
                            sku_id,
                            user_name,
                        )
                        success = False
                    else:
                        if (
                            "m365_skus" not in user
                        ):  # this is only True if the default license from the m365 object is taken
                            user["m365_skus"] = [sku_id]
                        self.logger.info(
                            "License -> '%s' has been assigned to Microsoft 365 user -> '%s'.",
                            sku_id,
                            user_name,
                        )
                else:
                    self.logger.info(
                        "Microsoft 365 user -> '%s' already has the license -> '%s'.",
                        user_name,
                        sku_id,
                    )

            # Now we assign the Content Server Teams App to the new M365 user.
            # First we check if the app is already assigned to the user.
            # If not we install / assign the app. If the user already has
            # the Content Server app we try to upgrade it:
            app_name = self._m365.config()["teamsAppName"]
            app_external_id = self._m365.config()["teamsAppExternalId"]
            app_internal_id = self._m365.config().get("teamsAppInternalId", None)
            response = self._m365.get_teams_apps_of_user(
                user_id=m365_user_id,
                filter_expression="contains(teamsAppDefinition/displayName, '{}')".format(
                    app_name,
                ),
            )
            if self._m365.exist_result_item(
                response=response,
                key="displayName",
                value=app_name,
                sub_dict_name="teamsAppDefinition",
            ):
                self.logger.info(
                    "M365 Teams app -> '%s' is already assigned to M365 user -> '%s' (%s). Trying to upgrade app...",
                    app_name,
                    user_name,
                    m365_user_id,
                )
                response = self._m365.upgrade_teams_app_of_user(
                    user_id=m365_user_id,
                    app_name=app_name,
                )
            else:
                self.logger.info(
                    "Assign M365 Teams app -> '%s' (%s) to M365 user -> '%s' (%s)...",
                    app_name,
                    app_external_id,
                    user_name,
                    m365_user_id,
                )
                # This can produce errors because the app may be assigned organization-wide.
                # So we don't treat it as an error and just show a warning.
                # We also try to use the internal app id instead of the name:
                if app_internal_id:
                    response = self._m365.assign_teams_app_to_user(
                        user_id=m365_user_id,
                        app_name=app_name,
                        app_internal_id=app_internal_id,
                        show_error=False,
                    )
                else:
                    response = self._m365.assign_teams_app_to_user(
                        user_id=m365_user_id,
                        app_name=app_name,
                        show_error=False,
                    )

            # Process Microsoft 365 group memberships of new user:
            # don't forget the base group (department) if it is not yet in groups!
            group_names = user.get("groups", [])
            if user_department and user_department not in group_names:
                group_names.append(user_department)
            self.logger.info(
                "User -> '%s' has these groups in payload -> %s (including base group -> %s). Checking if they are Microsoft 365 groups...",
                user_name,
                group_names,
                user_department,
            )

            # Go through all group names:
            for group_name in group_names:
                # Find the group payload item to the parent group name:
                group = next(
                    (item for item in self._groups if item["name"] == group_name),
                    None,
                )
                if not group:
                    # if group is not in payload then this membership
                    # is not relevant for Microsoft 365. This could be system generated
                    # groups like "PageEdit" or "Business Administrators".
                    # In this case we do "continue" as we can't process parent groups
                    # either:
                    self.logger.debug(
                        "No payload found for group -> '%s'. Skipping...",
                        group_name,
                    )
                    continue
                if "enable_o365" not in group or not group["enable_o365"]:
                    # If Microsoft 365 is not enabled for this group in
                    # the payload we don't create a M365 but we do NOT continue
                    # as there may still be parent groups that are M365 enabled
                    # we want to put the user in (see below):
                    self.logger.info(
                        "Payload group -> '%s' is not enabled for M365.",
                        group_name,
                    )
                else:
                    response = self._m365.get_group(group_name=group_name)
                    if response is None or "value" not in response or not response["value"]:
                        self.logger.error(
                            "Microsoft 365 group -> '%s' not found. Skipping...",
                            group_name,
                        )
                        success = False
                    else:
                        group_id = response["value"][0]["id"]

                        # Check if user is already a member. We don't want
                        # to throw an error if the user is not found as a member
                        # so we pass show_error=False:
                        if self._m365.is_member(
                            group_id,
                            m365_user_id,
                            show_error=False,
                        ):
                            self.logger.info(
                                "Microsoft 365 user -> '%s' (%s) is already in Microsoft 365 group -> '%s' (%s).",
                                user["name"],
                                m365_user_id,
                                group_name,
                                group_id,
                            )
                        else:
                            self.logger.info(
                                "Add Microsoft 365 user -> '%s' (%s) to Microsoft 365 group -> '%s' (%s)...",
                                user["name"],
                                m365_user_id,
                                group_name,
                                group_id,
                            )
                            response = self._m365.add_group_member(
                                group_id=group_id,
                                member_id=m365_user_id,
                            )
                            if not response:
                                self.logger.error(
                                    "Failed to add Microsoft 365 user -> '%s' (%s) to Microsoft 365 group -> '%s' (%s)!",
                                    user["name"],
                                    m365_user_id,
                                    group_name,
                                    group_id,
                                )
                                success = False

                            # As each group should have at least one owner in M365
                            # we set all users also as owners for now. Later we
                            # may want to configure this via payload:
                            self.logger.info(
                                "Make Microsoft 365 user -> '%s' (%s) owner of Microsoft 365 group -> '%s' (%s)...",
                                user["name"],
                                m365_user_id,
                                group_name,
                                group_id,
                            )
                            response = self._m365.add_group_owner(
                                group_id,
                                m365_user_id,
                            )
                            if not response:
                                self.logger.error(
                                    "Failed to make Microsoft 365 user -> '%s' (%s) owner of Microsoft 365 group -> '%s' (%s)!",
                                    user["name"],
                                    m365_user_id,
                                    group_name,
                                    group_id,
                                )
                                success = False

                # As M365 groups are flat (not nested), we also add the
                # user as member to the parent groups of the current group
                # if the parent group is enabled for M365:
                parent_group_names = group.get("parent_groups")
                self.logger.info(
                    "Group -> '%s' has the following parent groups -> %s",
                    group_name,
                    parent_group_names,
                )
                for parent_group_name in parent_group_names:
                    # Find the group dictionary item to the parent group name:
                    parent_group = next(
                        (item for item in self._groups if item["name"] == parent_group_name),
                        None,
                    )
                    if parent_group is None or "enable_o365" not in parent_group or not parent_group["enable_o365"]:
                        # if parent group is not in payload then this membership
                        # is not relevant for Microsoft 365.
                        # If Microsoft 365 is not enabled for this parent group in
                        # the payload we can also skip:
                        self.logger.info(
                            "Parent group -> '%s' is not enabled for M365. Skipping...",
                            group_name,
                        )
                        continue

                    response = self._m365.get_group(group_name=parent_group_name)
                    if response is None or "value" not in response or not response["value"]:
                        self.logger.error(
                            "Microsoft 365 group -> '%s' not found. Skipping...",
                            group_name,
                        )
                        success = False
                        continue
                    parent_group_id = response["value"][0]["id"]

                    # Check if user is already a member. We don't want
                    # to throw an error if the user is not found as a member:
                    if self._m365.is_member(
                        group_id=parent_group_id,
                        member_id=m365_user_id,
                        show_error=False,
                    ):
                        self.logger.info(
                            "Microsoft 365 user -> '%s' (%s) is already in Microsoft 365 group -> '%s' (%s).",
                            user["name"],
                            m365_user_id,
                            parent_group_name,
                            parent_group_id,
                        )
                    else:
                        self.logger.info(
                            "Add Microsoft 365 user -> '%s' (%s) to Microsoft 365 group -> '%s' (%s)...",
                            user["name"],
                            m365_user_id,
                            parent_group_name,
                            parent_group_id,
                        )
                        self._m365.add_group_member(
                            group_id=parent_group_id,
                            member_id=m365_user_id,
                        )
                # end for parent_group_name

                # Make this user follow the SharePoint site of his/her department.
                # We only do this for users that have a valid M365 license (SKU):
                if group_name == user_department and user["m365_skus"]:
                    group = self._m365.get_group(group_name=group_name)
                    group_id = self._m365.get_result_value(response=group, key="id")
                    if group_id:
                        group_site = self._m365.get_sharepoint_site_for_group(group_id=group_id)
                        group_site_id = self._m365.get_result_value(response=group_site, key="id")
                        if group_site_id:
                            group_site_name = self._m365.get_result_value(response=group_site, key="name")
                            # Make sure the user's mysite (drive) has been provisioned.
                            # This is a prerequisite for a user being able to follow a
                            # SharePoint site. For this to work we need "Files.ReadWrite"
                            # as a delegated permission in the Azure AppRegistration!
                            self.logger.info(
                                "Make sure user -> '%s' has a drive (mySite) provisioned...",
                                user["email"],
                            )
                            # We need to have 'delegated' permissions for this so we authenticate
                            # as the user... The scope is important here - the user's drive can only
                            # be provisioned if "Files.ReadWrite" scope is provided:
                            response = self._m365.authenticate_user(
                                username=user["email"], password=user["password"], scope="Files.ReadWrite"
                            )
                            if not response:
                                self.logger.error(
                                    "Couldn't authenticate as M365 user -> '%s' to provision user's drive!",
                                    username=user["email"],
                                )
                                success = False
                                continue
                            # Retrieve the drive endpoint to trigger the drive provisioning. It is important
                            # to use the 'me=True' to make sure the request is done with the user credentials,
                            # not the app credentials (using purely client_id / client_secret):
                            response = self._m365.get_user_drive(user_id=user["email"], me=True)
                            if not response:
                                self.logger.error("Couldn't get M365 drive of user -> '%s'!", user["email"])
                                success = False
                                continue
                            self.logger.info(
                                "Make user -> '%s' follow departmental SharePoint site -> '%s'...",
                                user["email"],
                                group_site_name,
                            )
                            response = self._m365.follow_sharepoint_site(site_id=group_site_id, user_id=m365_user_id)
                            if not response:
                                self.logger.warning(
                                    "User -> '%s' cannot follow SharePoint site -> '%s'!",
                                    user["email"],
                                    group_site_name,
                                )
                                success = False
                        # end if group_site_id:
                    # end if group_id:
                # end if group_name == user_department and user["m365_skus"]:
            # end for group name in group_names:
        # end for user

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_teams_m365")
    def process_teams_m365(self, section_name: str = "teamsM365") -> bool:
        """Process groups in payload and create matching Teams in Microsoft 365.

        We need to do this after the creation of the M365 users as we require
        Group Owners to create teams. These are NOT the teams for OTCS
        workspaces! Those are created by Scheduled Bots (Jobs) from OTCS!

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not isinstance(self._m365, M365):
            self.logger.error(
                "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
                section_name,
            )
            return False

        if not self._groups:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for group in self._groups:
            if "name" not in group:
                self.logger.error("Team needs a name. Skipping...")
                success = False
                continue
            group_name = group["name"]

            # Check if group has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not group.get("enabled", True):
                self.logger.info(
                    "Payload for group -> '%s' is disabled. Skipping...",
                    group_name,
                )
                continue

            if "enable_o365" not in group or not group["enable_o365"]:
                self.logger.info(
                    "Microsoft 365 is not enabled in payload for group -> '%s'. Skipping...",
                    group_name,
                )
                continue

            # Check if the M365 group does not exist (this should actually never happen at this point)
            m365_group_id = self.determine_group_id_m365(group=group)
            if not m365_group_id:
                # The "m365_id" value is set by the method process_groups_m365()
                self.logger.error(
                    "No M365 group exist for group -> '%s' (M365 group creation may have failed). Skipping...",
                    group_name,
                )
                success = False
                continue

            if self._m365.has_team(group_name=group_name):
                self.logger.info(
                    "M365 group -> '%s' already has an MS Team connected. Skipping...",
                    group_name,
                )
                continue

            self.logger.info(
                "Create M365 team -> '%s' for existing M365 group -> '%s'...",
                group_name,
                group_name,
            )
            # Now "upgrading" this group to a MS Team:
            new_team = self._m365.add_team(name=group_name)
            if not new_team:
                success = False
                continue

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._groups,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_teams_m365_apps")
    def process_teams_m365_apps(
        self,
        section_name: str = "teamsM365Apps",
        tab_name: str = "Extended ECM",
    ) -> bool:
        """Process groups in payload and configure Extended ECM Teams Apps.

        The app is exposed as a tab called "Extended ECM" in the "General"
        channel of the M365 Team.

        We need to do this after the transports as we need top level folders
        we can point the Extended ECM teams app to.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.
            tab_name (str, optional):
                Name of the Extended ECM tab. Default is "Extended ECM".

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not isinstance(self._m365, M365):
            self.logger.error(
                "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
                section_name,
            )
            return False

        if not self._groups:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # Determine the ID of the Extended ECM App:
        app_name = self._m365.config()["teamsAppName"]
        app_internal_id = self._m365.config()["teamsAppInternalId"]
        if not app_internal_id:
            response = self._m365.get_teams_apps(
                filter_expression="contains(displayName, '{}')".format(app_name),
            )
            # Get the App catalog ID:
            app_internal_id = self._m365.get_result_value(
                response=response,
                key="id",
                index=0,
            )
        if not app_internal_id:
            self.logger.error("M365 Teams app -> '%s' not found in catalog!", app_name)
            return False

        for group in self._groups:
            if "name" not in group:
                self.logger.error("Team needs a name. Skipping...")
                success = False
                continue
            group_name = group["name"]

            # Check if group has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not group.get("enabled", True):
                self.logger.info(
                    "Payload for group -> '%s' is disabled. Skipping...",
                    group_name,
                )
                continue

            if "enable_o365" not in group or not group["enable_o365"]:
                self.logger.info(
                    "Microsoft 365 is not enabled in payload for group -> '%s'. Skipping...",
                    group_name,
                )
                continue

            #
            # Now we create a tab in the "General" channel for the Extended ECM Teams App
            #

            # 1. Check if the tab is already assigned to the General channel.
            # This determines if we need to create or update the tab / app:
            app_name = self._m365.config()["teamsAppName"]
            response = self._m365.get_team_channel_tabs(
                team_name=group_name,
                channel_name="General",
            )
            # Check if tab is already there:
            if self._m365.exist_result_item(
                response=response,
                key="displayName",
                value=tab_name,
            ):
                self.logger.info(
                    "M365 Teams app -> '%s' is already configured for M365 Team -> '%s' (tab -> '%s' does already exist). Updating it with new URLs and IDs...",
                    app_name,
                    group_name,
                    tab_name,
                )
                update = True  # update existing tab
            else:
                self.logger.info(
                    "Add tab -> '%s' with app -> '%s' to channel -> 'General' of M365 Team -> '%s' ",
                    tab_name,
                    app_name,
                    group_name,
                )
                update = False  # create new tab

            # 2. Determine the M365 Team ID. If the team is not found then skip:
            response = self._m365.get_team(name=group_name)
            team_id = self._m365.get_result_value(response=response, key="id", index=0)
            if not team_id:
                self.logger.error("M365 Team -> '%s' not found!", group_name)
                success = False
                continue

            # 3. Install the App for the particular M365 Team (if it is not yet installed):
            response = self._m365.get_teams_apps_of_team(
                team_id=team_id,
                filter_expression="contains(teamsAppDefinition/displayName, '{}')".format(
                    app_name,
                ),
            )
            if self._m365.exist_result_item(
                response=response,
                key="displayName",
                value=app_name,
                sub_dict_name="teamsAppDefinition",
            ):
                self.logger.info(
                    "M365 Teams app -> '%s' is already installed for M365 Team -> '%s' (%s). Trying to upgrade app...",
                    app_name,
                    group_name,
                    team_id,
                )
                response = self._m365.upgrade_teams_app_of_team(
                    team_id=team_id,
                    app_name=app_name,
                )
                if not response:
                    self.logger.error(
                        "Failed to upgrade the existing app -> '%s' for the M365 Team -> '%s'!",
                        app_name,
                        group_name,
                    )
                    success = False
                    continue
            else:
                self.logger.info(
                    "Install M365 Teams app -> '%s' for M365 team -> '%s'...",
                    app_name,
                    group_name,
                )
                response = self._m365.assign_teams_app_to_team(
                    team_id=team_id,
                    app_id=app_internal_id,
                )
                if not response:
                    self.logger.error(
                        "Failed to install app -> '%s' (%s) for M365 Team -> '%s'!",
                        app_name,
                        app_internal_id,
                        group_name,
                    )
                    success = False
                    continue

            # 4. Create a Tab in the "General" channel of the M365 Team:
            if group_name == "Innovate":
                # Use the Enterprise Workspace for the
                # top-level group "Innovate":
                node_id = 2000
            else:
                # We assume the departmental group names are identical to
                # top-level folders in the Enterprise volume
                node = self._otcs.get_node_by_parent_and_name(
                    parent_id=2000,
                    name=group_name,
                )
                node_id = self._otcs.get_result_value(response=node, key="id")
                if not node_id:
                    self.logger.info(
                        "Cannot find a top-level folder for group -> '%s'. Cannot configure M365 Teams app. Skipping...",
                        group_name,
                    )
                    continue

            app_url = self._otcs_frontend.cs_support_public_url()  # it is important to use the frontend pod URL here
            app_url += "/xecmoffice/teamsapp.html?nodeId="
            app_url += str(node_id) + "&type=container&parentId=2000&target=content&csurl="
            app_url += self._otcs_frontend.cs_public_url()
            app_url += "&appId=" + app_internal_id

            if update:
                # App / Tab exist but needs to be updated with new
                # IDs for the new deployment of Extended ECM
                # as the M365 Teams survive between Terrarium deployments:

                self.logger.info(
                    "Updating tab -> '%s' of M365 Team channel -> 'General' for app -> '%s' (%s) with new URLs and node IDs...",
                    tab_name,
                    app_name,
                    app_internal_id,
                )

                response = self._m365.update_teams_app_of_channel(
                    team_name=group_name,
                    channel_name="General",
                    tab_name=tab_name,
                    app_url=app_url,
                    cs_node_id=node_id,
                )
            else:
                # Tab does not exist in "General" channel so we
                # add / configure it freshly:

                self.logger.info(
                    "Adding tab -> '%s' with app -> '%s' (%s) in M365 Team channel -> 'General'...",
                    tab_name,
                    app_name,
                    app_internal_id,
                )

                response = self._m365.add_teams_app_to_channel(
                    team_name=group_name,
                    channel_name="General",
                    app_id=app_internal_id,
                    tab_name=tab_name,
                    app_url=app_url,
                    cs_node_id=node_id,
                )
                if not response:
                    self.logger.error(
                        "Failed to add tab -> '%s' with app -> '%s' (%s) to M365 Team channel -> 'General'!",
                        tab_name,
                        app_name,
                        app_internal_id,
                    )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._groups,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="cleanup_stale_teams_m365")
    def cleanup_stale_teams_m365(self, workspace_types: list) -> bool:
        """Delete Microsoft Teams that are left-overs from former deployments.

        This method is currently not used.

        Args:
            workspace_types (list):
                The list of all workspace types.

        Returns:
            bool:
                True if successful, False otherwise.

        """

        if not isinstance(self._m365, M365):
            self.logger.error(
                "Microsoft 365 connection not setup properly. Skipping cleanup...",
            )
            return False

        if workspace_types == []:
            self.logger.error("Empty workspace type list!")
            return False

        for workspace_type in workspace_types:
            if "name" not in workspace_type:
                self.logger.error(
                    "Workspace type -> '%s' does not have a name. Skipping...",
                    workspace_type,
                )
                continue

            workspace_instances = self._otcs.get_workspace_instances_iterator(
                type_name=workspace_type["name"],
            )
            for workspace in workspace_instances:
                workspace = workspace.get("data").get("properties")

                workspace_name = workspace["name"]
                self.logger.info(
                    "Check if stale Microsoft 365 Teams -> '%s' exist...",
                    workspace_name,
                )
                self._m365.delete_teams(name=workspace_name)

        return True

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="cleanup_all_teams_m365")
    def cleanup_all_teams_m365(self, section_name: str = "teamsM365Cleanup") -> bool:
        """Delete Microsoft Teams that are left-overs from former deployments.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if teams have been deleted, False otherwise.

        """

        if not isinstance(self._m365, M365):
            self.logger.error(
                "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
                section_name,
            )
            return False

        # We want this cleanup to only run once even if we have
        # multiple payload files - so we pass payload_specific=False here:
        if self.check_status_file(
            payload_section_name=section_name,
            payload_specific=False,
        ):
            self.logger.info(
                "Payload section -> '%s' has been processed successfully before. Skip cleanup of M365 teams...",
                section_name,
            )
            return True

        self.logger.info("Processing payload section -> '%s'...", section_name)

        # We don't want to delete MS Teams that are matching the regular OTCS Group Names (like "Sales")
        exception_list = self.get_all_group_names()

        # These are the patterns that each MS Teams needs to match at least one of to be deleted
        # Pattern 1: all MS teams with a name that has a number in brackets, like "(1234)"
        # Pattern 2: all MS Teams with a name that starts with a number followed by a space,
        #            followed by a "-" and followed by another space
        # Pattern 3: all MS Teams with a name that starts with "WS" and a 1-4 digit number
        #            (these are the workspaces for Purchase Contracts generated for Intelligent Filing)
        # Pattern 4: all MS Teams with a name that ends with a 1-2 character + a number in brackets, like (US-1000)
        #            this is a specialization of pattern 1
        # Pattern 5: remove the teams that are created for the dummy copy&paste template for the
        #            Intelligent Filing workspaces
        pattern_list = [
            r"\(\d+\)",
            r"\d+\s-\s",
            r"^WS\d{1,4}$",
            r"^.+?\s\(.{1,2}-\d+\)$",
            r"Purchase\sContract\s\(Template\sfor\sIntelligent\sFiling\)",
            r"^OpenText.*$",
            r"^P-100.*$",
            r"^OILRIG.*$",
            r"^AGILUM.*$",
            r"^HD-102T.*$",
            r"^SG325A.*$",
            r"^[A-Za-z0-9]{18} - .*$",  # delete teams that start with the typical Salesforce IDs (e.g. opportunities)
            r".*\s\([A-Z]{3,4}\)$",  # delete stale Locations from NTSB scenario
        ]

        result = self._m365.delete_all_teams(
            exception_list=exception_list,
            pattern_list=pattern_list,
        )

        # We want this cleanup to only run once even if we have
        # multiple payload files - so we pass payload_specific=False here:
        self.write_status_file(
            success=True,
            payload_section_name=section_name,
            payload_section=exception_list + pattern_list,
            payload_specific=False,
        )

        return result

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_sites_m365")
    def process_sites_m365(self, section_name: str = "sitesM365") -> bool:
        """Process M365 groups in payload and configure SharePoint sites in Microsoft 365.

        These are NOT the SharePoint sites for Business Workspaces which are created
        by Scheduled Bots (Jobs) from OTCS via the creation of MS teams
        (each MS Team has a SharePoint site behind it)!

        These are the SharePoint sites for the departmental groups such as "Sales",
        "Procurement", "Enterprise Asset Management", ...
        Only departmental group that have a top-level folder with the exact same
        name as the Department are configured.

        For each departmental group:
        1. Determine a departmental folder in the Enterprise Workspace
        2. Determine the M365 Group
        3. Determine the SharePoint Site (based on the M365 group ID)
        4. Determine the Page in the SharePoint site
        5. Determine or create the SharePoint webpart for the OTCS browser
        6. Create URL object pointing to SharePoint site inside top level department folder

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not isinstance(self._m365, M365):
            self.logger.error(
                "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
                section_name,
            )
            return False

        if not self._groups:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for group in self._groups:
            if "name" not in group:
                self.logger.error("Group needs a name. Cannot configure SharePoint site for it. Skipping...")
                success = False
                continue
            group_name = group["name"]

            # Check if group has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not group.get("enabled", True):
                self.logger.info(
                    "Payload for group -> '%s' is disabled. Cannot configure SharePoint site. Skipping...",
                    group_name,
                )
                continue

            if "enable_o365" not in group or not group["enable_o365"]:
                self.logger.info(
                    "Microsoft 365 is not enabled in payload for group -> '%s'. Skipping...",
                    group_name,
                )
                continue

            #
            # 1. Determine a departmental folder in the Enterprise Workspace:
            #

            if group_name == "Innovate":
                folder_id = 2000
            else:
                folder = self._otcs.get_node_by_volume_and_path(
                    volume_type=self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE,
                    path=[group_name],
                    show_error=False,
                )
                folder_id = self._otcs.get_result_value(response=folder, key="id")
            if not folder_id:
                self.logger.warning(
                    "Group -> '%s' has no folder associated. Cannot configure SharePoint site. Skipping...",
                    group_name,
                )
                continue

            #
            # 2. Determine the M365 Group
            #

            # Check if the M365 group does not exist (this should actually never happen at this point)
            m365_group_id = self.determine_group_id_m365(group=group)
            if not m365_group_id:
                # The "m365_id" value is set by the method process_groups_m365()
                self.logger.error(
                    "No M365 group exist for group -> '%s' (M365 group creation may have failed). Skipping...",
                    group_name,
                )
                success = False
                continue

            #
            # 3. Determine the SharePoint Site:
            #

            site = self._m365.get_sharepoint_site_for_group(group_id=m365_group_id)
            site_id = self._m365.get_result_value(response=site, key="id")
            if not site_id:
                self.logger.info(
                    "M365 group -> '%s' has no M365 SharePoint site connected. Skipping...",
                    group_name,
                )
                success = False
                continue
            site_name = self._m365.get_result_value(response=site, key="name")

            # Store the SharePoint site ID in the payload.
            group["m365_site_id"] = site_id
            group["m365_site_name"] = site_name
            group["m365_folder_id"] = folder_id

            self.logger.info(
                "Configure M365 SharePoint site -> '%s' (%s) for M365 group -> '%s'...",
                site_name,
                site_id,
                group_name,
            )

            #
            # 4. Determine the Page in the SharePoint site:
            #

            site_page_name = group.get("o365_site_page_name", "OpenText Content Management")

            site_page_id = None
            site_pages = self._m365.get_sharepoint_pages(site_id=site_id)
            if site_pages:
                site_page_id = self._m365.lookup_result_value(
                    response=site_pages,
                    key="title",
                    value=site_page_name,
                    return_key="id",
                )
                if site_page_id:
                    self.logger.info(
                        "Found existing page -> '%s' (%s) for SharePoint site -> '%s' (%s).",
                        site_page_name,
                        site_page_id,
                        site_name,
                        site_id,
                    )
            if not site_page_id:
                site_page = self._m365.add_sharepoint_page(site_id=site_id, page_name=site_page_name)
                if site_page:
                    site_page_id = self._m365.get_result_value(response=site_page, key="id")
                    self.logger.info(
                        "Successfully created page -> '%s' (%s) for SharePoint site -> '%s' (%s).",
                        site_page_name,
                        site_page_id,
                        site_name,
                        site_id,
                    )
                else:
                    self.logger.error(
                        "Failed to create page -> '%s' for SharePoint site -> '%s' (%s)!",
                        site_page_name,
                        site_name,
                        site_id,
                    )
                    success = False
                    continue

            #
            # 5. Determine or create the SharePoint webpart for the OTCS browser:
            #

            # Get all webparts on the page:
            site_webparts = self._m365.get_sharepoint_webparts(site_id=site_id, page_id=site_page_id)
            # Check if there's already a webpart for the OTCS browser:
            site_webpart_id = self._m365.lookup_result_value(
                response=site_webparts,
                key="webPartType",
                value="cecfdba4-2e82-4538-9436-dbd1c4c01a80",  # OTCS Browser Type
                return_key="id",
            )
            if site_webpart_id:
                self.logger.info(
                    "Found existing OTCS Browser webpart -> '%s'. Updating with folder ID -> %s...",
                    site_webpart_id,
                    str(folder_id),
                )
                # Update the webpart with the new ID (which has changed after redeployment):
                update_data = {
                    "properties": {
                        "ContentServerFolderParentWP": "2000",
                        "ContentServerFolderSelectedWP": str(folder_id),
                    },
                }
                response = self._m365.update_sharepoint_webpart(
                    site_id=site_id,
                    page_id=site_page_id,
                    webpart_id=site_webpart_id,
                    update_data=update_data,
                )
                if response:
                    self.logger.info(
                        "Successfully updated OTCS Browser webpart -> '%s' with new folder ID -> %s.",
                        site_webpart_id,
                        str(folder_id),
                    )
                else:
                    self.logger.error(
                        "Failed to update OTCS Browser webpart -> '%s' with new folder ID -> %s!",
                        site_webpart_id,
                        str(folder_id),
                    )
                    success = False
                    continue
            else:
                # Check if the section we want for the webpart does already
                # exist. Otherwise create it. Per default we use horizontal
                # section 2. This allows to have a banner as first section
                # that is not affected by the customizer and can be configured
                # manually:
                site_page_section_id = group.get("o365_site_page_section_id", 1)
                site_page_section_type = group.get("o365_site_page_section_type", "horizontalSections")
                self.logger.info(
                    "Check if %s section -> %s on page -> '%s' (%s) of SharePoint site -> '%s' (%s) does already exist...",
                    "horizontal" if site_page_section_type == "horizontalSections" else "vertical",
                    site_page_section_id,
                    site_page_name,
                    site_page_id,
                    site_name,
                    site_id,
                )
                site_page_section = self._m365.get_sharepoint_section(
                    site_id=site_id,
                    page_id=site_page_id,
                    section_type=site_page_section_type,
                    section_id=site_page_section_id,
                    show_error=False,
                )
                if not site_page_section:
                    site_page_section = self._m365.add_sharepoint_section(
                        site_id=site_id,
                        page_id=site_page_id,
                        section_type=site_page_section_type,
                        section_id=site_page_section_id,
                    )
                    if not site_page_section:
                        success = False
                        continue
                    self.logger.info(
                        "Created %s section -> %s on page -> '%s' (%s) of SharePoint site -> '%s' (%s)",
                        "horizontal" if site_page_section_type == "horizontalSections" else "vertical",
                        site_page_section_id,
                        site_page_name,
                        site_page_id,
                        site_name,
                        site_id,
                    )
                else:
                    self.logger.info(
                        "Using existing %s section -> %s on page -> '%s' (%s) of SharePoint site -> '%s' (%s)",
                        "horizontal" if site_page_section_type == "horizontalSections" else "vertical",
                        site_page_section_id,
                        site_page_name,
                        site_page_id,
                        site_name,
                        site_id,
                    )

                # If the sharepointAppRootSite is not configured
                # we try to extract the site URL from the site ID
                # which typically has a format like this:
                # ideateqa.sharepoint.com,2c59000d-f3e7-44d1-9a8e-e5df82b8ab01,34b48533-af41-4743-8b41-185a21f0b80f
                site_url = (
                    self._m365.config()["sharepointAppRootSite"]
                    if self._m365.config()["sharepointAppRootSite"]
                    else "https://" + site_id.split(",", 1)[0]
                )
                # Build the web part create data:
                create_data = {
                    "@odata.type": "microsoft.graph.webPartData",
                    "audiences": [],
                    "dataVersion": "1.0",
                    "description": "Browse, access and work with documents from OpenText Content Server.",
                    "title": "Content Server Browser",
                    "properties": {
                        "AzureAppId": self._m365.config()["clientId"],
                        "ContentServerURLWP": "",
                        "URLPrefixWP": "",
                        "SSOEnabledWP": "",
                        "ShowPersonalWorkspaceWP": "",
                        "ShowNavigationBreadcrumbWP": "",
                        "PageSizeWP": "",
                        "ContentServerFolderParentWP": "2000",
                        "ContentServerFolderSelectedWP": str(folder_id),
                        "ContentServerFolderDisplayWP": group_name,
                        "SettingStorageURLSite": site_url,
                        "ContentServerURLSite": self._otcs.config()["csPublicUrl"],
                        "URLPrefixSite": "/cssupport",
                        "ShowPersonalWorkspaceSite": "",
                        "ShowNavigationBreadcrumbSite": "",
                        "PageSizeSite": "",
                        "ContentServerFolderParentSite": "",
                        "ContentServerFolderSelectedSite": "",
                        "ContentServerFolderDisplaySite": "",
                        "SSOEnabledSite": "",
                        "SettingStorageURLGL": "{}/sites/appcatalog".format(
                            site_url,
                        ),
                        "ContentServerURLGL": self._otcs.config()["csPublicUrl"],
                        "URLPrefixGL": "/cssupport",
                        "ShowPersonalWorkspaceGL": "No",
                        "ShowNavigationBreadcrumbGL": "No",
                        "PageSizeGL": "",
                        "ContentServerFolderParentGL": "",
                        "ContentServerFolderSelectedGL": "",
                        "ContentServerFolderDisplayGL": "",
                        "SSOEnabledGL": "No",
                        "TargetPlatform": "SPOnline",
                        "ConfigurationSiteUrl": "",
                        "WebPartVersion": "23.4.0.0",
                        "ContentServerURLAltGL": "",
                        "SavedQueryIdGL": "2344",
                        "SavedQueryParentIdGL": "2329",
                        "SavedQueryNameGL": "Full Text Business Workspaces Search",
                        "isCurrentUserSiteAdmin": True,
                        "isCurrentUserGlobalAdmin": True,
                        "isTeamsContext": False,
                        "ContentServerUrlLocal": self._otcs.config()["csPublicUrl"],
                    },
                }

                response = self._m365.add_sharepoint_webpart(
                    site_id=site_id,
                    page_id=site_page_id,
                    webpart_type_id="cecfdba4-2e82-4538-9436-dbd1c4c01a80",
                    section_id=site_page_section_id,
                    create_data=create_data,
                )
                if response:
                    self.logger.info(
                        "Successfully added OTCS browser webpart -> '%s' to page -> '%s' (%s) on site ->'%s' (%s).",
                        self._m365.get_result_value(response=response, key="id"),
                        site_page_name,
                        site_page_id,
                        site_name,
                        site_id,
                    )
                else:
                    self.logger.error(
                        "Failed to add OTCS browser webpart to page -> '%s' (%s) on site ->'%s' (%s)!",
                        site_page_name,
                        site_page_id,
                        site_name,
                        site_id,
                    )
                    success = False
                # end else
            # end else

            #
            # 6. Create URL object pointing to SharePoint site inside top level department folder
            #

            item_name = (
                "SharePoint site for {} department".format(group_name)
                if folder_id != 2000
                else "SharePoint site for Innovate"
            )
            response = self._otcs.get_node_by_parent_and_name(parent_id=folder_id, name=item_name)
            item_id = self._otcs.get_result_value(response=response, key="id")
            if not item_id:
                response = self._otcs.create_item(
                    parent_id=folder_id,
                    item_type=self._otcs.ITEM_TYPE_URL,
                    item_name=item_name,
                    url=site["webUrl"],
                )
                self.logger.info(
                    "Created URL item -> '%s' (%s).",
                    item_name,
                    site["webUrl"],
                )
            else:
                self.logger.info(
                    "URL item -> '%s' (%s) does already exist.",
                    item_name,
                    site["webUrl"],
                )
        # end for group in self._groups:

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._groups,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_admin_settings")
    def process_admin_settings(
        self,
        admin_settings: list,
        section_name: str = "adminSettings",
    ) -> bool:
        """Process admin settings in payload and import them to Content Server.

            The payload section is a list of dicts with these items:
            {
                enabled: True or False to enable or disable the payload item
                filename: The filename of the XML file with admin settings.
                          It needs to be the plain filename like "admin.xml".
                          The files reside inside the container in /settings root
                          directory. They are placed there by the Terraform automation
                          and are taken from the ./settings/payload directory.
                description: Some description about the purpose of the settings.
                             Just for information and optional.
            }

        Args:
            admin_settings (list):
                List of admin settings. We need this parameter
                as we process two different lists.
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if a restart of the OTCS pods is required. False otherwise.

        """

        if not admin_settings:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return False  # important to return False here as otherwise we are triggering a restart of services!!

        # If this payload section has been processed successfully before we
        # can return False and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return False  # important to return False here as otherwise we are triggering a restart of services!!

        restart_required: bool = False
        success: bool = True

        for admin_setting in admin_settings:
            # Sanity checks:
            if "filename" not in admin_setting:
                self.logger.error(
                    "Filename is missing. Skipping to next admin setting...",
                )
                continue
            filename = admin_setting["filename"]

            # Check if admin setting has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not admin_setting.get("enabled", True):
                self.logger.info(
                    "Payload for setting file -> '%s' is disabled. Skipping...",
                    filename,
                )
                continue

            settings_file = self._custom_settings_dir + filename
            if os.path.exists(settings_file):
                description = admin_setting.get("description")
                self.logger.info(
                    "Processing admin settings from file -> '%s'%s...",
                    filename,
                    " ({})".format(description) if description else "",
                )

                # Read the config file:
                with open(settings_file, encoding="utf-8") as file:
                    file_content = file.read()

                self.logger.debug(
                    "Replace Placeholder -> '%s' in file -> %s",
                    self._placeholder_values,
                    file_content,
                )

                file_content = self.replace_placeholders(content=file_content)

                # Write the updated config file:
                tmpfile = os.path.join(tempfile.gettempdir(), os.path.basename(settings_file))
                with open(tmpfile, "w", encoding="utf-8") as file:
                    file.write(file_content)

                response = self._otcs.apply_config(xml_file_path=tmpfile)
                if response and response["results"]["data"]["restart"]:
                    self.logger.info(
                        "A restart of the Content Server service is required.",
                    )
                    restart_required = True

                    if admin_setting.get("restart", False):
                        self.logger.info(
                            "Immediate restart requested - restart of OTCS services...",
                        )
                        # Restart OTCS frontend and backend pods:
                        self._otcs_restart_callback(
                            backend=self._otcs_backend,
                            frontend=self._otcs_frontend,
                        )

                        restart_required = False

            else:
                self.logger.error(
                    "Admin settings file -> '%s' not found!",
                    settings_file,
                )
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=admin_settings,
        )

        return restart_required

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="check_external_system")
    def check_external_system(self, external_system: dict) -> bool:
        """Check if external system is reachable.

        Args:
            external_system (dict):
                The payload data structure of external system.
                We assume here that sanity check for valid data is already done before.

        Returns:
            bool:
                True = system is reachable, False otherwise.

        """

        as_url = external_system["as_url"]

        # Extract the hostname:
        external_system_hostname = urlparse(as_url).hostname
        # Write this information back into the data structure:
        external_system["external_system_hostname"] = external_system_hostname
        # Extract the port:
        external_system_port = urlparse(as_url).port if urlparse(as_url).port else 80
        # Write this information back into the data structure:
        external_system["external_system_port"] = external_system_port

        if self._http_object.check_host_reachable(
            hostname=external_system_hostname,
            port=external_system_port,
        ):
            self.logger.info(
                "Mark external system -> '%s' as reachable for later workspace creation and user synchronization...",
                external_system["external_system_name"],
            )
            external_system["reachable"] = True
            return True
        else:
            external_system["reachable"] = False
            return False

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_external_systems")
    def process_external_systems(self, section_name: str = "externalSystems") -> bool:
        """Process external systems in payload and create them in Content Server.

            The payload section is a list of dicts (each representing one external
            system) with these items:
            {
                enabled: True or False to enable or disable the payload item
                external_system_name: Name of the external systen.
                external_system_type: Type of the external system.
                                      Possible values are
                                      * SAP
                                      * SuccessFactors
                                      * Salesforce
                                      * Guidewire
                                      * AppWorks Platform
                                      * Business Scenario Sample
                base_url: Base URL of the external system
                as_url: Application Server URL of the external system
                username: (Technical) User Name for the connection
                password: Passord of the (technical) user
                oauth_client_id: OAuth client ID
                oauth_client_secret: OAuth client secret
                archive_logical_name: Logical name of Archive for SAP
                archive_certificate_file: Path and filename to certificate file.
                                          This file is inside the customizer
                                          pof file system.
                skip_connection_test: Should we skip the connection test for this
                                      external system?
            }
            If OAuth Client ID and Client Secret are provided then username
            and password are no longer used.

            In the payload for SAP external systems there are additional
            items "client", "destination" that are processed by init_sap()

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        Side Effects:
            - based on system_type different other settings in the dict are set
            - reachability is tested and a flag is set in the payload dict

        """

        if not self._external_systems:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # Even if this payload section has been processed successfully before we
        # cannot skip processing it once more to re-initialize self._sap and self._salesforce!

        success: bool = True

        for external_system in self._external_systems:
            #
            # 1: Do sanity checks for the payload:
            #
            if "external_system_name" not in external_system:
                self.logger.error(
                    "External System connection needs a logical system name! Skipping to next external system...",
                )
                success = False
                continue
            system_name = external_system["external_system_name"]

            if "external_system_type" not in external_system:
                self.logger.error(
                    "External System connection -> '%s' needs a type (SAP, Salesfoce, SuccessFactors, AppWorks Platform)! Skipping...",
                    system_name,
                )
                success = False
                continue
            system_type = external_system["external_system_type"]

            self._log_header_callback(
                text="Process External System -> '{}' ({})".format(system_name, system_type),
                char="-",
            )

            # Check if external system has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not external_system.get("enabled", True):
                self.logger.info(
                    "Payload for External System -> '%s' (%s) is disabled. Skipping...",
                    system_name,
                    system_type,
                )
                continue

            """
            Possible Connection Types for external systems:
            "Business Scenario Sample" (Business Scenarios Sample Adapter)
            "ot.sap.c4c.SpiAdapter" (SAP C4C SPI Adapter)
            "ot.sap.c4c.SpiAdapterV2" (C4C SPI Adapter V2)
            "HTTP" (Default WebService Adapter)
            "ot.sap.S4HANAAdapter" (S/4HANA SPI Adapter)
            "SF" (SalesForce Adapter)
            "SFInstance" (SFWebService)
            "GWInstance" (Guidewire Adapter)
            """

            # Set the default settings for the different system types:
            match system_type:
                # Check if we have a SuccessFactors system:
                case "SuccessFactors":
                    connection_type = "SFInstance"
                    auth_method = "OAUTH"
                    username = None
                    password = None
                case "SAP":
                    connection_type = "HTTP"
                    auth_method = "BASIC"
                    oauth_client_id = None
                    oauth_client_secret = None
                case "Salesforce":
                    connection_type = "SF"
                    auth_method = "OAUTH"
                    username = None
                    password = None
                case "Guidewire":
                    connection_type = "GWInstance"
                    auth_method = "BASIC"
                    oauth_client_id = None
                    oauth_client_secret = None
                case "AppWorks Platform":
                    connection_type = "HTTP"
                    auth_method = "BASIC"
                    oauth_client_id = None
                    oauth_client_secret = None
                case "Business Scenario Sample":
                    connection_type = "Business Scenario Sample"
                    auth_method = "BASIC"
                    oauth_client_id = None
                    oauth_client_secret = None
                case _:
                    self.logger.error(
                        "Unsupported system_type defined -> '%s'",
                        system_type,
                    )
                    return False

            base_url = external_system.get("base_url", "")

            if "as_url" not in external_system:
                self.logger.warning(
                    "External system connection -> '%s' needs an application server URL! Skipping to next external system...",
                    system_name,
                )
                success = False
                continue
            as_url = external_system["as_url"]

            self.logger.info(
                "Processing external system -> '%s' (type -> '%s', connection type -> '%s', endpoint -> '%s')...",
                system_name,
                system_type,
                connection_type,
                as_url,
            )

            skip_connection_test = external_system.get("skip_connection_test", False)
            # If skip_connection_test is not enabled in payload and the external system is
            # not of type 'Business Scenario Sample', we run the external system check:
            if not skip_connection_test and system_type != "Business Scenario Sample":
                # Check if external system is reachable and
                # update the payload dict with a "reachable" key/value pair:
                if not self.check_external_system(external_system=external_system):
                    self.logger.warning(
                        "External System connection -> '%s' (%s) is not reachable! Skipping to next external system...",
                        system_name,
                        system_type,
                    )
                    success = False
                    continue
            else:
                self.logger.info(
                    "Skipped reachability check for external system -> '%s' (%s)...",
                    system_name,
                    system_type,
                )

            # Read either username/password (BASIC) or client ID / secret (OAuth)
            match auth_method:
                case "BASIC":
                    if "username" not in external_system:
                        self.logger.warning(
                            "External System connection -> '%s' needs a user name for BASIC authentication! Skipping to next external system...",
                            system_name,
                        )
                        continue
                    if "password" not in external_system:
                        self.logger.warning(
                            "External System connection -> '%s' needs a password for BASIC authentication! Skipping to next external system...",
                            system_name,
                        )
                        continue
                    username = external_system["username"]
                    password = external_system["password"]
                    oauth_client_id = ""
                    oauth_client_secret = ""

                case "OAUTH":
                    if "oauth_client_id" not in external_system:
                        self.logger.error(
                            "External System connection -> '%s' is missing OAuth client ID! Skipping to next external system...",
                            system_name,
                        )
                        success = False
                        continue
                    if "oauth_client_secret" not in external_system:
                        self.logger.error(
                            "External System connection -> '%s' is missing OAuth client secret! Skipping to next external system...",
                            system_name,
                        )
                        success = False
                        continue
                    oauth_client_id = external_system["oauth_client_id"]
                    oauth_client_secret = external_system["oauth_client_secret"]
                    # For backward compatibility we also read username/password
                    # with OAuth settings:
                    username = external_system["username"] if external_system.get("username") else None
                    password = external_system["password"] if external_system.get("password") else None
                case _:
                    self.logger.error(
                        "Unsupported authorization method specified (%s) , Skipping ... ",
                        auth_method,
                    )
                    return False
            # end match

            # We do this existance test late in this function to make sure the payload
            # datastructure is properly updated for debugging purposes.
            self.logger.info(
                "Test if external system -> '%s' does already exist...",
                system_name,
            )
            if self._otcs.get_external_system_connection(connection_name=system_name):
                self.logger.info(
                    "External system connection -> '%s' already exists.",
                    system_name,
                )
                # This is for handling re-runs of customizer pod where the transports
                # are skipped and thus self._sap or self._salesforce may not be initialized:
                if system_type == "SAP" and not self._sap:
                    self.logger.info(
                        "Re-Initialize SAP connection for external system -> '%s'...",
                        system_name,
                    )
                    # Initialize SAP object responsible for communication to SAP:
                    self._sap = self.init_sap(external_system)
                if system_type == "Salesforce" and not self._salesforce:
                    self.logger.info(
                        "Re-Initialize Salesforce connection for external system -> '%s'...",
                        system_name,
                    )
                    # Initialize Salesforce object responsible for communication to Salesforce:
                    self._salesforce = self.init_salesforce(
                        salesforce_external_system=external_system,
                    )
                if system_type == "SuccessFactors" and not self._successfactors:
                    self.logger.info(
                        "Re-Initialize SuccessFactors connection for external system -> '%s'...",
                        system_name,
                    )
                    # Initialize SuccessFactors object responsible for communication to SuccessFactors:
                    self._successfactors = self.init_successfactors(
                        sucessfactors_external_system=external_system,
                    )
                if system_type == "Guidewire" and "policy" in system_name.lower() and not self._guidewire_policy_center:
                    self.logger.info(
                        "Re-Initialize Guidewire connection for external system -> '%s'...",
                        system_name,
                    )
                    # Initialize Guidewire object responsible for communication to Guidewire Policy Center:
                    self._guidewire_policy_center = self.init_guidewire(
                        guidewire_external_system=external_system,
                    )
                if system_type == "Guidewire" and "claim" in system_name.lower() and not self._guidewire_claims_center:
                    self.logger.info(
                        "Re-Initialize Guidewire connection for external system -> '%s'...",
                        system_name,
                    )
                    # Initialize Guidewire object responsible for communication to Guidewire Claims Center:
                    self._guidewire_claims_center = self.init_guidewire(
                        guidewire_external_system=external_system,
                    )

                continue

            #
            # Create External System:
            #

            response = self._otcs.add_external_system_connection(
                connection_name=system_name,
                connection_type=connection_type,
                as_url=as_url,
                base_url=base_url,
                username=str(username),
                password=str(password),
                authentication_method=auth_method,
                client_id=oauth_client_id,
                client_secret=oauth_client_secret,
            )
            if response is None:
                self.logger.error(
                    "Failed to create external system -> '%s'; type -> '%s'!",
                    system_name,
                    connection_type,
                )
                success = False
            else:
                self.logger.info(
                    "Successfully created external system -> '%s'.",
                    system_name,
                )

            #
            # In case of an SAP external system we also initialize the SAP object
            #
            if system_type == "SAP":
                # Initialize SAP object responsible for communication to SAP:
                self._sap = self.init_sap(sap_external_system=external_system)

            #
            # In case of an SuccessFactors external system we also initialize the SuccessFactors object
            #
            if system_type == "SuccessFactors":
                # Initialize SuccessFactors object responsible for communication to SuccessFactors:
                self._successfactors = self.init_successfactors(
                    sucessfactors_external_system=external_system,
                )

            #
            # In case of an Salesforce external system we also initialize the Salesforce object
            #
            if system_type == "Salesforce":
                # Initialize Salesforce object responsible for communication to Salesforce:
                self._salesforce = self.init_salesforce(
                    salesforce_external_system=external_system,
                )

            #
            # In case of an Guidewire external system we also initialize the Guidewire objects:
            #
            if system_type == "Guidewire":
                if "claim" in system_name.lower():
                    # Initialize Guidewire Claim Center object:
                    self._guidewire_claims_center = self.init_guidewire(
                        guidewire_external_system=external_system,
                    )
                elif "policy" in system_name.lower():
                    # Initialize Guidewire Policy Center object:
                    self._guidewire_policy_center = self.init_guidewire(
                        guidewire_external_system=external_system,
                    )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._external_systems,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="lookup_external_system")
    def lookup_external_system(self, ext_system_id: str, prefix: str = "success_payload_") -> dict | None:
        """Lookup an external system.

        Considers the current payload but also payload processed before (which is stored
        as JSON file in the Admin Personal Workspace).

        TODO: This is a workaround as the REST API for external systems is too limited
        and does not return the actual configuration settings of external systems.
        Check for newer OTCS versions.

        Args:
            ext_system_id (str):
                The external system name to lookup.
            prefix (str):
                The prefix of the success file in the Admin personal workspace.

        Returns:
            dict | None:
                The configuration data of the external system.

        """

        # First check if external system is in current payload:
        external_system = next(
            (item for item in self._external_systems if item["external_system_name"] == ext_system_id), None
        )
        if external_system:
            self.logger.info("Found external system -> '%s' declared in current payload.", ext_system_id)
            return external_system

        # Now we try to load it from previous processed payloads:

        if not self.check_status_file(payload_section_name="externalSystems", payload_specific=False, prefix=prefix):
            self.logger.error("Cannot find external system -> '%s'", ext_system_id)
            return None

        additional_systems = self.get_status_file(
            payload_section_name="externalSystems", payload_specific=False, prefix=prefix
        )

        # Merge avoiding duplicates and only enabled entries. existing_names is a set:
        existing_names = {item["external_system_name"] for item in self._external_systems}
        for sys in additional_systems:
            if sys["enabled"] and sys["external_system_name"] not in existing_names:
                self._external_systems.append(sys)
                existing_names.add(sys["external_system_name"])

        # Try finding the external system payload again after merging:
        external_system = next(
            (item for item in self._external_systems if item["external_system_name"] == ext_system_id), None
        )

        if external_system:
            self.logger.info("Found external system -> '%s' in previously processed payload file.", ext_system_id)
        else:
            self.logger.error(
                "Cannot find external system -> '%s' in list of external systems -> %s!",
                ext_system_id,
                str(existing_names),
            )

        return external_system

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="download_transport_package")
    def download_transport_package(
        self,
        package_url: str,
        download_dir: str | None = None,
    ) -> str | None:
        """Download the transort package from the given URL.

        Args:
            package_url (str):
                The URL to the transport package.
            package_name (str):
                The name of the transport package.
            download_dir (str, optional):
                The file system directory to download to. If None,
                a temporary directory is automatically determined.

        Returns:
            str | None:
                Path of the transport package in local file system or None in case of an error.

        """

        if not download_dir:
            download_dir = os.path.join(tempfile.gettempdir(), "transports/")

        if not self._http_object:
            self._http_object = HTTP(logger=self.logger)

        # Parse the URL
        parsed_url = urlparse(package_url)

        # Extract the path from the URL
        path = parsed_url.path

        # Get the file name from the path
        file_name = os.path.basename(path)

        download_name = os.path.join(download_dir, file_name)

        os.makedirs(download_dir, exist_ok=True)

        if not self._http_object.download_file(
            url=package_url,
            filename=download_name,
            show_error=True,
        ):
            return None

        self.logger.info(
            "Successfully downloaded transport package from -> '%s' to local file -> '%s'.",
            package_url,
            download_name,
        )

        return download_name

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_transport_packages")
    def process_transport_packages(
        self,
        transport_packages: list,
        section_name: str = "transportPackages",
    ) -> bool:
        """Process transport packages in payload and import them to Content Server.

        Args:
            transport_packages (list):
                A list of transport packages. As we have three different lists (transport,
                content_transport, transport_post) we need a parameter to pass it.
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not transport_packages:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for transport_package in transport_packages:
            name = transport_package.get("name")
            if not name:
                self.logger.error(
                    "Transport package needs a name! Skipping to next transport...",
                )
                success = False
                continue

            # Check if transport package has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not transport_package.get("enabled", True):
                self.logger.info(
                    "Payload for transport package -> '%s' is disabled. Skipping...",
                    name,
                )
                continue

            url = transport_package.get("url")
            if not url:
                self.logger.error(
                    "Transport package -> '%s' needs a URL! Skipping to next transport...",
                    name,
                )
                success = False
                continue

            description = transport_package.get("description", "")
            if not description:
                self.logger.warning(
                    "Transport package -> '%s' is missing a description.",
                    name,
                )

            # if the transport is not in the local file system
            # but given by an URL we download it first:
            if url.startswith("http"):
                package_path = self.download_transport_package(package_url=url)
                if not package_path:
                    success = False
                    continue
                url = package_path

            # For some transports there can be string replacements
            # configured:
            if "replacements" in transport_package:
                replacements = transport_package["replacements"]
                self.logger.info(
                    "Transport -> '%s' has replacements -> %s.",
                    name,
                    str(replacements),
                )
            else:
                replacements = None

            # For some transports there can be data extractions
            # configured:
            if "extractions" in transport_package:
                extractions = transport_package["extractions"]
                self.logger.info(
                    "Transport -> '%s' has extractions -> %s.",
                    name,
                    str(extractions),
                )
            else:
                extractions = None

            if description:
                self.logger.info("Deploy transport -> '%s' ('%s')...", name, description)
            else:
                self.logger.info("Deploy transport -> '%s'...", name)

            response = self._otcs.deploy_transport(
                package_url=url,
                package_name=name,
                package_description=description,
                replacements=replacements,
                extractions=extractions,
            )
            if response is None:
                self.logger.error("Failed to deploy transport -> '%s'!", name)
                success = False
                if self._stop_on_error:
                    break  # stop the for loop
            else:
                self.logger.info("Successfully deployed transport -> '%s'.", name)
                # Save the extractions for later processing, e.g. in process_business_object_types()
                if extractions:
                    self.add_transport_extractions(extractions=extractions)
        # end for transports

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=transport_packages,
        )
        self.write_status_file(
            success=success,
            payload_section_name=section_name + "Extractions",
            payload_section=self._transport_extractions,
        )

        # If stop_on_error is enabled we want to completely
        # stop the execution of the customizer and bail out:
        if not success and self._stop_on_error:
            raise StopOnError(message="STOP_ON_ERROR enabled -> Stopping execution")

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_photos")
    def process_user_photos(self, section_name: str = "userPhotos") -> bool:
        """Process user photos in payload and assign them to Content Server users.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # we assume the nickname of the photo item equals the login name of the user
        # we also assume that the photos have been uploaded / transported into the target system
        for user in self._users:
            user_name = user.get("name")
            if not user_name:
                self.logger.error("User is missing a login. Skipping to next user...")
                success = False
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            # We skip also user of type "ServiceUser":
            if user.get("type", "User") == "ServiceUser":
                self.logger.info("Skipping service user -> '%s'...", user_name)
                continue

            user_id = user.get("id")
            if not user_id:
                self.logger.error(
                    "User -> '%s' does not have an ID. The user creation may have failed before. Skipping...",
                    user_name,
                )
                success = False
                continue

            response = self._otcs.get_node_from_nickname(nickname=user_name)
            if response is None:
                self.logger.warning(
                    "Missing photo for user -> '%s' - nickname not found. Skipping...",
                    user_name,
                )
                continue
            photo_id = self._otcs.get_result_value(response=response, key="id")
            response = self._otcs.update_user_photo(user_id=user_id, photo_id=photo_id)
            if not response:
                self.logger.error("Failed to add photo for user -> '%s'!", user_name)
                success = False
            else:
                self.logger.info("Successfully added photo for user -> '%s'.", user_name)

        # Check if Admin has a photo as well (nickname needs to be "admin"):
        response = self._otcs.get_node_from_nickname(nickname="admin")
        if response is None:
            self.logger.warning(
                "Missing photo for admin - nickname not found. Skipping...",
            )
        else:
            photo_id = self._otcs.get_result_value(response=response, key="id")
            response = self._otcs.update_user_photo(user_id=1000, photo_id=photo_id)
            if response is None:
                self.logger.warning("Failed to add photo for user -> 'admin'!")
            else:
                self.logger.info("Successfully added photo for user -> 'admin'.")

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_photos_m365")
    def process_user_photos_m365(self, section_name: str = "userPhotosM365") -> bool:
        """Process user photos in payload and assign them to Microsoft 365 users.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not isinstance(self._m365, M365):
            self.logger.error(
                "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
                section_name,
            )
            return False

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # we assume the nickname of the photo item equals the login name of the user
        # we also assume that the photos have been uploaded / transported into the target system
        for user in self._users:
            user_name = user.get("name")
            if not user_name:
                self.logger.error("User is missing a login. Skipping to next user...")
                success = False
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            if "id" not in user:
                self.logger.error(
                    "User -> '%s' does not have an ID. The user creation may have failed before. Skipping...",
                    user_name,
                )
                success = False
                continue

            if "enable_o365" not in user or not user["enable_o365"]:
                self.logger.info(
                    "Microsoft 365 is not enabled in payload for user -> '%s'. Skipping...",
                    user_name,
                )
                continue

            # If the customizer pod is restarted it may be that
            # the M365 user exists even if the M365 user ID is
            # not yet written back into the payload. So we use the
            # determine_user_id_m365() method that handles both cases
            # and updates the payload if the user exists in M365 already.
            user_m365_id = self.determine_user_id_m365(user=user)
            if not user_m365_id:
                self.logger.error(
                    "M365 user -> '%s' does not exist. Skipping...",
                    user_name,
                )
                success = False
                continue

            if self._m365.get_user_photo(user_id=user_m365_id, show_error=False):
                self.logger.info(
                    "User -> '%s' (%s) has already a photo in Microsoft 365. Skipping...",
                    user_name,
                    user_m365_id,
                )
                continue
            self.logger.info(
                "User -> '%s' (%s) has not yet a photo in Microsoft 365. Uploading...",
                user_name,
                user_m365_id,
            )

            response = self._otcs.get_node_from_nickname(nickname=user_name)
            if response is None:
                self.logger.warning(
                    "Missing photo for user -> '%s' - nickname not found. Skipping...",
                    user_name,
                )
                continue
            photo_id = self._otcs.get_result_value(response=response, key="id")
            photo_name = self._otcs.get_result_value(response=response, key="name")

            photo_path = os.path.join(tempfile.gettempdir(), photo_name)
            result = self._otcs.download_document(
                node_id=photo_id,
                file_path=photo_path,
            )
            # download_document() delivers a boolean result:
            if result is False:
                self.logger.warning(
                    "Failed to download photo for user -> '%s' from Content Server!",
                    user_name,
                )
                success = False
                continue
            self.logger.info(
                "Successfully downloaded photo for user -> '%s' from Content Server to file -> '%s'.",
                user_name,
                photo_path,
            )

            # Upload photo to M365:
            response = self._m365.update_user_photo(user_m365_id, photo_path)
            if response is None:
                self.logger.error(
                    "Failed to upload photo for user -> '%s' to Microsoft 365!",
                    user_name,
                )
                success = False
            else:
                self.logger.info(
                    "Successfully uploaded photo for user -> '%s' to Microsoft 365.",
                    user_name,
                )
        # end for loop

        # Check if Admin has a photo as well (nickname needs to be "admin")
        # Then we want this to be applied in M365 as well:
        response = self._otcs.get_node_from_nickname(nickname="admin")
        if response is None:
            self.logger.warning(
                "Missing photo for admin - nickname not found. Skipping...",
            )
        else:
            photo_id = self._otcs.get_result_value(response=response, key="id")
            photo_name = self._otcs.get_result_value(response=response, key="name")
            photo_path = os.path.join(tempfile.gettempdir(), photo_name)
            result = self._otcs.download_document(
                node_id=photo_id,
                file_path=photo_path,
            )
            # download_document() delivers a boolean result:
            if result is False:
                self.logger.warning(
                    "Failed to download photo for user -> 'admin' from Content Server!",
                )
                success = False
            else:
                self.logger.info(
                    "Successfully downloaded photo for user -> 'admin' from Content Server to file -> '%s'.",
                    photo_path,
                )
                m365_admin_email = "admin@" + self._m365.config()["domain"]
                response = self._m365.update_user_photo(
                    user_id=m365_admin_email,
                    photo_path=photo_path,
                )
                if response is None:
                    self.logger.warning("Failed to add photo for Microsoft 365 user -> '%s'!", m365_admin_email)
                else:
                    self.logger.info(
                        "Successfully added photo for Microsoft 365 user -> '%s'.",
                        m365_admin_email,
                    )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_photos_salesforce")
    def process_user_photos_salesforce(
        self,
        section_name: str = "userPhotosSalesforce",
    ) -> bool:
        """Process user photos in payload and assign them to Salesforce users.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not isinstance(self._salesforce, Salesforce):
            self.logger.error(
                "Salesforce connection not setup properly. Skipping payload section -> '%s'...",
                section_name,
            )
            return False

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # we assume the nickname of the photo item equals the login name of the user
        # we also assume that the photos have been uploaded / transported into the target system
        for user in self._users:
            user_name = user.get("name")
            if not user_name:
                self.logger.error("User is missing a login. Skipping to next user...")
                success = False
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            # Check if the user is explicitly enabled for Salesforce:
            if not user.get("enable_salesforce", False):
                self.logger.info(
                    "User -> '%s' is not enabled for Salesforce. Skipping...",
                    user_name,
                )
                continue

            extra_attributes = user.get("extra_attributes", None)
            if not extra_attributes or len(extra_attributes) == 0:
                self.logger.info(
                    "User -> '%s' does not have the extra attributes for Salesforce. Skipping...",
                    user_name,
                )
                continue
            user_login = extra_attributes[0].get("value", "")
            if not user_login:
                self.logger.info(
                    "User -> '%s' does not have the extra attributes value for Salesforce. Skipping...",
                    user_name,
                )
                continue

            user_id = self._salesforce.get_user_id(username=user_login)
            if user_id is None:
                self.logger.error(
                    "Failed to get Salesforce user ID of user -> '%s'!",
                    user_login,
                )
                success = False
                continue

            response = self._otcs.get_node_from_nickname(nickname=user_name)
            if response is None:
                self.logger.warning(
                    "Missing photo for user -> '%s' - nickname not found. Skipping...",
                    user_name,
                )
                continue
            photo_id = self._otcs.get_result_value(response=response, key="id")
            photo_name = self._otcs.get_result_value(response=response, key="name")
            photo_path = os.path.join(tempfile.gettempdir(), photo_name)

            # Check if it is not yet downloaded:
            if not os.path.isfile(photo_path):
                # download the profile picture into the tmp directory:
                result = self._otcs.download_document(
                    node_id=photo_id,
                    file_path=photo_path,
                )
                # download_document() delivers a boolean result:
                if result is False:
                    self.logger.warning(
                        "Failed to download photo for user -> '%s' from Content Server to file -> '%s'!",
                        user_name,
                        photo_path,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Successfully downloaded photo for user -> '%s' from Content Server to file -> '%s'.",
                    user_name,
                    photo_path,
                )
            else:
                self.logger.info(
                    "Reusing downloaded photo -> '%s' for Salesforce user -> '%s' (%s).",
                    photo_path,
                    user_name,
                    user_id,
                )

            response = self._salesforce.update_user_photo(
                user_id=user_id,
                photo_path=photo_path,
            )
            if response:
                self.logger.info(
                    "Successfully updated profile photo of Salesforce user -> '%s' (%s).",
                    user_login,
                    user_id,
                )
            else:
                self.logger.error(
                    "Failed to update profile photo of Salesforce user -> '%s' (%s)! Skipping...",
                    user_login,
                    user_id,
                )
                success = False
                continue

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_photos_core_share")
    def process_user_photos_core_share(
        self,
        section_name: str = "userPhotosCoreShare",
    ) -> bool:
        """Process user photos in payload and assign them to Core Share users.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not isinstance(self._core_share, CoreShare):
            self.logger.error(
                "Core Share connection not setup properly. Skipping payload section -> '%s'...",
                section_name,
            )
            return False

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # we assume the nickname of the photo item equals the login name of the user
        # we also assume that the photos have been uploaded / transported into the target system
        for user in self._users:
            if "name" not in user:
                self.logger.error(
                    "User is missing login name. Skipping to next user...",
                )
                success = False
                continue
            user_login = user["name"]
            user_last_name = user.get("lastname", "")
            user_first_name = user.get("firstname", "")
            user_name = "{} {}".format(user_first_name, user_last_name).strip()

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_login,
                )
                continue

            # Check if the user is enabled for Core Share.
            # Core Share is disabled per default for the users.
            # There needs to be "enable_core_share" in user payload
            # and it needs to be True:
            if not user.get("enable_core_share", False):
                self.logger.info(
                    "User -> '%s' is not enabled for Core Share. Skipping...",
                    user_login,
                )
                continue

            core_share_user_id = self.determine_user_id_core_share(user=user)

            if core_share_user_id is None:
                self.logger.error(
                    "Failed to get ID of Core Share user -> '%s'!",
                    user_name,
                )
                success = False
                continue

            response = self._otcs.get_node_from_nickname(nickname=user_login)
            if response is None:
                self.logger.warning(
                    "Missing photo for user -> '%s' - nickname not found. Skipping...",
                    user_login,
                )
                continue
            photo_id = self._otcs.get_result_value(response=response, key="id")
            photo_name = self._otcs.get_result_value(response=response, key="name")
            photo_path = os.path.join(tempfile.gettempdir(), photo_name)

            # Check if it is not yet downloaded:
            if not os.path.isfile(photo_path):
                # download the profile picture into the tmp directory:
                result = self._otcs.download_document(
                    node_id=photo_id,
                    file_path=photo_path,
                )
                # download_document() delivers a boolean result:
                if result is False:
                    self.logger.warning(
                        "Failed to download photo for user -> '%s' from Content Server to file -> '%s'!",
                        user_name,
                        photo_path,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Successfully downloaded photo for user -> '%s' from Content Server to file -> '%s'.",
                    user_name,
                    photo_path,
                )
            else:
                self.logger.info(
                    "Reusing downloaded photo -> '%s' for Core Share user -> '%s' (%s).",
                    photo_path,
                    user_name,
                    core_share_user_id,
                )

            response = self._core_share.update_user_photo(
                user_id=core_share_user_id,
                photo_path=photo_path,
            )
            if response:
                self.logger.info(
                    "Successfully updated profile photo of Core Share user -> '%s' (%s).",
                    user_name,
                    core_share_user_id,
                )
            else:
                self.logger.error(
                    "Failed to update profile photo of Core Share user -> '%s' (%s)! Skipping...",
                    user_name,
                    core_share_user_id,
                )
                success = False
                continue

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="extract_properties_from_transport_packages")
    def extract_properties_from_transport_packages(
        self, business_object_type: dict, bo_type_name: str, bo_type_id: int
    ) -> bool:
        """Extract properties from transport packages for a given Business Object Type.

        Args:
            business_object_type (dict):
                The Business Object Type dictionary to be enriched with properties.
            bo_type_name (str):
                The name of the Business Object Type.
            bo_type_id (int):
                The ID of the Business Object Type.

        Returns:
            bool:
                True if properties were successfully extracted and added, False otherwise.

        """

        business_object_type["business_properties"] = []
        business_object_type["business_property_groups"] = []

        # Now we complete the data with what we have extracted from the transport packages
        # for Business Object Types. This is a workaround for the insufficient REST API
        # implementation (see otcs.get_business_object_type)
        if self._transport_extractions:
            self.logger.info(
                "Enrich business object type -> '%s' (%d) with extractions from transport packages (found '%s' extractions)...",
                bo_type_name,
                bo_type_id,
                str(len(self._transport_extractions)),
            )
        else:
            self.logger.info(
                "No transport extractions are recorded. This may be because of customizer restart.",
            )
            extraction_status_file = "transportPackagesExtractions"
            if self.check_status_file(payload_section_name=extraction_status_file):
                self.logger.info(
                    "Try to load extractions from success file -> '%s'...",
                    extraction_status_file,
                )
                self._transport_extractions = self.get_status_file(
                    payload_section_name=extraction_status_file,
                )

        for extraction in self._transport_extractions:
            xpath = extraction.get("data")
            data_list = extraction.get("data")
            if not data_list:
                self.logger.error(
                    "Extraction -> '%s' is missing the data element. Skipping...",
                    xpath,
                )
                return False
            if not isinstance(data_list, list):
                self.logger.warning(
                    "Extracted data for -> '%s' is not a list. Cannot process it. Skipping...",
                    xpath,
                )
                return False

            """
            The following loop processes a dictionary of this structure:

            llnode: {
                '@created': '2017-11-23T16:43:35',
                '@createdby': '1000',
                '@createdbyname': 'Terrarium Admin',
                '@description': '',
                '@id': '16013',
                '@modified': '2023-12-09T12:08:21',
                '@name': 'SFDC Order',
                '@objname': 'Business Object Type',
                '@objtype': '889',
                '@ownedby': '1000',
                '@ownedbyname': 'Terrarium Admin',
                '@parentguid': '95F96645-057D-4EAF-9083-BE9F24C0CB6C',
                '@parentid': '2898',
                '@parentname': 'Business Object Types',
                ...
                'Nickname': {'@domain': ''},
                'name': {'@xml:lang': 'en', '#text': 'SFDC Order'},
                'description': {'@xml:lang': 'en'},
                'businessObjectTypeInfo': {
                    'basicInfo': {
                        '@businessObjectId': '9',
                        '@businessobjectType': 'Order',
                        '@deleted': 'false',
                        '@name': 'SFDC Order',
                        '@subtype': '889',
                        '@useBusWorkspace': 'true',
                        'displayUrl': {...}
                    },
                    'businessApplication': {
                        'businessObjectTypeReference': {...}},
                        'businessAttachmentInfo': {
                            '@automaticAddingOfBusinessObject': 'false',
                            '@canbeAddedAsBusinessObject': 'false',
                            '@enableBADIBeforeAddingBO': 'false',
                            '@enableBADIBeforeRemovingBO': 'false',
                            '@enableMetadataMapping': 'false'
                        },
                        'managedObjectTypes': {
                            'managedObjectType': [...]
                        },
                        'multilingualNames': {'language': [...]},
                        'callbacks': {'callback': [...]},
                        'workspaceTypeReference': {'@isDefaultDisplay': 'false', '@isDefaultSearch': 'false', 'businessObjectTypeReference': {...}},
                        'businessPropertyMappings': {
                            'propertyMapping': [...]
                        },
                        'businessPropertyGroupMappings': {
                            'propertyGroupMapping': [...]
                        },
                        'documentTypes': {
                            'documentType': [...]
                        },
                        'CustomBOTypeInfo': None
                    }
            }
            """

            for data in data_list:
                #
                # Level 1: llnode
                #
                llnode = data.get("llnode")
                if not llnode:
                    self.logger.error(
                        "Missing llnode structure in data. Skipping...",
                    )
                    return False

                #
                # Level 2: businessobjectTypeInfo
                #
                business_object_type_info = llnode.get(
                    "businessobjectTypeInfo",
                    None,
                )
                if not business_object_type_info:
                    self.logger.error(
                        "Information is missing for business object type -> '%s'. Skipping...",
                        bo_type_name,
                    )
                    return False

                # Check if this extraction is for the current business object type:
                basic_info = business_object_type_info.get("basicInfo", None)
                if not basic_info:
                    self.logger.error(
                        "Cannot find basic info of business object type -> '%s'. Skipping...",
                        bo_type_name,
                    )
                    return False
                name = basic_info.get("@businessobjectType", "")
                if not name:
                    self.logger.error(
                        "Cannot find name of business object type -> '%s'. Skipping...",
                        bo_type_name,
                    )
                    return False
                obj_type = llnode.get("@objtype", None)
                # we need to compare bo_type and NOT bo_type_name here!
                # Otherwise we don't find the SAP and SuccessFactors data:
                if name != bo_type_name or obj_type != "889":
                    continue

                #
                # Level 3: businessPropertyMappings - plain, non-grouped properties
                #
                business_property_mappings = business_object_type_info.get(
                    "businessPropertyMappings",
                    None,
                )
                if not business_property_mappings:
                    self.logger.info(
                        "No property mapping for business object type -> '%s'. Skipping...",
                        bo_type_name,
                    )
                    continue
                property_mappings = business_property_mappings.get(
                    "propertyMapping",
                    [],
                )
                # This can happen if there's only 1 propertyMapping;
                if not isinstance(property_mappings, list):
                    self.logger.debug(
                        "Found a single property mapping in a dictionary (not in a list). Package it into a list...",
                    )
                    property_mappings = [property_mappings]

                for property_mapping in property_mappings:
                    property_name = property_mapping.get("@propertyName")
                    attribute_name = property_mapping.get("@attributeName")
                    category_id = property_mapping.get("@categoryId")
                    mapping_type = property_mapping.get("@type")
                    self.logger.debug(
                        "%s Property Mapping for Business Object -> '%s' property -> '%s' is mapped to attribute -> '%s' (category -> %s)",
                        mapping_type,
                        bo_type_name,
                        property_name,
                        attribute_name,
                        category_id,
                    )
                    business_object_type["business_properties"].append(
                        property_mapping,
                    )

                #
                # Level 3: businessPropertyGroupMappings - grouped properties
                #
                business_property_group_mappings = business_object_type_info.get(
                    "businessPropertyGroupMappings",
                    None,
                )
                if not business_property_group_mappings:
                    self.logger.info(
                        "No property group mapping for business object type -> '%s'. Skipping...",
                        bo_type_name,
                    )
                    continue

                property_group_mappings = business_property_group_mappings.get(
                    "propertyGroupMapping",
                    [],
                )
                # This can happen if there's only 1 propertyMapping;
                if isinstance(property_group_mappings, dict):
                    self.logger.debug(
                        "Found a single property group mapping in a dictionary (not in a list). Pack it into a list...",
                    )
                    property_group_mappings = [property_group_mappings]

                for property_group_mapping in property_group_mappings:
                    group_name = property_group_mapping.get("@groupName")
                    set_name = property_group_mapping.get("@setName")
                    category_id = property_group_mapping.get("@categoryId")
                    mapping_type = property_group_mapping.get("@type")
                    self.logger.debug(
                        "%s property group mapping for business object -> %s: group -> '%s' is mapped to set -> '%s' (category -> %s)",
                        mapping_type,
                        bo_type_name,
                        group_name,
                        set_name,
                        category_id,
                    )

                    property_mappings = property_group_mapping.get(
                        "propertyMapping",
                        [],
                    )
                    # This can happen if there's only 1 propertyMapping;
                    if not isinstance(property_mappings, list):
                        self.logger.debug(
                            "Found a single property mapping in a dictionary (not in a list). Package it into a list...",
                        )
                        property_mappings = [property_mappings]

                    for property_mapping in property_mappings:
                        # for nested mappings we only have 2 fields - the rest is on the group level - see above
                        property_name = property_mapping.get("@propertyName")
                        attribute_name = property_mapping.get("@attributeName")
                        self.logger.debug(
                            "%s Property Mapping inside group for Business Object -> '%s', group -> '%s', property -> '%s' is mapped to set -> %s, attribute -> '%s' (category -> %s)",
                            mapping_type,
                            bo_type_name,
                            group_name,
                            property_name,
                            set_name,
                            attribute_name,
                            category_id,
                        )
                        # we write the group / set information also in the property mapping
                        # tp have a plain list with all information:
                        property_mapping["@groupName"] = group_name
                        property_mapping["@setName"] = set_name
                        property_mapping["@type"] = mapping_type
                        business_object_type["business_property_groups"].append(
                            property_mapping,
                        )
                # end for property_group_mapping in property_group_mappings
            # end for data in data_list
        # end for extraction in self._transport_extractions

        return True

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_business_object_types")
    def process_business_object_types(
        self,
        section_name: str = "businessObjectTypes",
    ) -> list:
        """Create a data structure for all business object types in the Extended ECM system.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            list:
                A list of business object types. Each list element is a dict with these values:
                - id (str)
                - name (str)
                - type (str)
                - ext_system_id (str)
                - business_properties (list)
                - business_property_groups (list)

        """

        # If this payload section has been processed successfully before we
        # still need to read the data structure from the status file and
        # initialize self._business_object_types:
        if self.check_status_file(payload_section_name=section_name):
            # read the list from the json file in admin Home
            # this is important for restart of customizer pod
            # as this data structure is used later on for workspace processing
            self.logger.info(
                "Re-Initialize business object types list from status file -> '%s' for later use...",
                self.get_status_file_name(payload_section_name=section_name),
            )
            self._business_object_types = self.get_status_file(
                payload_section_name=section_name,
            )
            if self._business_object_types:
                self.logger.info(
                    "Found -> %s business object types.",
                    str(len(self._business_object_types)),
                )
                self.logger.debug(
                    "Business object types -> %s",
                    str(self._business_object_types),
                )
                return self._business_object_types
            else:
                self.logger.warning(
                    "Couldn't read business object types from status file -> '%s'. Regenerate list...",
                    self.get_status_file_name(payload_section_name=section_name),
                )

        success: bool = True

        # Read payload_section "businessObjectTypes" if available.
        # For some (external) payload we may want to skip this time-
        # consuming processing as there are not BO specific payload
        # elements:
        for section in self._payload_sections:
            # Check if the section does exist and it is explicitly disabled
            # (enabled = False). Then we can skip further processing:
            if section.get("name", "") == "businessObjectTypes" and not section.get(
                "enabled",
                True,
            ):
                self.logger.info(
                    "Skip business object type processing as its section is disabled in payload.",
                )
                return self._business_object_types

        # get all business object types (these have been created by the transports and are not in the payload!)
        # we need to do this each time as it needs to work across multiple payload files...
        response = self._otcs.get_business_object_types()
        if response is None:
            self.logger.info("No business object types found!")
            self._business_object_types = []
        else:
            # Store result list in self._business_object_types for later use:
            self._business_object_types = response["results"]
            self.logger.info(
                "Found -> %s business object types.",
                str(len(self._business_object_types)),
            )
            self.logger.debug(
                "Business object types -> %s",
                str(self._business_object_types),
            )

        otcs_version = float(self._otcs.get_server_version())
        if otcs_version >= 25.3:
            self.logger.info(
                "Using new REST API of OTCS %s to retrieve business object type information...",
                str(otcs_version),
            )
        else:
            self.logger.info(
                "Have to use transport extraction with OTCS %s to retrieve business object type information...",
                str(otcs_version),
            )

        # now we enrich the business_object_type list elments (which are dicts)
        # with additional dict elements for further processing:
        for business_object_type in self._business_object_types:
            # Flatten the response structure for more easy retrieval:
            # Get BO Type (e.g. KNA1):
            bo_type = business_object_type["data"]["properties"]["bo_type"]
            self.logger.debug("Business object type -> %s", bo_type)
            business_object_type["type"] = bo_type
            # Get BO Type ID:
            bo_type_id = business_object_type["data"]["properties"]["bo_type_id"]
            self.logger.debug("Business object type ID -> %s", bo_type_id)
            business_object_type["id"] = bo_type_id
            # Get BO Type Name:
            bo_type_name = business_object_type["data"]["properties"]["bo_type_name"]
            self.logger.debug("Business object type name -> %s", bo_type_name)
            business_object_type["name"] = bo_type_name
            # Get External System ID:
            ext_system_id = business_object_type["data"]["properties"]["ext_system_id"]
            self.logger.debug("External system ID -> %s", ext_system_id)
            business_object_type["ext_system_id"] = ext_system_id
            # Get External System ID:
            workspace_type_id = business_object_type["data"]["properties"]["workspace_type_id"]
            self.logger.debug("Workspace type ID -> %s", workspace_type_id)
            business_object_type["workspace_type_id"] = workspace_type_id

            if not bo_type_name or not bo_type_id:
                self.logger.info(
                    "Business object type %sis void (dummy data for workspace types without a business object connection)! Skipping...",
                    "for workspace type ID -> {} ".format(workspace_type_id) if workspace_type_id else "",
                )
                continue
            self.logger.info(
                "Processing business object type -> '%s' (%s) with ID -> %s for workspace type with ID -> %d...",
                bo_type_name,
                bo_type,
                bo_type_id,
                workspace_type_id,
            )

            # Get additional information per BO Type (before 25.3 REST API is severly
            # limited) - it does not return Property names from External System
            # and is also missing Business Property Groups. Thus, for older versions
            # we extract that information from the Transport XML files (see else case):
            if otcs_version >= 25.3:
                response = self._otcs.get_business_object_type(type_id=bo_type_id)
                if response is None or not response["results"]:
                    self.logger.warning(
                        "Cannot retrieve additional information for business object type -> %s (%s). Skipping...",
                        bo_type,
                        bo_type_id,
                    )
                    success = False
                    continue

                business_properties = response["results"]["GeneralTab"]["data"]["properties"]["fWkspCreationConfig"][
                    "propertyMappings"
                ]
                business_object_type["business_properties"] = business_properties

                business_property_groups = response["results"]["GeneralTab"]["data"]["properties"][
                    "fWkspCreationConfig"
                ]["propertyGroups"]
                business_object_type["business_properties_groups"] = business_property_groups
            elif not self.extract_properties_from_transport_packages(business_object_type, bo_type_id, bo_type_name):
                success = False
        # end for business_object_type in self._business_object_types

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._business_object_types,
        )

        return self._business_object_types

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_business_object_properties")
    def get_business_object_properties(self, bo_type_name: str) -> dict | None:
        """Get a dictionary with all property mapping of a business object type.

        We contruct this dictionary from the two lists for the given
        business object types (property mapping and property group mappings)
        These two lists have been created before by process_business_object_types()

        This method is used for creation of business objects in Salesforce.

        Args:
            bo_type_name (str):
                The name of the business object type

        Returns:
            dict | None:
                A dictionary with keys that are either the attribute name or
                a key that is contructed like this: set name + "-" + attribute name.
                This allows for an easy lookup in methods that have access to
                the category data of business workspaces.

        """

        if not self._business_object_types:
            self.logger.warning(
                "List of business object types is empty / not initialized! Cannot lookup type -> '%s'!",
                bo_type_name,
            )
            return None

        # Find the matching business object type:
        business_object_type = next(
            (item for item in self._business_object_types if item["name"] == bo_type_name),
            None,
        )
        if not business_object_type:
            self.logger.warning(
                "Cannot find business object type -> '%s'!",
                bo_type_name,
            )
            return None

        business_properties = business_object_type.get("business_properties")
        business_property_groups = business_object_type.get("business_property_groups")

        lookup_dict = {}

        # 25.3 uses a new REST API to retreive the business object type details.
        # Pre 25.3 we use extractions from the transport package to get this information
        # See process_business_object_type() method.
        otcs_version = float(self._otcs.get_server_version())
        if otcs_version < 25.3:
            att_key = "@attributeName"
            set_key = "@setName"
        else:
            att_key = "att_name"
            set_key = "set_name"

        for mapping in business_properties:
            attribute_name = mapping.get(att_key)
            lookup_dict[attribute_name] = mapping

        for mapping in business_property_groups:
            set_name = mapping.get(set_key)
            attribute_name = mapping.get(att_key)
            lookup_dict[set_name + "-" + attribute_name] = mapping

        return lookup_dict

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_types")
    def process_workspace_types(self, section_name: str = "workspaceTypes") -> list:
        """Create a data structure for all workspace types in the OTCS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            list:
                A list of workspace types. Each list element is a dict with these values:
                - id (str)
                - name (str)
                - templates (list)
                    + name (str)
                    + id

        """

        # If this payload section has been processed successfully before we
        # still need to read the data structure from the status file and
        # initialize self._workspace_types:
        if self.check_status_file(payload_section_name=section_name):
            # read the list from the json file in admin Home
            # this is important for restart of customizer pod
            # as this data structure is used later on for workspace processing
            self.logger.info(
                "Re-Initialize workspace types list from status file -> '%s' for later use...",
                self.get_status_file_name(payload_section_name=section_name),
            )
            self._workspace_types = self.get_status_file(
                payload_section_name=section_name,
            )
            if self._workspace_types:
                self.logger.info(
                    "Found -> %s workspace types.",
                    str(len(self._workspace_types)),
                )
                self.logger.debug("Workspace types -> %s", str(self._workspace_types))
                return self._workspace_types
            else:
                self.logger.error(
                    "Couldn't read workspace types from status file -> '%s'. Regenerate list...",
                    self.get_status_file_name(payload_section_name=section_name),
                )

        # Read payload_section "workspaceTypes" if available
        payload_section = {}
        for section in self._payload_sections:
            if section["name"] == "workspaceTypes":
                payload_section = section

        # get all workspace types (these have been created by the transports and are not in the payload!)
        # we need to do this each time as it needs to work across multiple payload files...
        response = self._otcs.get_workspace_types()
        if response is None:
            self.logger.error("No workspace types found!")
            self._workspace_types = []
        else:
            self._workspace_types = response["results"]
            self.logger.info(
                "Found -> %s workspace types.",
                str(len(self._workspace_types)),
            )
            self.logger.debug("Workspace types -> %s", str(self._workspace_types))

        # now we enrich the workspace_type list elments (which are dicts)
        # with additional dict elements for further processing:
        for workspace_type in self._workspace_types:
            workspace_type_id = workspace_type["data"]["properties"]["wksp_type_id"]
            self.logger.debug("Workspace Type ID -> %s", workspace_type_id)
            workspace_type["id"] = workspace_type_id
            workspace_type_name = workspace_type["data"]["properties"]["wksp_type_name"]
            workspace_type["name"] = workspace_type_name
            workspace_templates = workspace_type["data"]["properties"]["templates"]
            if not workspace_templates:
                self.logger.warning(
                    "Workspace type -> '%s' has no workspace templates to process! Skipping...",
                    workspace_type_name,
                )
                continue
            self.logger.info(
                "Workspace type -> '%s' has %d template%s to process...",
                workspace_type_name,
                len(workspace_templates),
                "s" if len(workspace_templates) > 1 else "",
            )

            # Create empty lists of dicts with template names and node IDs:
            workspace_type["templates"] = []
            # Determine available templates per workspace type (there can be multiple!)
            # and record them in a simplified data structure:
            for workspace_template in workspace_templates:
                workspace_template_id = workspace_template["id"]
                workspace_template_name = workspace_template["name"]
                self.logger.info(
                    "Found workspace template -> '%s' (%s).",
                    workspace_template_name,
                    workspace_template_id,
                )
                template = {
                    "name": workspace_template_name,
                    "id": workspace_template_id,
                }
                workspace_type["templates"].append(template)

                if payload_section.get("inherit_permissions", False):
                    # TODO: Workaround for problem with workspace role inheritance
                    # which may be related to Transport or REST API: to work-around this we
                    # push down the workspace roles to the workspace folders explicitly:
                    response = self._otcs.get_workspace_roles(
                        workspace_id=workspace_template_id,
                    )
                    for roles in response["results"]:
                        role_name = roles["data"]["properties"]["name"]
                        role_id = roles["data"]["properties"]["id"]
                        permissions = roles["data"]["properties"]["perms"]
                        # as get_workspace_roles() delivers permissions as a value (bit encoded)
                        # we need to convert it to a permissions string list:
                        permission_string_list = self._otcs.convert_permission_value_to_permission_string(
                            permission_value=permissions,
                        )

                        self.logger.info(
                            "Inherit permissions of workspace template -> '%s' (%s) and role -> '%s' (%s) to workspace folders...",
                            workspace_template_name,
                            workspace_template_id,
                            role_name,
                            role_id,
                        )

                        # Inherit permissions to folders of workspace template:
                        response = self._otcs.assign_workspace_permissions(
                            workspace_id=workspace_template_id,
                            role_id=role_id,
                            permissions=permission_string_list,
                            apply_to=1,  # 1 = only sub items - workspace node itself is OK
                        )
                    # end for roles in response["results"]:
                # end if payload_section.get("inherit_permissions", False):
            # end for workspace_template in workspace_templates:
        # end for workspace_type in self._workspace_types:

        self.write_status_file(success=True, payload_section_name=section_name, payload_section=self._workspace_types)

        return self._workspace_types

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_templates")
    def process_workspace_templates(
        self,
        section_name: str = "workspaceTemplates",
    ) -> bool:
        """Process workspace template playload.

        This allows to define role members on template basis.
        This avoids having to "pollute" workspace templates
        with user or group information and instead controls this via payload.

        This method also allows to assign (additional) categories to
        a workspace template controlled by payload.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for workspace_template in self._workspace_templates:
            # Read Workspace Type Name from payload:
            type_name = workspace_template.get("type_name")
            if not type_name:
                self.logger.error(
                    "Workspace template needs a type name! Skipping to next workspace template...",
                )
                success = False
                continue

            # Check if workspace template has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not workspace_template.get("enabled", True):
                self.logger.info(
                    "Payload for workspace template -> '%s' is disabled. Skipping to next workspace template...",
                    type_name,
                )
                continue

            # Read Workspace Template Name from payload:
            template_name = workspace_template.get("template_name")
            if not template_name:
                self.logger.error(
                    "Workspace template for workspace type -> '%s' needs a template name! Skipping...",
                    type_name,
                )
                success = False
                continue

            # Read members from payload:
            members = workspace_template.get("members", [])

            # Read categories from payload:
            categories = workspace_template.get("categories", [])

            # Find the workspace type with the name given in the _workspace_types
            # datastructure that has been generated by process_workspace_types() method before:
            workspace_type = next(
                (item for item in self._workspace_types if item["name"] == type_name),
                None,
            )
            if workspace_type is None:
                self.logger.error(
                    "Workspace type -> '%s' not found. Skipping...",
                    type_name,
                )
                success = False
                continue
            if workspace_type["templates"] == []:
                self.logger.error(
                    "Workspace type -> '%s' does not have templates. Skipping...",
                    type_name,
                )
                success = False
                continue

            workspace_template = next(
                (item for item in workspace_type["templates"] if item["name"] == template_name),
                None,
            )
            if workspace_template:  # does this template exist?
                self.logger.info(
                    "Workspace template -> '%s' has been specified in payload and it does exist.",
                    template_name,
                )
            else:
                self.logger.error(
                    "Workspace template -> '%s' has been specified in payload but it doesn't exist!",
                    template_name,
                )
                self.logger.error(
                    "Workspace type -> '%s' has only these templates -> %s",
                    type_name,
                    workspace_type["templates"],
                )
                success = False
                continue

            template_id = workspace_template["id"]

            workspace_roles = self._otcs.get_workspace_roles(workspace_id=template_id)
            if workspace_roles is None:
                self.logger.info(
                    "Workspace template -> '%s' with node Id -> %s has no roles. Skipping to next workspace...",
                    template_name,
                    template_id,
                )
                continue

            for member in members:
                # read user list, group list, and role name from payload:
                member_users = member.get("users", [])
                member_groups = member.get("groups", [])
                member_role_name = member.get("role", "")

                if not member_role_name:  # role name is required
                    self.logger.error(
                        "Members of workspace template -> '%s' is missing the role name.",
                        template_name,
                    )
                    success = False
                    continue
                if (
                    member_users == [] and member_groups == []
                ):  # we either need users or groups (or both) in the payload
                    self.logger.debug(
                        "Payload for workspace template -> '%s' and role -> '%s' does not have any members (no users nor groups).",
                        template_name,
                        member_role_name,
                    )
                    continue

                role_id = self._otcs.lookup_result_value(
                    response=workspace_roles,
                    key="name",
                    value=member_role_name,
                    return_key="id",
                )
                if role_id is None:
                    #    if member_role is None:
                    self.logger.error(
                        "Workspace template -> '%s' does not have a role -> '%s'",
                        template_name,
                        member_role_name,
                    )
                    success = False
                    continue

                # Process users as workspace template members:
                for member_user in member_users:
                    # find member user in current payload:
                    member_user_id = next(
                        (item for item in self._users if item["name"] == member_user),
                        {"name": member_user},  # we construct a payload on the fly to make determine_user_id() work
                    )
                    user_id = self.determine_user_id(user=member_user_id) if member_user_id else None
                    if not user_id:
                        self.logger.error(
                            "Cannot find member user with login -> '%s'. Skipping...",
                            member_user,
                        )
                        success = False
                        continue

                    # Add member if it does not yet exists - suppress warning
                    # message if user is already in role:
                    response = self._otcs.add_workspace_member(
                        workspace_id=template_id,
                        role_id=int(role_id),
                        member_id=user_id,
                        show_warning=False,
                    )
                    if response is None:
                        self.logger.error(
                            "Failed to add user -> '%s' (%s) as member to role -> '%s' (%s) of workspace template -> '%s' (%s)!",
                            member_user,
                            user_id,
                            member_role_name,
                            role_id,
                            template_name,
                            template_id,
                        )
                        success = False
                    else:
                        self.logger.info(
                            "Successfully added user -> '%s' (%s) as member to role -> '%s' (%s) of workspace template -> '%s' (%s).",
                            member_user,
                            user_id,
                            member_role_name,
                            role_id,
                            template_name,
                            template_id,
                        )
                # end for member_user in member_users:

                # Process groups as workspace template members:
                for member_group in member_groups:
                    member_group_id = next(
                        (item for item in self._groups if item["name"] == member_group),
                        None,
                    )

                    group_id = self.determine_group_id(group=member_group_id) if member_group_id else None
                    if not group_id:
                        self.logger.error(
                            "Cannot find member group -> '%s'. Skipping...",
                            member_group,
                        )
                        success = False
                        continue

                    response = self._otcs.add_workspace_member(
                        workspace_id=template_id,
                        role_id=int(role_id),
                        member_id=group_id,
                        show_warning=False,
                    )
                    if response is None:
                        self.logger.error(
                            "Failed to add group -> '%s' (%s) as member to role -> '%s' (%s) of workspace template -> '%s' (%s)!",
                            member_group_id["name"],
                            group_id,
                            member_role_name,
                            role_id,
                            template_name,
                            template_id,
                        )
                        success = False
                    else:
                        self.logger.info(
                            "Successfully added group -> '%s' (%s) as member to role -> '%s' (%s) of workspace template -> '%s' (%s).",
                            member_group_id["name"],
                            group_id,
                            member_role_name,
                            role_id,
                            template_name,
                            template_id,
                        )
                # end for member_group in member_groups:
            # end for member in members:

            existing_template_categories = None
            for category in categories:
                category_path = category.get("path")
                category_nickname = category.get("nickname")
                inheritance = category.get("inheritance")
                apply_to_sub_items = category.get("apply_to_sub_items")
                category_id = None

                if not category_nickname and not category_path:
                    self.logger.error(
                        "Category assignment to workspace template needs eith the category nickname or the category path in the category volume!",
                    )
                    success = False
                    continue
                if category_path:
                    response = self._otcs.get_node_by_volume_and_path(
                        volume_type=self._otcs.VOLUME_TYPE_CATEGORIES_VOLUME,
                        path=category_path,
                    )
                    category_id = self._otcs.get_result_value(response=response, key="id")
                elif category_nickname:
                    response = self._otcs.get_node_from_nickname(nickname=category_nickname, show_error=False)
                    category_id = self._otcs.get_result_value(response=response, key="id")
                if not category_id:
                    self.logger.error(
                        "Cannot find category for workspace template -> '%s' (%s). Tried category %s.",
                        template_name,
                        template_id,
                        "path {}".format(category_path) if category_path else "nickname {}".format(category_nickname),
                    )
                    success = False
                    continue
                if existing_template_categories is None:
                    existing_template_categories = self._otcs.get_node_category_ids(node_id=template_id)
                if category_id not in existing_template_categories:
                    result = self._otcs.assign_category(
                        node_id=template_id,
                        category_id=category_id,
                        inheritance=inheritance,
                        apply_to_sub_items=apply_to_sub_items,
                    )
                    if not result:
                        self.logger.error(
                            "Cannot assign category with ID -> %s to workspace template -> '%s' (%s)!",
                            category_id,
                            template_name,
                            template_id,
                        )
                        success = False
                        continue
                    self.logger.info(
                        "Successfully assigned category with ID -> %s to workspace template -> '%s' (%s).",
                        category_id,
                        template_name,
                        template_id,
                    )
                    # Write ID back into the payload dictionary:
                    category["id"] = category_id
                else:
                    self.logger.info(
                        "Category with ID -> %s is already assigned to workspace template -> '%s' (%s).",
                        category_id,
                        template_name,
                        template_id,
                    )

            # end for category in categories:
        # end for workspace_template in self._workspace_templates:

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._workspace_templates,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="resolve_attribute_values")
    def resolve_attribute_values(
        self,
        attribute_name: str,
        attribute_type: str,
        attribute_values: str | list[str],
    ) -> str | list[str]:
        """Resolve user logins or group names to user / group IDs.

        This method handles the "special" cases where the actual
        attribute values need to be IDs but the payload has names.

        This method is called for all attributes. If no special handling
        is required it just return an unmodified attribute value.

        Args:
            attribute_name (str):
                The name of the attribute.
            attribute_type (str):
                The type of the attribute.
            attribute_values (str | list[str]):
                The value of the attribute. A single value or a list of values.

        Returns:
            str | list[str]:
                A resolved attribute value or a list of resolved attribute values.

        """

        # Special Case 1: handle special case where attribute type is a user picker.
        # we expect that the payload includes the login name for this
        # (as user IDs are not stable across systems) but then we need
        # to lookup the real user ID here:
        if attribute_type in ("otcs_user_picker", "otcs_member_picker"):
            self.logger.debug(
                "Attribute -> '%s' is is of type -> 'User Picker' (%s). Looking up user ID for user login name -> %s",
                attribute_name,
                attribute_type,
                attribute_values,
            )

            # Do we have a single value (not a list)?
            if not isinstance(attribute_values, list):
                user = self._otcs_frontend.get_user(name=attribute_values)
                user_id = self._otcs_frontend.lookup_result_value(
                    response=user,
                    key="name",
                    value=attribute_values,
                    return_key="id",
                )
                if user_id:
                    # User has been found - determine ID:
                    self.logger.debug(
                        "User -> '%s' has ID -> %s",
                        attribute_values,
                        user_id,
                    )
                    # Put the user ID into data structure
                    return str(user_id)
                else:
                    self.logger.error(
                        "User with login name -> '%s' does not exist!",
                        attribute_values,
                    )
                    # Clear the value to avoid workspace create failure
                    return ""
            # Multi-value user attribute:
            else:
                user_ids = []
                for value in attribute_values:
                    user = self._otcs_frontend.get_user(name=value)
                    user_id = self._otcs_frontend.lookup_result_value(
                        response=user,
                        key="name",
                        value=value,
                        return_key="id",
                    )
                    if user_id:
                        # User has been found - determine ID:
                        self.logger.debug(
                            "User -> '%s' has ID -> %s",
                            value,
                            user_id,
                        )
                        # Put the user ID into the result list:
                        user_ids.append(str(user_id))
                    else:
                        self.logger.error(
                            "User with login name -> '%s' does not exist!",
                            value,
                        )
                return user_ids

        # Special Case 2: handle Extended ECM for Government attribute type "Organizational Unit" (OU).
        # This is referring to a group ID which is not stable across deployments. So we need to lookup
        # the Group ID and add it to the data structure. This expects that the payload has the
        # group name and not the group ID:
        elif attribute_type == str(11480):
            self.logger.debug(
                "Attribute -> '%s' is is of type -> 'Organizational Unit' (%s). Looking up group ID for group name -> %s",
                attribute_name,
                attribute_type,
                attribute_values,
            )
            # Do we have a single value (not a list)?
            if not isinstance(attribute_values, list):
                group = self._otcs_frontend.get_group(
                    name=attribute_values,
                )
                group_id = self._otcs_frontend.lookup_result_value(
                    response=group,
                    key="name",
                    value=attribute_values,
                    return_key="id",
                )

                if group_id:
                    self.logger.debug(
                        "Group for Organizational Unit -> '%s' has ID -> %s",
                        attribute_values,
                        group_id,
                    )
                    # Put the group ID as a string:
                    return str(group_id)
                else:
                    self.logger.error(
                        "Group for Organizational Unit -> '%s' does not exist!",
                        attribute_values,
                    )
                    # Return an empty value string:
                    return ""
            # Multi-value org group attribute:
            else:
                group_ids = []
                for value in attribute_values:
                    group = self._otcs_frontend.get_group(
                        name=value,
                    )
                    group_id = self._otcs_frontend.lookup_result_value(
                        response=group,
                        key="name",
                        value=value,
                        return_key="id",
                    )

                    if group_id:
                        self.logger.debug(
                            "Group for Organizational Unit -> '%s' has ID -> %s",
                            value,
                            group_id,
                        )
                        # Put the group ID into the result list:
                        group_ids.append(str(group_id))
                    else:
                        self.logger.error(
                            "Group for Organizational Unit -> '%s' does not exist!",
                            value,
                        )
                return group_ids

        # This is the default case - we return the unchanged attribute values:
        return attribute_values

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="prepare_workspace_create_form")
    def prepare_workspace_create_form(
        self,
        categories: list,
        template_id: int,
        ext_system_id: str | None = None,
        bo_type: str | None = None,
        bo_id: str | None = None,
        parent_workspace_node_id: int | None = None,
    ) -> dict | None:
        """Prepare the category structure for the workspace creation.

        Args:
            categories (list):
                The categories list from the workspace payload.
            template_id (int):
                The workspace template ID.
            ext_system_id (str, optional):
                The optional external system ID.
            bo_type (str, optional):
                The optional business object type ID.
            bo_id (str, optional):
                the business object ID.
            parent_workspace_node_id (int, optional):
                The optional parent Workspace ID.

        Returns:
            dict | None:
                Category structure for workspace creation or None
                in case of an error.

        """

        category_create_data = {}

        response = self._otcs_frontend.get_workspace_create_form(
            template_id=template_id,
            external_system_id=ext_system_id,
            bo_type=bo_type,
            bo_id=bo_id,
            parent_id=parent_workspace_node_id,
        )
        if response is None or "forms" not in response:
            self.logger.error(
                "Failed to retrieve create information for template ID -> %s!",
                template_id,
            )
            return None

        self.logger.debug(
            "Successfully retrieved create information for template ID -> %d!",
            template_id,
        )

        # Process category information
        forms = response["forms"]

        categories_form = {}

        # Typically the the create workspace form delivers 4-5 forms:
        # 1. Form for System Attributes (has no role name)
        # 2. Form for Category Data (role name = "categories")
        # 3. Form for Classifications (role name = "classifications")
        # 4. Form for importing team members from another existing team
        # 5. Form for Microsoft Teams integration (role name = "microsoftteams")
        for form in forms:
            if "role_name" not in form:
                self.logger.debug("Found 'system attributes' form -> %s", str(form))
                continue
            match form["role_name"]:
                case "categories":
                    categories_form = form
                    self.logger.debug("Found 'categories' form -> %s", str(form))
                    continue
                case "classifications":
                    self.logger.debug("Found 'classifications' form -> %s", str(form))
                    continue
                case "rmclassifications":
                    self.logger.debug("Found 'RM classifications' form -> %s", str(form))
                    continue
                case "importteam":
                    self.logger.debug("Found 'import team participants' form -> %s", str(form))
                    continue
                case "microsoftteams":
                    self.logger.debug("Found 'microsoft teams' form -> %s", str(form))
                    continue
                case _:
                    # the remaining option is that this form is the system attributes form:
                    self.logger.warning("Unknown form -> %s", str(form))
            # end match form["role_name"]:
        # end for form in forms:

        # We are just interested in the single category data set (role_name = "categories"):
        data = categories_form.get("data", None)

        if not data:
            self.logger.debug("No categories data found.")
            return category_create_data

        self.logger.debug("Categories data found -> %s", data)
        schema = categories_form["schema"]["properties"]
        self.logger.debug("Categories schema found -> %s", schema)
        # Loop over categories:
        for cat_id in data:  # these are the category IDs (dict keys):
            self.logger.debug("Category ID -> %s", cat_id)
            data_attributes = data[cat_id]
            self.logger.debug("Data attributes -> %s", data_attributes)
            schema_attributes = schema[cat_id]["properties"]
            self.logger.debug("Schema attributes -> %s", schema_attributes)
            cat_name = schema[cat_id]["title"]
            self.logger.debug("Category name -> %s", cat_name)
            # Loop over attributes:
            # * Sets with one (fixed) row have type = object
            # * Multi-value Sets with (multiple) rows have type = array and "properties" in "items" schema
            # * Multi-value attributes have also type = array but NO "properties" in "items" schema
            for attr_id in data_attributes:  # these a attribute IDs (dict keys)
                self.logger.debug("Attribute ID -> %s", attr_id)
                self.logger.debug("Attribute data -> %s", data_attributes[attr_id])
                self.logger.debug(
                    "Attribute schema -> %s",
                    str(schema_attributes[attr_id]),
                )
                attr_type = schema_attributes[attr_id]["type"]
                self.logger.debug("Attribute type -> %s", attr_type)
                if "title" not in schema_attributes[attr_id]:
                    self.logger.debug("Attribute has no title in schema. Skipping...")
                    continue

                #
                # Handle multi-line set:
                #
                if attr_type == "array" and ("properties" in schema_attributes[attr_id]["items"]):
                    set_name = schema_attributes[attr_id]["title"]
                    self.logger.debug("Multi-line set -> '%s'", set_name)
                    set_data_attributes = data_attributes[attr_id]  # this is a list []
                    if set_data_attributes is None:
                        self.logger.debug("Attribute has no value in data. Skipping...")
                        continue
                    self.logger.debug("Set data attributes -> %s", set_data_attributes)
                    set_schema_attributes = schema_attributes[attr_id]["items"]["properties"]
                    self.logger.debug(
                        "Set schema attributes -> %s",
                        set_schema_attributes,
                    )
                    set_schema_max_rows = schema_attributes[attr_id]["items"]["maxItems"]
                    self.logger.debug("Set schema max rows -> %s", set_schema_max_rows)
                    set_data_max_rows = len(set_data_attributes)
                    self.logger.debug("Set data max rows -> %s", set_data_max_rows)
                    row = 1
                    # it can happen that the payload contains more rows than the
                    # initial rows in the set data structure. In this case we use
                    # a copy of the data structure from row 0 as template...
                    first_row = dict(set_data_attributes[0])
                    # We don't know upfront how many rows of data we will find in payload
                    # but we at max process the maxItems specified in the schema:
                    while row <= set_schema_max_rows:
                        # Test if we have any payload for this row:
                        attribute = next(
                            (
                                item
                                for item in categories
                                if (
                                    item["name"] == cat_name
                                    and "set" in item  # not all items may have a "set" key
                                    and item["set"] == set_name
                                    and "row" in item  # not all items may have a "row" key
                                    and item["row"] == row
                                )
                            ),
                            None,
                        )
                        # stop if there's no payload for the row:
                        if attribute is None:
                            self.logger.debug(
                                "No payload found for set -> %s, row -> %s",
                                set_name,
                                row,
                            )
                            # we assume that if there's no payload for row n there will be no payload for rows > n
                            # and break the while loop:
                            break
                        # do we need to create a new row in the data frame?
                        if row > set_data_max_rows:
                            # use the row we stored above to create a new empty row:
                            self.logger.debug(
                                "Found payload for row -> %s, we need a new data row for it",
                                row,
                            )
                            self.logger.debug(
                                "Adding an additional row -> %s to set data -> '%s'",
                                row,
                                set_name,
                            )
                            # add the empty dict to the list:
                            set_data_attributes.append(dict(first_row))
                            set_data_max_rows += 1
                        else:
                            self.logger.debug(
                                "Found payload for row -> %s %s we can store in existing data row",
                                row,
                                set_name,
                            )
                        # traverse all attributes in a single set row:
                        for set_attr_id in set_schema_attributes:
                            self.logger.debug(
                                "Set attribute ID -> %s (row -> %s)",
                                set_attr_id,
                                row,
                            )
                            self.logger.debug(
                                "Set attribute schema -> %s (row -> %s)",
                                set_schema_attributes[set_attr_id],
                                row,
                            )
                            set_attr_type = set_schema_attributes[set_attr_id]["type"]
                            self.logger.debug(
                                "Set attribute type -> %s (row -> %s)",
                                set_attr_type,
                                row,
                            )
                            set_attr_name = set_schema_attributes[set_attr_id]["title"]
                            self.logger.debug(
                                "Set attribute name -> %s (row -> %s)",
                                set_attr_name,
                                row,
                            )
                            # Lookup the attribute with the right category, set, attribute name, and row number in payload:
                            attribute = next(
                                (
                                    item
                                    for item in categories
                                    if (
                                        item["name"] == cat_name
                                        and "set" in item  # not all items may have a "set" key
                                        and item["set"] == set_name
                                        and item["attribute"] == set_attr_name
                                        and "row" in item  # not all items may have a "row" key
                                        and item["row"] == row
                                    )
                                ),
                                None,
                            )
                            if attribute is None:
                                self.logger.debug(
                                    "Set -> '%s', attribute -> '%s', row -> %s not found in payload.",
                                    set_name,
                                    set_attr_name,
                                    row,
                                )

                                # need to use row - 1 as index starts with 0 but payload rows start with 1
                                set_data_attributes[row - 1][set_attr_id] = ""
                            else:
                                if set_attr_type == "array" and "items" in set_schema_attributes[set_attr_id]:
                                    set_attr_type = set_schema_attributes[set_attr_id]["items"].get(
                                        "type",
                                        set_attr_type,
                                    )

                                self.logger.debug(
                                    "Set -> '%s', attribute -> '%s', row -> %s found in payload, value -> '%s'",
                                    set_name,
                                    set_attr_name,
                                    row,
                                    attribute["value"],
                                )
                                set_data_attributes[row - 1][set_attr_id] = self.resolve_attribute_values(
                                    attribute_name=set_attr_name,
                                    attribute_type=set_attr_type,
                                    attribute_values=attribute["value"],
                                )
                        row += 1  # continue the while loop with the next row
                    # end while row <= set_schema_max_rows:
                # end if attr_type == "array" and ("properties" in schema_attributes[attr_id]["items"]):

                #
                # Handle single-line set:
                #
                elif attr_type == "object":
                    set_name = schema_attributes[attr_id]["title"]
                    self.logger.debug("Single-line set -> '%s'", set_name)
                    set_data_attributes = data_attributes[attr_id]
                    self.logger.debug("Set data attributes -> %s", set_data_attributes)

                    set_schema_attributes = schema_attributes[attr_id]["properties"]
                    self.logger.debug(
                        "Set schema attributes -> %s",
                        str(set_schema_attributes),
                    )
                    # Loop over set attributes:
                    for set_attr_id in set_data_attributes:
                        self.logger.debug("Set attribute ID -> %s", set_attr_id)
                        self.logger.debug(
                            "Set attribute data -> %s",
                            str(set_data_attributes[set_attr_id]),
                        )
                        self.logger.debug(
                            "Set attribute schema -> %s",
                            str(set_schema_attributes[set_attr_id]),
                        )
                        set_attr_type = set_schema_attributes[set_attr_id]["type"]
                        self.logger.debug("Set attribute type -> %s", set_attr_type)
                        set_attr_name = set_schema_attributes[set_attr_id]["title"]
                        self.logger.debug("Set attribute name -> '%s'", set_attr_name)
                        # Lookup the attribute with the right category, set and attribute name in payload:
                        attribute = next(
                            (
                                item
                                for item in categories
                                if (
                                    item["name"] == cat_name
                                    and "set" in item  # not all items may have a "set" key
                                    and item["set"] == set_name
                                    and item["attribute"] == set_attr_name
                                )
                            ),
                            None,
                        )
                        if attribute is None:
                            self.logger.debug(
                                "Category -> '%s', set -> %s, attribute -> '%s' not found in payload.",
                                cat_name,
                                set_name,
                                set_attr_name,
                            )
                            set_data_attributes[set_attr_id] = ""
                        else:
                            self.logger.debug(
                                "Category -> '%s', set -> %s, attribute -> '%s' found in payload, value -> %s",
                                cat_name,
                                set_name,
                                set_attr_name,
                                attribute["value"],
                            )
                            # Resolve any special cases (e.g. user picker, group picker):
                            set_data_attributes[set_attr_id] = self.resolve_attribute_values(
                                attribute_name=set_attr_name,
                                attribute_type=set_attr_type,
                                attribute_values=attribute["value"],
                            )
                    # end for set_attr_id in set_data_attributes:
                # end elif attr_type == "object":

                #
                # Handle plain attribute (not inside a set):
                #
                else:
                    attr_name = schema_attributes[attr_id]["title"]
                    self.logger.debug("Attribute name -> '%s'", attr_name)
                    # Lookup the attribute with the right category and attribute name in payload:
                    attribute = next(
                        (item for item in categories if (item["name"] == cat_name and item["attribute"] == attr_name)),
                        None,
                    )

                    if attr_type == "array" and "items" in schema_attributes[attr_id]:
                        attr_type = schema_attributes[attr_id]["items"].get("type", attr_type)

                    if attribute is None:
                        self.logger.debug(
                            "Category -> '%s', attribute -> '%s' not found in payload.",
                            cat_name,
                            attr_name,
                        )
                        data_attributes[attr_id] = ""
                    else:
                        self.logger.debug(
                            "Category -> '%s', attribute -> '%s' found in payload, value -> %s",
                            cat_name,
                            attr_name,
                            attribute["value"],
                        )
                        # Resolve any special cases (e.g. user picker, group picker):
                        data_attributes[attr_id] = self.resolve_attribute_values(
                            attribute_name=attr_name,
                            attribute_type=attr_type,
                            attribute_values=attribute["value"],
                        )
                # end else (plain attribute)
            # end for attr_data, attr_schema
            category_create_data[cat_id] = data_attributes
        # end for cat_data, cat_schema

        self.logger.debug("Category create data -> %s", category_create_data)

        return category_create_data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_salesforce_business_object")
    def get_salesforce_business_object(
        self,
        workspace: dict,
        object_type: str,
        search_field: str,
        search_value: str,
    ) -> str | None:
        """Get the Salesforce ID (str) of an Salesforce object by querying the Salesforce API.

        Args:
            workspace (dict):
                The workspace payload element.
            object_type (str):
                The business object type.
            search_field (str):
                Search field to find business object in external system.
            search_value (str):
                Search value to find business object in external system.

        Returns:
            str | None:
                The technical ID of the business object in Salesforce.

        """

        if not self._salesforce:
            self.logger.error(
                "Salesforce connection not initialized! Cannot connect to Salesforce API!",
            )
            return None

        self.logger.debug(
            "Workspaces is connected to Salesforce and we need to lookup the business object ID...",
        )
        salesforce_token = self._salesforce.authenticate()
        if not salesforce_token:
            self.logger.error("Failed to authenticate with Salesforce!")
            return None

        response = self._salesforce.get_object(
            object_type=object_type,
            search_field=search_field,
            search_value=search_value,
            result_fields=["Id"],
        )
        bo_id = self._salesforce.get_result_value(response=response, key="Id")
        num_of_bos = int(response.get("totalSize", 0)) if (response is not None and "totalSize" in response) else 0
        if num_of_bos > 1:
            self.logger.warning(
                "Salesforce lookup delivered %s business objects of type -> '%s'! Picking the first one with ID -> %s...",
                str(num_of_bos),
                str(object_type),
                bo_id,
            )
        if not bo_id:
            self.logger.warning(
                "Business object of type -> '%s' with '%s' = '%s' does not exist in Salesforce!",
                object_type,
                search_field,
                search_value,
            )
            self.logger.info(
                "We try to create the Salesforce object of type -> '%s'...",
                object_type,
            )

            # Get a helper dict to quickly lookup Salesforce properties
            # for given set + attribute name:
            property_lookup = self.get_business_object_properties(
                bo_type_name=object_type,
            )
            # In case we couldn't find properties for the given Business Object Type
            # we bail out...
            if not property_lookup:
                self.logger.warning(
                    "Cannot create Salesforce object - no business object properties found!",
                )
                return None

            categories = workspace.get("categories", [])
            parameter_dict = {}
            # We process all category entries in workspace payload
            # and see if we have a matching mapping to a business property
            # in the BO Type definition:
            for category in categories:
                # generate the lookup key:
                key = ""
                if "set" in category:
                    key += category["set"] + "-"
                key += category.get("attribute")
                # get the attribute value:
                value = category.get("value")
                # lookup the mapping
                mapping = property_lookup.get(key, None)
                # Check if we have a mapping:
                if mapping:
                    property_name = mapping.get("@propertyName", None)
                    self.logger.debug(
                        "Found business property -> '%s' for attribute -> '%s'",
                        property_name,
                        category.get("attribute"),
                    )
                    parameter_dict[property_name] = value
                else:
                    self.logger.debug(
                        "Attribute -> '%s' (key -> %s) does not have a mapped business property.",
                        category.get("attribute"),
                        key,
                    )

            if not parameter_dict:
                self.logger.warning(
                    "Cannot create Salesforce object of type -> '%s' - no parameters found!",
                    object_type,
                )
                return None

            self.logger.info(
                "Create Salesforce object of type -> '%s' with parameters -> %s...",
                object_type,
                str(parameter_dict),
            )
            #
            # Now we try to create the Salesforce object
            #
            response = self._salesforce.add_object(
                object_type=object_type,
                **parameter_dict,
            )
            bo_id = self._salesforce.get_result_value(response=response, key="id")
            if bo_id:
                self.logger.info(
                    "Successfully created Salesforce business object with ID -> %s of type -> '%s'.",
                    bo_id,
                    object_type,
                )
            else:
                self.logger.error(
                    "Failed to create Salesforce business object of type -> '%s'!",
                    object_type,
                )
        else:  # BO found
            self.logger.debug(
                "Retrieved ID -> %s for Salesforce object type -> '%s' (looking up -> '%s' in field -> '%s')",
                bo_id,
                object_type,
                search_value,
                search_field,
            )

        return bo_id

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_guidewire_business_object")
    def get_guidewire_business_object(
        self,
        external_system: dict,
        object_type: str,
        search_field: str,
        search_value: str,
    ) -> str | None:
        """Get the Guidewire ID (str) of an Guidewire object by querying the Guidewire API.

        Args:
            external_system (dict):
                Payload of the external System. Required if Guidewire python object
                needs to be re-initialized.
            object_type (str):
                The business object type (like "Claim", "Policy", "Account").
            search_field (str):
                Search field to find business object in external system.
            search_value (str):
                Search value to find business object in external system.

        Returns:
            str | None:
                The technical ID of the business object in Guidewire. None in case of an error.

        """

        if object_type in ["Account", "Policy", "account", "policy", "gw.account", "gw.policy"]:
            if not self._guidewire_policy_center:
                self._guidewire_policy_center = self.init_guidewire(guidewire_external_system=external_system)
            guidewire_object = self._guidewire_policy_center
        elif object_type in ["Claim", "claim", "gw.claim"]:
            if not self._guidewire_claims_center:
                self._guidewire_claims_center = self.init_guidewire(guidewire_external_system=external_system)
            guidewire_object = self._guidewire_claims_center
        else:
            self.logger.error("Not supported Guidewire object type -> '%s'", object_type)
            return None

        if not guidewire_object:
            self.logger.error(
                "Guidewire connection not initialized! Cannot connect to Guidewire API!",
            )
            return None

        self.logger.debug(
            "Workspaces is connected to Guidewire and we need to lookup the business object ID...",
        )
        guidewire_token = guidewire_object.authenticate()
        if not guidewire_token:
            self.logger.error("Failed to authenticate with Guidewire!")
            return None

        match object_type:
            case "Account" | "account" | "gw.account":
                response = guidewire_object.search_account(
                    attributes={search_field: search_value},
                )
                bo_id = guidewire_object.get_result_value(response=response, key="id")
            case "Policy" | "policy" | "gw.policy":
                response = guidewire_object.search_policy(
                    attributes={search_field: search_value},
                )
                bo_id = guidewire_object.get_result_value(response=response, key="policyId")
            case _:
                self.logger.warning("Currently we only support lookup of 'Account' and 'Policy' objects in Guidewire!")
                return None

        num_of_bos = int(response.get("count", 0)) if (response is not None and "count" in response) else 0
        if num_of_bos > 1:
            self.logger.warning(
                "Guidewire lookup delivered %s business objects of type -> '%s'. Picking the first one with ID -> %s.",
                str(num_of_bos),
                str(object_type),
                bo_id,
            )
        if not bo_id:
            self.logger.warning(
                "Business object of type -> '%s' with '%s' = '%s' does not exist in Guidewire!",
                object_type,
                search_field,
                search_value,
            )
        else:  # BO found
            self.logger.info(
                "Retrieved ID -> %s for Guidewire object type -> '%s' (looking up -> '%s' in field -> '%s').",
                bo_id,
                object_type,
                search_value,
                search_field,
            )

        return bo_id

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="prepare_item_create_form")
    def prepare_item_create_form(
        self,
        parent_id: int,
        categories: list,
        subtype: int = OTCS.ITEM_TYPE_DOCUMENT,
    ) -> dict | None:
        """Prepare the category structure for the item creation.

        Args:
            parent_id (int):
                The node the category should be applied to.
            categories (list):
                The categories list from the document payload.
            subtype (int):
                The subtype of the new node. Default is document.

        Returns:
            dict | None:
                Category structure for workspace creation or None
                in case of an error.

        """

        category_create_data = {}

        category_ids = []

        # Get unique combination of volume, path, name to get a list of all categories
        unique_categories = {
            (entry.get("volume", OTCS.VOLUME_TYPE_CATEGORIES_VOLUME), entry.get("path", None), entry["name"])
            for entry in categories
        }

        # Determine the names of all categories inherited from the parent_id:
        response = self._otcs_frontend.get_node_categories(node_id=parent_id)
        if response and response["results"]:
            category_ids = [
                next(iter(item["metadata_order"]["categories"])) for item in response["results"] if item.get("metadata")
            ]

        for volume, path, name in unique_categories:
            self.logger.debug("%s : %s", name, path)

            if isinstance(path, str):
                path = [path, name]
            elif isinstance(path, list):
                path.append(name)
            else:
                continue

            response = self._otcs_frontend.get_node_by_volume_and_path(
                volume_type=volume,
                path=path,
            )
            cat_id = self._otcs_frontend.get_result_value(response=response, key="id")
            if cat_id and cat_id not in category_ids:
                category_ids.append(cat_id)
        # end for volume, path, name in unique_categories:

        response = self._otcs_frontend.get_node_create_form(
            parent_id=parent_id,
            subtype=subtype,
            category_ids=category_ids,
        )
        if response is None or "forms" not in response:
            self.logger.error(
                "Failed to retrieve create information for subtype -> %s in parent with ID -> %d!",
                subtype,
                parent_id,
            )
            return None

        self.logger.debug(
            "Successfully retrieved create information for subtype -> %s in parent with ID -> %d.",
            subtype,
            parent_id,
        )

        # Process category information
        forms = response["forms"]

        categories_form = {}

        # Typically the the create item form delivers 4-5 forms:
        # 1. Form for System Attributes (has no role name)
        # 2. Form for Category Data (role name = "categories")
        # 3. Form for Classifications (role name = "classifications")
        # 4. Form for importing team members from another existing team
        # 5. Form for Microsoft Teams integration (role name = "microsoftteams")
        for form in forms:
            if "role_name" not in form:
                self.logger.debug("Found 'system attributes' form -> %s", str(form))
                continue
            match form["role_name"]:
                case "categories":
                    categories_form = form
                    self.logger.debug("Found 'categories' form -> %s", form)
                    continue
                case "classifications":
                    self.logger.debug("Found 'classifications' form -> %s", form)
                    continue
                case "rmclassifications":
                    self.logger.debug("Found 'RM classifications' form -> %s", form)
                    continue
                case "importteam":
                    self.logger.debug("Found 'import team participants' form -> %s", form)
                    continue
                case "microsoftteams":
                    self.logger.debug("Found Import Team participants form -> %s", form)
                    continue
                case _:
                    # the remaining option is that this form is the system attributes form:
                    self.logger.warning("Unknown form -> %s", str(form))
            # end match form["role_name"]:
        # end for form in forms:

        # We are just interested in the single category data set (role_name = "categories"):
        data = categories_form.get("data", None)

        if not data:
            self.logger.debug("No categories data found.")
            return category_create_data

        self.logger.debug("Categories data found -> %s", data)
        schema = categories_form["schema"]["properties"]
        self.logger.debug("Categories schema found -> %s", schema)
        # Loop over categories:
        for cat_id in data:  # these are the category IDs (dict keys):
            self.logger.debug("Category ID -> %s", cat_id)
            data_attributes = data[cat_id]
            self.logger.debug("Data attributes -> %s", data_attributes)
            schema_attributes = schema[cat_id]["properties"]
            self.logger.debug("Schema attributes -> %s", schema_attributes)
            cat_name = schema[cat_id]["title"]
            self.logger.debug("Category name -> %s", cat_name)
            # Loop over attributes:
            # * Sets with one (fixed) row have type = object
            # * Multi-value Sets with (multiple) rows have type = array and "properties" in "items" schema
            # * Multi-value attributes have also type = array but NO "properties" in "items" schema
            for attr_id in data_attributes:  # these a attribute IDs (dict keys)
                self.logger.debug("Attribute ID -> %s", attr_id)
                self.logger.debug("Attribute data -> %s", data_attributes[attr_id])
                self.logger.debug(
                    "Attribute schema -> %s",
                    schema_attributes[attr_id],
                )
                attr_type = schema_attributes[attr_id]["type"]
                self.logger.debug("Attribute type -> %s", attr_type)
                if "title" not in schema_attributes[attr_id]:
                    self.logger.debug("Attribute has no title. Skipping...")
                    continue

                #
                # Handle multi-line set:
                #
                if attr_type == "array" and ("properties" in schema_attributes[attr_id]["items"]):
                    set_name = schema_attributes[attr_id]["title"]
                    self.logger.debug("Multi-line set -> %s", set_name)
                    set_data_attributes = data_attributes[attr_id]  # this is a list []
                    if set_data_attributes is None:
                        self.logger.debug("Attribute has no value in data. Skipping...")
                        continue
                    self.logger.debug("Set data attributes -> %s", set_data_attributes)
                    set_schema_attributes = schema_attributes[attr_id]["items"]["properties"]
                    self.logger.debug(
                        "Set schema attributes -> %s",
                        set_schema_attributes,
                    )
                    set_schema_max_rows = schema_attributes[attr_id]["items"]["maxItems"]
                    self.logger.debug("Set schema max rows -> %s", set_schema_max_rows)
                    set_data_max_rows = len(set_data_attributes)
                    self.logger.debug("Set data xax rows -> %s", set_data_max_rows)
                    row = 1
                    # it can happen that the payload contains more rows than the
                    # initial rows in the set data structure. In this case we use
                    # a copy of the data structure from row 0 as template...
                    first_row = dict(set_data_attributes[0])
                    # We don't know upfront how many rows of data we will find in payload
                    # but we at max process the maxItems specified in the schema:
                    while row <= set_schema_max_rows:
                        # Test if we have any payload for this row:
                        attribute = next(
                            (
                                item
                                for item in categories
                                if (
                                    item["name"] == cat_name
                                    and "set" in item  # not all items may have a "set" key
                                    and item["set"] == set_name
                                    and "row" in item  # not all items may have a "row" key
                                    and item["row"] == row
                                )
                            ),
                            None,
                        )
                        # stop if there's no payload for the row:
                        if attribute is None:
                            self.logger.debug(
                                "No payload found for set -> %s, row -> %s",
                                set_name,
                                row,
                            )
                            # we assume that if there's no payload for row n there will be no payload for rows > n
                            # and break the while loop:
                            break
                        # do we need to create a new row in the data frame?
                        if row > set_data_max_rows:
                            # use the row we stored above to create a new empty row:
                            self.logger.debug(
                                "Found payload for row -> %s, we need a new data row for it",
                                row,
                            )
                            self.logger.debug(
                                "Adding an additional row -> %s to set data -> '%s'",
                                row,
                                set_name,
                            )
                            # add the empty dict to the list:
                            set_data_attributes.append(dict(first_row))
                            set_data_max_rows += 1
                        else:
                            self.logger.debug(
                                "Found payload for row -> %s %s we can store in existing data row",
                                row,
                                set_name,
                            )
                        # traverse all attributes in a single row:
                        for set_attr_id in set_schema_attributes:
                            self.logger.debug(
                                "Set attribute ID -> %s (row -> %s)",
                                set_attr_id,
                                row,
                            )
                            self.logger.debug(
                                "Set attribute schema -> %s (row -> %s)",
                                set_schema_attributes[set_attr_id],
                                row,
                            )
                            set_attr_type = set_schema_attributes[set_attr_id]["type"]
                            self.logger.debug(
                                "Set attribute type -> %s (row -> %s)",
                                set_attr_type,
                                row,
                            )
                            set_attr_name = set_schema_attributes[set_attr_id]["title"]
                            self.logger.debug(
                                "Set attribute name -> %s (row -> %s)",
                                set_attr_name,
                                row,
                            )
                            # Lookup the attribute with the right category, set, attribute name, and row number in payload:
                            attribute = next(
                                (
                                    item
                                    for item in categories
                                    if (
                                        item["name"] == cat_name
                                        and "set" in item  # not all items may have a "set" key
                                        and item["set"] == set_name
                                        and item["attribute"] == set_attr_name
                                        and "row" in item  # not all items may have a "row" key
                                        and item["row"] == row
                                    )
                                ),
                                None,
                            )
                            if attribute is None:
                                self.logger.debug(
                                    "Set -> '%s', attribute -> '%s', row -> %s not found in payload.",
                                    set_name,
                                    set_attr_name,
                                    row,
                                )

                                # need to use row - 1 as index starts with 0 but payload rows start with 1
                                set_data_attributes[row - 1][set_attr_id] = ""
                            else:
                                if set_attr_type == "array" and "items" in set_schema_attributes[set_attr_id]:
                                    set_attr_type = set_schema_attributes[set_attr_id]["items"].get(
                                        "type",
                                        set_attr_type,
                                    )

                                self.logger.debug(
                                    "Set -> '%s', attribute -> '%s', row -> %s found in payload, value -> '%s'",
                                    set_name,
                                    set_attr_name,
                                    row,
                                    attribute["value"],
                                )
                                set_data_attributes[row - 1][set_attr_id] = self.resolve_attribute_values(
                                    attribute_name=set_attr_name,
                                    attribute_type=set_attr_type,
                                    attribute_values=attribute["value"],
                                )
                        row += 1  # continue the while loop with the next row
                    # end while row <= set_schema_max_rows:
                # end if attr_type == "array" and ("properties" in schema_attributes[attr_id]["items"]):

                #
                # Handle single-line set:
                #
                elif attr_type == "object":
                    set_name = schema_attributes[attr_id]["title"]
                    self.logger.debug("Single-line set -> '%s'", set_name)
                    set_data_attributes = data_attributes[attr_id]
                    self.logger.debug("Set data attributes -> %s", set_data_attributes)

                    set_schema_attributes = schema_attributes[attr_id]["properties"]
                    self.logger.debug(
                        "Set schema attributes -> %s",
                        str(set_schema_attributes),
                    )
                    # Loop over set attributes:
                    for set_attr_id in set_data_attributes:
                        self.logger.debug("Set attribute ID -> %s", set_attr_id)
                        self.logger.debug(
                            "Set attribute data -> %s",
                            str(set_data_attributes[set_attr_id]),
                        )
                        self.logger.debug(
                            "Set attribute schema -> %s",
                            str(set_schema_attributes[set_attr_id]),
                        )
                        set_attr_type = set_schema_attributes[set_attr_id]["type"]
                        self.logger.debug("Set attribute type -> %s", set_attr_type)
                        set_attr_name = set_schema_attributes[set_attr_id]["title"]
                        self.logger.debug("Set attribute name -> '%s'", set_attr_name)
                        # Lookup the attribute with the right category, set and attribute name in payload:
                        attribute = next(
                            (
                                item
                                for item in categories
                                if (
                                    item["name"] == cat_name
                                    and "set" in item  # not all items may have a "set" key
                                    and item["set"] == set_name
                                    and item["attribute"] == set_attr_name
                                )
                            ),
                            None,
                        )
                        if attribute is None:
                            self.logger.debug(
                                "Category -> '%s', set -> %s, attribute -> '%s' not found in payload.",
                                cat_name,
                                set_name,
                                set_attr_name,
                            )
                            set_data_attributes[set_attr_id] = ""
                        else:
                            self.logger.debug(
                                "Category -> '%s', set -> %s, attribute -> '%s' found in payload, value -> %s",
                                cat_name,
                                set_name,
                                set_attr_name,
                                attribute["value"],
                            )
                            # Resolve any special cases (e.g. user picker, group picker):
                            set_data_attributes[set_attr_id] = self.resolve_attribute_values(
                                attribute_name=set_attr_name,
                                attribute_type=set_attr_type,
                                attribute_values=attribute["value"],
                            )
                    # end for set_attr_id in set_data_attributes:
                # end elif attr_type == "object":

                #
                # Handle plain attribute (not inside a set):
                #
                else:
                    attr_name = schema_attributes[attr_id]["title"]
                    self.logger.debug("Attribute name -> '%s'", attr_name)
                    # Lookup the attribute with the right category and attribute name in payload:
                    attribute = next(
                        (item for item in categories if (item["name"] == cat_name and item["attribute"] == attr_name)),
                        None,
                    )

                    if attr_type == "array" and "items" in schema_attributes[attr_id]:
                        attr_type = schema_attributes[attr_id]["items"].get("type", attr_type)

                    if attribute is None:
                        self.logger.debug(
                            "Category -> '%s', attribute -> '%s' not found in payload.",
                            cat_name,
                            attr_name,
                        )
                        data_attributes[attr_id] = ""
                    else:
                        self.logger.debug(
                            "Category -> '%s', attribute -> '%s' found in payload, value -> %s",
                            cat_name,
                            attr_name,
                            attribute["value"],
                        )
                        # Resolve any special cases (e.g. user picker, group picker):
                        data_attributes[attr_id] = self.resolve_attribute_values(
                            attribute_name=attr_name,
                            attribute_type=attr_type,
                            attribute_values=attribute["value"],
                        )
                # end else (plain attribute)
            # end for attr_data, attr_schema
            category_create_data[cat_id] = data_attributes
        # end for cat_data, cat_schema

        self.logger.debug("Category Create Data -> %s", category_create_data)

        return category_create_data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="prepare_workspace_business_objects")
    def prepare_workspace_business_objects(
        self,
        workspace: dict,
        business_objects: list,
    ) -> list | None:
        """Prepare the business object data for the workspace creation.

        This supports multiple external system connections. This methods
        also checks if the external system is reachable and tries to create
        missing business objects in the leading system if they are missing.

        Args:
            workspace (dict):
                The payload data for the workspace.
            business_objects (list):
                The payload data for the business object connections.

        Returns:
            list | None:
                A list of business object data connections
                (list elements are dictionaries).

        """

        business_object_list = []

        for business_object_data in business_objects:
            business_object = {}

            name = workspace.get("name")

            # Read business object data from workspace payload.
            # business_object_data is a dict with 3-5 elements:
            if "external_system" in business_object_data:
                ext_system_id = business_object_data["external_system"]
            else:
                self.logger.error(
                    "Missing External System in Business Object payload for workspace -> '%s'.",
                    name,
                )
                continue
            if "bo_type" in business_object_data:
                bo_type = business_object_data["bo_type"]
            else:
                self.logger.error(
                    "Missing type in Business Object payload for workspace -> '%s'.",
                    name,
                )
                continue

            if "bo_id" in business_object_data:
                bo_id = business_object_data["bo_id"]
                bo_search_field = None
                bo_search_value = None
            elif "bo_search_field" not in business_object_data or "bo_search_value" not in business_object_data:
                self.logger.error(
                    "Missing BO search fields (bo_search_field, bo_search_value) in Business Object payload without BO ID for workspace -> '%s'.",
                    name,
                )
                continue
            else:
                bo_search_field = business_object_data["bo_search_field"]
                bo_search_value = business_object_data["bo_search_value"]
                bo_id = None

            # Lookup external system in this payload or a former processed payload:
            external_system = self.lookup_external_system(ext_system_id=ext_system_id)

            # If the external system is not in the current payload but in the system
            # we try to avoid errors in the following code.
            # TODO: review REST APIs in OTCS 26.1 version to see if we can improve this.
            if not external_system and self._otcs.get_external_system_connection(connection_name=ext_system_id):
                # As the REST API for reading external system data is pretty much limited
                # we try to do the bare minimum here:
                if "Guidewire" in business_object_data["external_system"]:
                    external_system_type = "Guidewire"
                elif "Salesforce" in business_object_data["external_system"]:
                    external_system_type = "Salesforce"
                external_system = {"enabled": True, "reachable": True, "external_system_type": external_system_type}
                self.logger.info(
                    "External system -> '%s' is not found in current payload but it exists in the system. Construct minimum external system information -> %s.",
                    ext_system_id,
                    str(external_system),
                )

            if not external_system:
                self.logger.warning(
                    "External System -> '%s' does not exist. Cannot connect workspace -> '%s' to -> %s. Create workspace without connection.",
                    ext_system_id,
                    name,
                    ext_system_id,
                )
                continue
            if not external_system.get("enabled", True):
                self.logger.info(
                    "External System -> '%s' is disabled in payload. Cannot connect workspace -> '%s' to -> (%s, %s, %s, %s, %s). Create workspace without connection...",
                    ext_system_id,
                    name,
                    ext_system_id,
                    bo_type,
                    bo_id,
                    bo_search_field,
                    bo_search_value,
                )
                continue
            if not external_system.get("reachable"):
                self.logger.warning(
                    "External System -> '%s' is not reachable. Cannot connect workspace -> '%s' to -> (%s, %s, %s, %s, %s). Create workspace without connection...",
                    ext_system_id,
                    name,
                    ext_system_id,
                    bo_type,
                    bo_id,
                    bo_search_field,
                    bo_search_value,
                )
                continue
            external_system_type = external_system.get("external_system_type", "")

            # For Salesforce we try to determine the actual business object ID (technical ID) if it is
            # not specified directly (but instead a search field and search value):
            if external_system_type == "Salesforce" and not bo_id:
                # If Salesforce external system is used across payloads it may not be initialized here:
                if not self._salesforce:
                    self._salesforce = self.init_salesforce(external_system)
                bo_id = self.get_salesforce_business_object(
                    workspace,
                    object_type=bo_type,
                    search_field=bo_search_field,
                    search_value=bo_search_value,
                )
                if not bo_id:
                    self.logger.warning(
                        "Workspace -> '%s' will not be connected to Salesforce as the Business Object ID couldn't be determined (type -> '%s', search_field -> '%s', search_value -> '%s')!",
                        name,
                        bo_type,
                        bo_search_field,
                        bo_search_value,
                    )
                    continue
            # end if external_system_type == "Salesforce" and not bo_id

            # For Guidewire we try to determine the actual business object ID (technical ID) if it is
            # not specified in the payload (but instead a search field and search value):
            if external_system_type == "Guidewire" and not bo_id:
                bo_id = self.get_guidewire_business_object(
                    external_system=external_system,
                    object_type=bo_type,
                    search_field=bo_search_field,
                    search_value=bo_search_value,
                )
                if not bo_id:
                    self.logger.warning(
                        "Workspace -> '%s' will not be connected to Guidewire as the business object ID couldn't be determined (type -> '%s', search_field -> '%s', search_value -> '%s')!",
                        name,
                        bo_type,
                        bo_search_field,
                        bo_search_value,
                    )
                    continue
            # end if external_system_type == "Guidewire" and not bo_id

            self.logger.info(
                "Workspace -> '%s' will be connected with external system -> '%s' (%s) with (type -> '%s', ID -> '%s').",
                name,
                external_system_type,
                ext_system_id,
                bo_type,
                bo_id,
            )

            business_object["ext_system_id"] = ext_system_id
            business_object["bo_type"] = bo_type
            business_object["bo_id"] = bo_id

            business_object_list.append(business_object)

        return business_object_list

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace")
    def process_workspace(
        self,
        workspace: dict,
    ) -> bool:
        """Worker thread for workspace creation.

        Args:
            workspace (dict):
                Dictionary with payload of a single workspace.

        Returns:
            bool:
                True = Success, False = Failure

        """

        # Read name from payload:
        if "name" not in workspace:
            self.logger.error("Workspace needs a name! Skipping to next workspace...")
            return False
        name = workspace["name"]

        # Check if workspace has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not workspace.get("enabled", True):
            self.logger.info(
                "Payload for workspace -> '%s' is disabled. Skipping...",
                name,
            )
            return True

        # Read optional description from payload:
        description = workspace.get("description", "")

        # Read Type Name from payload:
        if "type_name" not in workspace:
            self.logger.error(
                "Workspace -> '%s' needs a type name! Skipping to next workspace...",
                name,
            )
            return False
        type_name = workspace["type_name"]

        # We need to do this early to find out if we have a cross-application workspace
        # and need to continue even if the workspace does exist...
        if workspace.get("business_objects"):
            business_objects = workspace["business_objects"]

            business_object_list = self.prepare_workspace_business_objects(
                workspace=workspace,
                business_objects=business_objects,
            )
            # Check if any of the external systems are avaiable:
            if business_object_list:
                self.logger.info(
                    "Workspace -> '%s' will be connected to -> %s business object%s.",
                    name,
                    str(len(business_object_list)),
                    "s" if len(business_object_list) > 1 else "",
                )
        else:
            self.logger.debug(
                "Workspace -> '%s' is not connected to any business object.",
                name,
            )
            business_object_list = []

        # Intialize cross-application workspace to "off":
        ibo_workspace_id = None

        # check if the workspace has been created before (effort to make the customizing code idem-potent)
        self.logger.debug(
            "Check if workspace -> '%s' of type -> '%s' does already exist...",
            name,
            type_name,
        )
        # Check if workspace does already exist
        # In case the workspace exists, determine_workspace_id()
        # also stores the node ID into workspace["nodeId"]
        workspace_id = self.determine_workspace_id(workspace=workspace)
        if workspace_id:
            self.logger.info(
                "Workspace -> '%s' of type -> '%s' does already exist and has ID -> %d.",
                name,
                type_name,
                workspace_id,
            )
            # Check if we have an existing workspace that is cross-application.
            # In this case we cannot just skip (return).
            if len(business_object_list) > 1:
                ibo_workspace_id = workspace_id
                self.logger.info(
                    "Workspace -> '%s' is a cross-application workspace so we cannot skip the creation...",
                    name,
                )
            elif not business_object_list:
                self.logger.info(
                    "Workspace -> '%s' does already exist and has no business object references to update - skipping the creation...",
                    name,
                )
                return True

        # Parent ID is optional and only required if workspace type does not specify a create location.
        # This is typically the case if it is a nested workspace or workspaces of the same type can be created
        # in different locations in the Enterprise Workspace:
        parent_id = workspace.get("parent_id")

        if parent_id is not None:
            parent_workspace = next(
                (item for item in self._workspaces if item["id"] == parent_id),
                None,
            )
            if parent_workspace is None:
                self.logger.error(
                    "Parent workspace with logical ID -> %s not found.",
                    parent_id,
                )
                return False

            parent_workspace_node_id = self.determine_workspace_id(
                workspace=parent_workspace,
            )
            if not parent_workspace_node_id:
                self.logger.error(
                    "Parent workspace without node ID (parent workspace creation may have failed). Skipping to next workspace...",
                )
                return False

            self.logger.debug(
                "Parent workspace with logical ID -> %s has node ID -> %s",
                parent_id,
                parent_workspace_node_id,
            )
        else:
            # Alternatively a path could be specified in the payload:
            parent_path = workspace.get("parent_path")
            if parent_path:
                self.logger.info(
                    "Workspace -> '%s' has parent path -> %s specified in payload.",
                    name,
                    parent_path,
                )
                response = self._otcs.get_node_by_volume_and_path(
                    volume_type=self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE,
                    path=parent_path,
                    create_path=True,
                )
                parent_workspace_node_id = self._otcs.get_result_value(
                    response=response,
                    key="id",
                )
            else:
                # if no parent_id is specified the workspace location is determined by the workspace type definition
                # and we pass None as parent ID to the get_workspace_create_form and create_workspace methods below:
                parent_workspace_node_id = None
                self.logger.info(
                    "Workspace -> '%s' has no parent path specified in payload. Using the default defined in the workspace type...",
                    name,
                )

        # Find the workspace type with the name given in the payload:
        workspace_type = next(
            (item for item in self._workspace_types if item["name"] == type_name),
            None,
        )
        if workspace_type is None:
            self.logger.error(
                "Workspace type -> '%s' not found. Skipping to next workspace...",
                type_name,
            )
            return False
        if workspace_type["templates"] == []:
            self.logger.error(
                "Workspace type -> '%s' does not have templates. Skipping to next workspace...",
                type_name,
            )
            return False

        # check if the template to be used is specified in the payload:
        if "template_name" in workspace:
            template_name = workspace["template_name"]
            workspace_template = next(
                (item for item in workspace_type["templates"] if item["name"] == template_name),
                None,
            )
            if workspace_template:  # does this template exist?
                self.logger.debug(
                    "Workspace template -> '%s' has been specified in payload and it does exist.",
                    template_name,
                )
            else:
                self.logger.error(
                    "Workspace template -> '%s' has been specified in payload but it doesn't exist!",
                    template_name,
                )
                self.logger.error(
                    "Workspace type -> '%s' has only these templates -> %s",
                    type_name,
                    workspace_type["templates"],
                )
                return False
        # template to be used is NOT specified in the payload - then we just take the first one:
        else:
            workspace_template = workspace_type["templates"][0]
            self.logger.info(
                "Workspace template has not been specified in payload - taking the first one (%s)...",
                workspace_template,
            )

        template_id = workspace_template["id"]
        template_name = workspace_template["name"]
        workspace_type_id = workspace_type["id"]

        if not workspace_id:
            self.logger.info(
                "Create workspace -> '%s' (type -> '%s') from workspace template -> '%s' (%s)%s...",
                name,
                type_name,
                template_name,
                template_id,
                " with business object references -> {}".format(business_object_list) if business_object_list else "",
            )
        elif business_object_list:
            self.logger.info(
                "Update workspace -> '%s' (type -> '%s') business object references with -> %s...",
                name,
                type_name,
                str(business_object_list),
            )

        # Handle the case where the workspace is not connected
        # to any external system / business object:
        if not business_object_list:
            business_object_list.append(
                {
                    "ext_system_id": None,
                    "bo_type": None,
                    "bo_id": None,
                },
            )

        for business_object in business_object_list:
            external_system_id = business_object["ext_system_id"]
            bo_type = business_object["bo_type"]
            bo_id = business_object["bo_id"]

            if workspace_id and not ibo_workspace_id and bo_id:
                # See if the existing workspace does not yet have a business object references
                # that is given in the payload:
                self.logger.info("Get existing workspace references for workspace -> '%s' (%d)...", name, workspace_id)
                workspace_references = self._otcs.get_workspace_references(node_id=workspace_id)
                workspace_reference = next(
                    (
                        item
                        for item in workspace_references or []
                        if item["external_system_id"] == external_system_id
                        and item["business_object_id"] == bo_id
                        and item["business_object_type"].lower() == bo_type.lower()
                    ),
                    None,
                )
                if not workspace_reference:
                    self.logger.info(
                        "Set workspace reference for workspace -> '%s' (%d) to business object -> ('%s', '%s', %s)...",
                        name,
                        workspace_id,
                        external_system_id,
                        bo_type,
                        bo_id,
                    )
                    response = self._otcs.set_workspace_reference(
                        workspace_id=workspace_id, external_system_id=external_system_id, bo_type=bo_type, bo_id=bo_id
                    )
                    if not response:
                        self.logger.error(
                            "Failed to set a reference for workspace -> '%s' (%d) to business object -> ('%s', '%s', %s)!",
                            name,
                            workspace_id,
                            external_system_id,
                            bo_type,
                            bo_id,
                        )
                        return False
                else:
                    self.logger.info(
                        "Workspace -> '%s' (%d) has already a reference to business object -> ('%s', '%s', %s).",
                        name,
                        workspace_id,
                        external_system_id,
                        bo_type,
                        bo_id,
                    )
                continue

            # Read categories from payload:
            if "categories" in workspace:
                categories = workspace["categories"]
                workspace_category_data = self.prepare_workspace_create_form(
                    categories=categories,
                    template_id=template_id,
                    ext_system_id=business_object["ext_system_id"],
                    bo_type=business_object["bo_type"],
                    bo_id=business_object["bo_id"],
                    parent_workspace_node_id=parent_workspace_node_id,
                )
                if not workspace_category_data:
                    self.logger.error(
                        "Failed to prepare the category data for workspace -> '%s'!",
                        name,
                    )
                    return False
            else:
                self.logger.debug(
                    "Workspace payload has no category data! Will leave category attributes empty...",
                )
                workspace_category_data = {}

            if ibo_workspace_id:
                self.logger.info(
                    "Connect existing workspace -> '%s' to an additional business object of type -> '%s' and ID -> '%s' (IBO)...",
                    name,
                    business_object["bo_type"],
                    business_object["bo_id"],
                )
            # Create the workspace with all provided information:
            response = self._otcs.create_workspace(
                workspace_template_id=template_id,
                workspace_name=name,
                workspace_description=description,
                workspace_type=workspace_type_id,
                category_data=workspace_category_data,
                external_system_id=business_object["ext_system_id"],
                bo_type=business_object["bo_type"],
                bo_id=business_object["bo_id"],
                parent_id=parent_workspace_node_id,
                ibo_workspace_id=ibo_workspace_id,
                show_error=(
                    not self._sap
                ),  # if SAP is active it may produce workspaces concurrently (race condition). Then we don't want to issue errors.
            )
            if response is None:
                # Check if workspace has been concurrently created by some other
                # process (e.g. via SAP or Salesforce). This would be a race condition
                # that seems to really occur.

                # we wait a bit to give the concurrent thread the chance to fully finish the creation:
                time.sleep(5)

                workspace_id = self.determine_workspace_id(workspace=workspace)
                if workspace_id:
                    self.logger.info(
                        "Workspace -> '%s' of type -> '%s' has been created by an external process and has ID -> %s.",
                        name,
                        type_name,
                        workspace_id,
                    )
                    workspace["nodeId"] = workspace_id
                else:
                    self.logger.error(
                        "Failed to create workspace -> '%s' of type -> '%s'!",
                        name,
                        type_name,
                    )
                    return False
            # Now we add the node ID of the new workspace to the payload data structure
            # This will be reused when creating the workspace relationships!
            elif not ibo_workspace_id:
                workspace["nodeId"] = self._otcs.get_result_value(
                    response=response,
                    key="id",
                )
                workspace_id = workspace["nodeId"]
                ibo_workspace_id = workspace["nodeId"]

                # We also get the name the workspace was finally created with.
                # This can be different form the name in the payload as additional
                # naming conventions from the Workspace Type definitions may apply.
                # This is important to make the python container idem-potent.
                response = self._otcs.get_workspace(node_id=workspace["nodeId"])
                workspace["name"] = self._otcs.get_result_value(
                    response=response,
                    key="name",
                )
                # Also update the 'name' variable accordingly, as it is used below.
                name = workspace["name"]

                self.logger.info(
                    "Successfully created workspace with final name -> '%s', type -> '%s', and node ID -> %s.",
                    workspace["name"],
                    type_name,
                    workspace["nodeId"],
                )
        # end for business_object in business_object_list

        # if the workspace creation has failed - e.g. error in lookup of business
        # object in external system then we continue with the next workspace:
        if "nodeId" not in workspace:
            self.logger.error(
                "Couldn't create the workspace -> '%s'. Skipping to next workspace...",
                workspace["name"],
            )
            return False

        # Check if there's an workspace nickname configured:
        if "nickname" in workspace:
            nickname = workspace["nickname"]
            self.logger.info(
                "Assign nickname -> '%s' to workspace -> '%s' (%s)...",
                nickname,
                name,
                workspace["nodeId"],
            )
            response = self._otcs.set_node_nickname(
                node_id=workspace["nodeId"],
                nickname=nickname,
                show_error=True,
            )
            if not response:
                self.logger.error(
                    "Failed to assign nickname -> '%s' to workspace -> '%s' (%s)!",
                    nickname,
                    name,
                    workspace["nodeId"],
                )

        # Check if there's an workspace icon/image configured:
        if "image_nickname" in workspace:
            image_nickname = workspace["image_nickname"]

            response = self._otcs.get_node_from_nickname(nickname=image_nickname)
            node_id = self._otcs.get_result_value(response=response, key="id")
            if node_id:
                mime_type = self._otcs.get_result_value(
                    response=response,
                    key="mime_type",
                )
                if not mime_type:
                    self.logger.warning(
                        "Missing mime type information - assuming 'image/png'...",
                    )
                    mime_type = "image/png"
                file_path = os.path.join(tempfile.gettempdir(), image_nickname)
                result = self._otcs.download_document(node_id=node_id, file_path=file_path)
                if not result:
                    self.logger.error(
                        "Failed to download workspace image with nickname -> '%s' to file -> '%s'!",
                        image_nickname,
                        file_path,
                    )
                else:
                    response = self._otcs.update_workspace_icon(
                        workspace_id=workspace["nodeId"],
                        file_path=file_path,
                        file_mimetype=mime_type,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to assign icon -> '%s' to workspace -> '%s' from file -> '%s'!",
                            image_nickname,
                            name,
                            file_path,
                        )
            else:
                self.logger.error(
                    "Cannot find workspace image with nickname -> '%s' for workspace -> '%s'!",
                    image_nickname,
                    name,
                )

        # Check if an RM classification is specified for the workspace:
        # RM Classification is specified as list of path elements (top-down)
        if "rm_classification_path" in workspace and workspace["rm_classification_path"] != []:
            rm_class_node = self._otcs.get_node_by_volume_and_path(
                volume_type=self._otcs.VOLUME_TYPE_CLASSIFICATION_VOLUME,
                path=workspace["rm_classification_path"],
            )
            rm_class_node_id = self._otcs.get_result_value(
                response=rm_class_node,
                key="id",
            )
            if rm_class_node_id:
                response = self._otcs.assign_rm_classification(
                    node_id=workspace["nodeId"],
                    rm_classification=rm_class_node_id,
                    apply_to_sub_items=False,
                )
                if response is None:
                    self.logger.error(
                        "Failed to assign RM classification -> '%s' (%s) to workspace -> '%s'!",
                        workspace["rm_classification_path"][-1],
                        rm_class_node_id,
                        name,
                    )
                else:
                    self.logger.info(
                        "Assigned RM Classification -> '%s' to workspace -> '%s'.",
                        workspace["rm_classification_path"][-1],
                        name,
                    )
        # Check if one or multiple classifications are specified for the workspace
        # Classifications are specified as list of path elements (top-down)
        if "classification_pathes" in workspace and workspace["classification_pathes"] != []:
            for classification_path in workspace["classification_pathes"]:
                class_node = self._otcs.get_node_by_volume_and_path(
                    volume_type=self._otcs.VOLUME_TYPE_CLASSIFICATION_VOLUME,
                    path=classification_path,
                )
                class_node_id = self._otcs.get_result_value(
                    response=class_node,
                    key="id",
                )
                if class_node_id:
                    response = self._otcs.assign_classifications(
                        node_id=workspace["nodeId"],
                        classifications=[class_node_id],
                        apply_to_sub_items=False,
                    )
                    if response is None:
                        self.logger.error(
                            "Failed to assign classification -> '%s' to workspace -> '%s'!",
                            class_node_id,
                            name,
                        )
                    else:
                        self.logger.info(
                            "Successfully assigned Classification -> '%s' to workspace -> '%s'.",
                            classification_path[-1],
                            name,
                        )

        return True

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspaces_worker")
    def process_workspaces_worker(
        self,
        partition: pd.DataFrame,
        results: list,
    ) -> None:
        """Multi-threading worker method to process a partition of the workspaces.

        Args:
            partition (pd.DataFrame):
                The partition of the workspaces to process by this thread.
            results (list):
                A mutable (shared) list of all workers to collect the results.

        """

        thread_id = threading.get_ident()
        thread_name = threading.current_thread().name

        t = trace.get_current_span()
        t.set_attributes(
            {
                "thread.id": thread_id,
                "thread.name": thread_name,
            }
        )

        result = {}
        result["thread_id"] = thread_id
        result["thread_name"] = thread_name
        result["success"] = True
        result["failure_counter"] = 0
        result["success_counter"] = 0

        total_rows = len(partition)

        # Process all datasets in the partion that was given to the thread:
        for index, row in partition.iterrows():
            # Calculate percentage of completion
            percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

            self.logger.info(
                "Processing data row -> %s to create workspace -> '%s' (%.2f %% complete)...",
                str(index),
                row["name"],
                percent_complete,
            )
            # Convert the row to a dictionary - omitting any empty column:
            workspace = row.dropna().to_dict()
            # workspace is a mutable dictionary that may be updated
            # by process_workspace():
            try:
                success = self.process_workspace(workspace=workspace)
            except Exception:
                self.logger.exception("Failed process workspace -> %s", workspace)
                success = False
            # We need to make sure the row (and the whole data frame)
            # gets these updates back (and adds new columns such as "nodeId"):
            for key, value in workspace.items():
                row[key] = value  # This will update existing keys and add new ones
            self.logger.debug("Final values of row %s -> %s", str(index), str(row))

            # As iterrows() creates a copy of the data we need to
            # write the changes back into the partition
            partition.loc[index] = row

            if success:
                result["success_counter"] += 1
            else:
                self.logger.error(
                    "Failed to process row -> %s for workspace -> '%s'!",
                    str(index),
                    row["name"],
                )
                result["failure_counter"] += 1
                result["success"] = False

        results.append(result)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspaces")
    def process_workspaces(self, section_name: str = "workspaces") -> bool:
        """Process workspaces in payload and create them in Content Server.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        Side Effects:
            Set workspace["nodeId"] to the node ID of the created workspace and update
            the workspace["name"] to the final name of the workspaces (which may be different
            from the ones in the payload depending on workspace type configutrations)

        """

        if not self._workspaces:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            # Read the list of created workspaces from the json file in admin Home
            # This is important in case of restart / rerun of customizer pod
            # as this data structure is used later on for workspace relationship
            # processing (and other) and the workspaces dictionaries have been
            # updated with "nodeId" and "name" (the final names of the workspaces
            # that can be different from the names in the payload)
            self.logger.info(
                "Re-Initialize workspace list from status file -> '%s' to have final names and node IDs...",
                self.get_status_file_name(payload_section_name=section_name),
            )
            self._workspaces = self.get_status_file(payload_section_name=section_name)

            return True

        success: bool = True

        if ENABLE_MULTI_THREADING:
            # Create a list to hold the threads
            threads = []
            # And another list to collect the results
            results = []

            df = Data(self._workspaces, logger=self.logger)

            # Add empty column for "nodeId" so that the worker threads can properly fill it:
            df.get_data_frame()["nodeId"] = None

            self.logger.info(
                "Created a data frame with -> %s rows from the workspaces list with -> %s elements.",
                str(len(df)),
                str(len(self._workspaces)),
            )
            df.print_info()

            partitions = df.partitionate(THREAD_NUMBER)

            # Create and start a thread for each partition
            for index, partition in enumerate(partitions, start=1):
                # start a thread executing the process_workspaces_worker() method below:
                thread = threading.Thread(
                    name=f"{section_name}_{index:02}",
                    target=self.thread_wrapper,
                    args=(self.process_workspaces_worker, partition, results),
                )
                self.logger.info("Starting thread -> '%s'...", str(thread.name))
                thread.start()
                threads.append(thread)
                # Avoid that all threads start at the exact same time with
                # potentially expired cookies that cases race conditions:
                time.sleep(1)
            # end for index, partition in enumerate(partitions, start=1)

            # Wait for all threads to complete
            for thread in threads:
                self.logger.info(
                    "Waiting for thread -> '%s' to complete...",
                    str(thread.name),
                )
                thread.join()
                self.logger.info("Thread -> '%s' has completed.", str(thread.name))

            # As we have basically created a copy of self._workspaces into the Pandas
            # data frame (see df = Data(...) above) and the workspace processing
            # updates the workspaces data with "nodeID" and the final
            # workspace names, we need to write the Pandas Data frame
            # back into the self._workspaces data structure for further processing
            # e.g. in the process_workspace_relationships. Otherwise the
            # changes to "nodeId" or "name" would be lost. We need to do it
            # in 2 steps as we want to avoid to have NaN values in the resulting dicts:
            # 1. Convert the data frame back to a list of dictionaries:
            updated_workspaces = df.get_data_frame().to_dict(orient="records")
            # 2. Remove any dictionary item that has a "NaN" scalar value
            # (pd.notna() only works on scalar values, not on lists!):
            self._workspaces = [
                #                {k: v for k, v in w.items() if pd.notna(v)} for w in updated_workspaces
                {
                    key: value
                    for key, value in updated_workspace.items()
                    if not pd.api.types.is_scalar(value) or pd.notna(value)
                }
                for updated_workspace in updated_workspaces
            ]

            # Print statistics for each thread. In addition,
            # check if all threads have completed without error / failure.
            # If there's a single failure in on of the thread results we
            # set 'success' variable to False.
            results.sort(key=lambda x: x["thread_name"])
            for result in results:
                if not result["success"]:
                    self.logger.info(
                        "Thread -> '%s' had %s failures and completed %s workspaces successfully.",
                        result["thread_name"],
                        result["failure_counter"],
                        result["success_counter"],
                    )
                    success = False  # mark the complete processing as "failure" for the status file.
                else:
                    self.logger.info(
                        "Thread -> '%s' completed %s workspaces successfully.",
                        result["thread_name"],
                        result["success_counter"],
                    )
        else:  # no multi-threading
            for workspace in self._workspaces:
                try:
                    result = self.process_workspace(workspace=workspace)
                except Exception:
                    self.logger.exception("Failed process workspace -> %s", workspace)
                    result = False
                success = success and result  # if a single result is False then mark this in 'success' variable.

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._workspaces,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_relationship")
    def process_workspace_relationship(self, workspace: dict) -> bool:
        """Worker thread for workspace relationship creation.

        Args:
            workspace (dict):
                Dictionary with payload of a single workspace.

        Returns:
            bool:
                True = Success, False = Failure

        """

        # Read name from payload. If it does not exist then bail out:
        name = workspace.get("name")
        if not name:
            return False

        # Check if element has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not workspace.get("enabled", True):
            self.logger.info(
                "Payload for workspace -> '%s' is disabled. Skipping...",
                name,
            )
            return True

        # Read relationships from payload:
        if "relationships" not in workspace:
            self.logger.debug(
                "Workspace -> '%s' has no relationships. Skipping to next workspace...",
                name,
            )
            return True

        # Check that workspaces actually have a logical ID -
        # otherwise we cannot establish the relationship:
        workspace_id = workspace.get("id")
        if not workspace_id:
            self.logger.warning(
                "Workspace without logical ID in payload cannot have a relationship. Skipping to next workspace...",
            )
            return False

        self.logger.info(
            "Workspace -> '%s' (type -> '%s') has relationships - creating...",
            name,
            workspace["type_name"],
        )

        # now determine the actual node ID of the workspace (which should have been created before):
        workspace_node_id = int(self.determine_workspace_id(workspace=workspace))
        if not workspace_node_id:
            self.logger.warning(
                "Workspace -> '%s' (type -> '%s') has no node ID and cannot have a relationship (workspace creation may have failed or final name is different from payload). Skipping to next workspace...",
                name,
                workspace["type_name"],
            )
            return False

        self.logger.debug(
            "Workspace with logical ID -> %s has node ID -> %s",
            str(workspace_id),
            str(workspace_node_id),
        )

        success: bool = True

        for related_workspace in workspace["relationships"]:
            # Initialize variable to determine if we found a related workspace:
            related_workspace_node_id = None
            found_by = ""

            if isinstance(related_workspace, (str, int)):
                #
                # 1. Option: Find the related workspace with the logical ID given in the payload:
                #
                related_workspace_payload = next(
                    (item for item in self._workspaces if str(item["id"]) == str(related_workspace)),
                    None,
                )
                if related_workspace_payload:
                    if not related_workspace_payload.get("enabled", True):
                        self.logger.info(
                            "Payload for related workspace -> '%s' is disabled. Skipping...",
                            related_workspace_payload["name"],
                        )
                        continue

                    related_workspace_node_id = self.determine_workspace_id(
                        workspace=related_workspace_payload,
                    )
                    if not related_workspace_node_id:
                        self.logger.warning(
                            "Related workspace -> '%s' (type -> '%s') has no node ID (workspaces creation may have failed or name is different from payload). Skipping to next workspace...",
                            related_workspace_payload["name"],
                            related_workspace_payload["type_name"],
                        )
                        continue
                    found_by = "logical ID -> '{}' in payload".format(related_workspace)
                # end if related_workspace_payload:

                #
                # 2. Option: Find the related workspace with nickname:
                #
                else:
                    # See if a nickname exists the the provided related_workspace:
                    response = self._otcs.get_node_from_nickname(nickname=related_workspace)
                    related_workspace_node_id = self._otcs.get_result_value(
                        response=response,
                        key="id",
                    )
                    if related_workspace_node_id:
                        found_by = "nickname -> '{}'".format(related_workspace)
            # end if isinstance(related_workspace_id, (str, int)):

            #
            # 3. Option: Find the related workspace type and name:
            #
            elif isinstance(related_workspace, dict):
                related_workspace_type = related_workspace.get("type", None)
                related_workspace_name = related_workspace.get("name", None)
                if related_workspace_type and related_workspace_name:
                    response = self._otcs.get_workspace_by_type_and_name(
                        type_name=related_workspace_type, name=related_workspace_name
                    )
                    related_workspace_node_id = self._otcs.get_result_value(
                        response=response,
                        key="id",
                    )
                    if related_workspace_node_id:
                        found_by = "type -> '{}' and name -> '{}'".format(
                            related_workspace_type, related_workspace_name
                        )
            #
            # 4. Option: Find the related workspace volume and path:
            #
            elif isinstance(related_workspace, list):
                response = self._otcs.get_node_by_volume_and_path(
                    volume_type=self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE, path=related_workspace
                )
                related_workspace_node_id = self._otcs.get_result_value(
                    response=response,
                    key="id",
                )
                if related_workspace_node_id:
                    found_by = "path -> {}".format(related_workspace)

            if related_workspace_node_id is None:
                self.logger.error(
                    "Related workspace -> %s not found.",
                    related_workspace,
                )
                success = False
                continue

            self.logger.debug(
                "Related workspace with %s has node ID -> %s",
                found_by,
                related_workspace_node_id,
            )

            # Check if relationship does already exists:
            response = self._otcs.get_workspace_relationships(
                workspace_id=workspace_node_id,
            )

            existing_workspace_relationship = self._otcs.exist_result_item(
                response=response,
                key="id",
                value=related_workspace_node_id,
            )
            if existing_workspace_relationship:
                self.logger.info(
                    "Workspace relationship between workspace ID -> %s and related workspace ID -> %s does already exist. Skipping...",
                    str(workspace_node_id),
                    related_workspace_node_id,
                )
                continue

            self.logger.info(
                "Create workspace relationship between workspace node ID -> %s and workspace node ID -> %s...",
                str(workspace_node_id),
                related_workspace_node_id,
            )

            response = self._otcs.create_workspace_relationship(
                workspace_id=workspace_node_id,
                related_workspace_id=related_workspace_node_id,
            )
            if not response:
                self.logger.error("Failed to create workspace relationship!")
                success = False
            else:
                self.logger.info("Successfully created workspace relationship.")

        # end for relationships

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_relationships_worker")
    def process_workspace_relationships_worker(
        self,
        partition: pd.DataFrame,
        results: list,
    ) -> None:
        """Multi-threading worker method to process a partition of the workspaces to create workspace relationships.

        Args:
            partition (pd.DataFrame):
                The partition of the workspace relationships to process by this thread.
            results (list):
                A mutable (shared) list of all workers to collect the results.

        """

        thread_id = threading.get_ident()
        thread_name = threading.current_thread().name

        result = {}
        result["thread_id"] = thread_id
        result["thread_name"] = thread_name
        result["success"] = True
        result["failure_counter"] = 0
        result["success_counter"] = 0

        total_rows = len(partition)

        # Process all datasets in the partion that was given to the thread:
        for index, row in partition.iterrows():
            # Calculate percentage of completion
            percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

            self.logger.info(
                "Processing data row -> %s to create relationships for workspace -> '%s' (%.2f %% complete)...",
                str(index),
                row["name"],
                percent_complete,
            )
            success = self.process_workspace_relationship(
                workspace=row.dropna().to_dict(),
            )
            if success:
                result["success_counter"] += 1
            else:
                self.logger.error(
                    "Failed to process row -> %s for relationships of workspace -> '%s'!",
                    str(index),
                    row["name"],
                )
                result["failure_counter"] += 1
                result["success"] = False

        results.append(result)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_relationships")
    def process_workspace_relationships(
        self,
        section_name: str = "workspaceRelationships",
    ) -> bool:
        """Process workspaces relationships in payload and create them in Content Server.

        Relationships can only be created if all workspaces have been created before.
        Once a workspace got created, the node ID of that workspaces has been added
        to the payload["workspaces"] data structure (see process_workspaces())
        Relationships are created between the node IDs of two business workspaces
        (and not the logical IDs in the inital payload specification)

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._workspaces:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        if ENABLE_MULTI_THREADING:
            # Create a list to hold the threads
            threads = []
            # And another list to collect the results
            results = []

            df = Data(self._workspaces, logger=self.logger)

            partitions: list = df.partitionate(number=THREAD_NUMBER)

            # Create and start a thread for each partition
            for index, partition in enumerate(partitions, start=1):
                # start a thread executing the process_workspace_relationships_worker() method below:
                thread = threading.Thread(
                    name=f"{section_name}_{index:02}",
                    target=self.thread_wrapper,
                    args=(
                        self.process_workspace_relationships_worker,
                        partition,
                        results,
                    ),
                )
                self.logger.info("Starting thread -> '%s'...", str(thread.name))
                thread.start()
                threads.append(thread)
                # Avoid that all threads start at the exact same time with
                # potentially expired cookies that cases race conditions:
                time.sleep(1)
            # end for index, partition in enumerate(partitions, start=1)

            # Wait for all threads to complete
            for thread in threads:
                self.logger.info(
                    "Waiting for thread -> '%s' to complete...",
                    str(thread.name),
                )
                thread.join()
                self.logger.info("Thread -> '%s' has completed.", str(thread.name))

            # Print statistics for each thread. In addition,
            # check if all threads have completed without error / failure.
            # If there's a single failure in on of the thread results we
            # set 'success' variable to False.
            results.sort(key=lambda x: x["thread_name"])
            for result in results:
                if not result["success"]:
                    self.logger.info(
                        "Thread -> '%s' had %s failures and completed relationships for %s workspaces successfully.",
                        result["thread_name"],
                        result["failure_counter"],
                        result["success_counter"],
                    )
                    success = False  # mark the complete processing as "failure" for the status file.
                else:
                    self.logger.info(
                        "Thread -> '%s' completed relationships for %s workspace successfully.",
                        result["thread_name"],
                        result["success_counter"],
                    )
        else:  # no multi-threading
            for workspace in self._workspaces:
                result = self.process_workspace_relationship(workspace=workspace)
                success = (
                    success and result
                )  # if a single result is False then the 'success' variable becomes 'False' as well.

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._workspaces,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_members")
    def process_workspace_members(self, section_name: str = "workspaceMembers") -> bool:
        """Process workspaces members in payload and create them in Content Server.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._workspaces:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for workspace in self._workspaces:
            # Read name from payload (just for logging):
            if "name" not in workspace:
                continue
            workspace_name = workspace["name"]

            # Check if workspace has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not workspace.get("enabled", True):
                self.logger.info(
                    "Payload for workspace -> '%s' is disabled. Skipping...",
                    workspace_name,
                )
                continue

            # Read members from payload:
            members = workspace.get("members")
            if not members:
                self.logger.info(
                    "Workspace -> '%s' has no members in payload. Skipping to next workspace...",
                    workspace_name,
                )
                continue

            workspace_id = workspace["id"]

            workspace_node_id = int(self.determine_workspace_id(workspace=workspace))
            if not workspace_node_id:
                self.logger.warning(
                    "Workspace without node ID cannot have members (workspaces creation may have failed). Skipping to next workspace...",
                )
                continue

            self.logger.info(
                "Workspace -> '%s' (%s) has memberships in payload - establishing...", workspace_name, workspace_node_id
            )

            # now determine the actual node IDs of the workspaces (have been created by process_workspaces()):
            workspace_node = self._otcs.get_node(node_id=workspace_node_id)
            workspace_owner_id = self._otcs.get_result_value(
                response=workspace_node,
                key="owner_user_id",
            )
            workspace_owner_name = self._otcs.get_result_value(
                response=workspace_node,
                key="owner",
            )

            workspace_roles = self._otcs.get_workspace_roles(
                workspace_id=workspace_node_id,
            )
            if workspace_roles is None:
                self.logger.debug(
                    "Workspace -> '%s' (%s) has no roles. Skipping to next workspace...",
                    workspace_name,
                    workspace_node_id,
                )
                continue

            # We don't want the workspace creator to be in the leader role
            # of automatically created workspaces - this can happen because the
            # creator gets added to the leader role automatically if
            # the workspace type advanved configuration setting
            # "Add the creator of a business workspace to the Lead role" is
            # enabled:
            roles_iterator = self._otcs.get_result_values_iterator(response=workspace_roles)
            for role in roles_iterator:
                # We can have two leader roles if in a sub-workspaces a leader
                # roles is inherited from the parent workspace. As we want
                # don't want to consider leader role of the parent workspace
                # we check that 'inherited_from_id' is not set:
                if role["leader"] and role["inherited_from_id"] is None:
                    leader_role_id = role["id"]
                    break
            else:
                leader_role_id = None

            if leader_role_id:
                leader_role_name = self._otcs.lookup_result_value(
                    response=workspace_roles,
                    key="leader",
                    value=True,
                    return_key="name",
                )
                response = self._otcs.remove_workspace_member(
                    workspace_id=workspace_node_id,
                    role_id=leader_role_id,
                    member_id=workspace_owner_id,
                    show_warning=False,
                )
                if response:
                    self.logger.info(
                        "Removed creator user -> '%s' (%s) from leader role -> '%s' (%s) of workspace -> '%s' (%s).",
                        workspace_owner_name,
                        workspace_owner_id,
                        leader_role_name,
                        leader_role_id,
                        workspace_name,
                        workspace_node_id,
                    )
                else:
                    self.logger.info(
                        "Creator user -> '%s' (%s) is not in leader role -> '%s' (%s) of workspace -> '%s' (%s). No need to remove it.",
                        workspace_owner_name,
                        workspace_owner_id,
                        leader_role_name,
                        leader_role_id,
                        workspace_name,
                        workspace_node_id,
                    )

            self.logger.info(
                "Adding members to workspace -> '%s' (%s) defined in payload...",
                workspace_name,
                workspace_node_id,
            )

            for member in members:
                # read user list and role name from payload:
                member_users = member.get("users", [])
                member_groups = member.get("groups", [])
                member_role_name = member.get("role", "")
                member_clear = member.get("clear", False)

                if member_role_name == "":  # role name is required
                    self.logger.error(
                        "Members of workspace -> '%s' is missing the role name in the payload.",
                        workspace_name,
                    )
                    success = False
                    continue

                role_id = self._otcs.lookup_result_value(
                    response=workspace_roles,
                    key="name",
                    value=member_role_name,
                    return_key="id",
                )
                if role_id is None:
                    #    if member_role is None:
                    self.logger.error(
                        "Workspace -> '%s' does not have a role -> '%s'",
                        workspace_name,
                        member_role_name,
                    )
                    success = False
                    continue
                inherited_role_id = self._otcs.lookup_result_value(
                    response=workspace_roles,
                    key="name",
                    value=member_role_name,
                    return_key="inherited_from_id",
                )
                if inherited_role_id is not None:
                    self.logger.error(
                        "The role -> '%s' (%s) of workspace -> '%s' (%s) is inherited from role with ID -> %d and members cannot be set in this sub-workspace.",
                        member_role_name,
                        role_id,
                        workspace_name,
                        workspace_node_id,
                        inherited_role_id,
                    )
                    success = False
                    continue
                self.logger.debug(
                    "Role -> '%s' has ID -> %s",
                    member_role_name,
                    role_id,
                )

                # check if we want to clear (remove) existing members of this role:
                if member_clear:
                    self.logger.info(
                        "Clear existing members of role -> '%s' (%s) for workspace -> '%s' (%s)...",
                        member_role_name,
                        role_id,
                        workspace_name,
                        workspace_id,
                    )
                    self._otcs.remove_workspace_members(
                        workspace_id=workspace_node_id,
                        role_id=role_id,
                    )

                if member_users == [] and member_groups == []:  # we either need users or groups (or both)
                    self.logger.debug(
                        "Role -> '%s' of workspace -> '%s' does not have any members (no users nor groups).",
                        member_role_name,
                        workspace_name,
                    )
                    continue

                # Process users as workspaces members:
                for member_user in member_users:
                    # find member user in current payload:
                    member_user_id = next(
                        (item for item in self._users if item["name"] == member_user),
                        {"name": member_user},  # we construct a payload on the fly to make determine_user_id() work
                    )
                    user_id = self.determine_user_id(user=member_user_id)
                    if not user_id:
                        self.logger.error(
                            "Cannot find member user with login -> '%s'. Skipping...",
                            member_user,
                        )
                        continue

                    # Add member if it does not yet exists - suppress warning
                    # message if user is already in role:
                    response = self._otcs.add_workspace_member(
                        workspace_id=workspace_node_id,
                        role_id=int(role_id),
                        member_id=user_id,
                        show_warning=False,
                    )
                    if response is None:
                        self.logger.error(
                            "Failed to add user -> '%s' (%s) as member to role -> '%s' of workspace -> '%s' (%s)!",
                            member_user,
                            user_id,
                            member_role_name,
                            workspace_name,
                            workspace_node_id,
                        )
                        success = False
                    else:
                        self.logger.info(
                            "Successfully added user -> '%s' (%s) as member to role -> '%s' of workspace -> '%s' (%s).",
                            member_user,
                            user_id,
                            member_role_name,
                            workspace_name,
                            workspace_node_id,
                        )

                # Process groups as workspaces members:
                for member_group in member_groups:
                    member_group_id = next(
                        (item for item in self._groups if item["name"] == member_group),
                        None,
                    )

                    group_id = self.determine_group_id(group=member_group_id) if member_group_id else None
                    if not group_id:
                        self.logger.error(
                            "Cannot find member group -> '%s'. Skipping...",
                            member_group,
                        )
                        success = False
                        continue

                    response = self._otcs.add_workspace_member(
                        workspace_id=workspace_node_id,
                        role_id=int(role_id),
                        member_id=group_id,
                        show_warning=False,
                    )
                    if response is None:
                        self.logger.error(
                            "Failed to add group -> '%s' (%s) to role -> '%s' of workspace -> '%s'!",
                            member_group_id["name"],
                            group_id,
                            member_role_name,
                            workspace_name,
                        )
                        success = False
                    else:
                        self.logger.info(
                            "Successfully added group -> '%s' (%s) to role -> '%s' of workspace -> '%s'.",
                            member_group_id["name"],
                            group_id,
                            member_role_name,
                            workspace_name,
                        )

                # Optionally the payload may have a permission list for the role
                # to change the default permission from the workspace template
                # to something more specific:
                member_permissions = member.get("permissions", [])
                if member_permissions == []:
                    self.logger.debug(
                        "No permission change for workspace -> '%s' and role -> '%s'",
                        workspace_name,
                        member_role_name,
                    )
                    continue

                self.logger.info(
                    "Update permissions of workspace -> '%s' (%s) and role -> '%s' to -> %s...",
                    workspace_name,
                    str(workspace_node_id),
                    member_role_name,
                    str(member_permissions),
                )
                response = self._otcs.assign_permission(
                    node_id=workspace_node_id,
                    assignee_type="custom",
                    assignee=role_id,
                    permissions=member_permissions,
                    apply_to=2,
                )
                if not response:
                    self.logger.error(
                        "Failed to update permissions of workspace -> '%s' (%s) and role -> '%s' to -> %s!",
                        workspace_name,
                        str(workspace_node_id),
                        member_role_name,
                        str(member_permissions),
                    )
                    success = False
            # end for member in members:
        # end for workspace in self._workspaces:

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._workspaces,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_member_permissions")
    def process_workspace_member_permissions(
        self,
        section_name: str = "workspaceMemberPermissions",
    ) -> bool:
        """Process workspaces members in payload and set their permissions.

        We need this separate from process_workspace_members() which also
        sets permissions (if in payload) as we add documents to workspaces with
        content transports and these documents don't inherit role permissions
        (this is a transport limitation)

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._workspaces:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for workspace in self._workspaces:
            # Read name from payload (just for logging):
            workspace_name = workspace.get("name")
            if not workspace_name:
                continue

            # Check if workspace has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not workspace.get("enabled", True):
                self.logger.info(
                    "Payload for workspace -> '%s' is disabled. Skipping...",
                    workspace_name,
                )
                continue

            # Read members from payload:
            members = workspace.get("members")
            if not members:
                self.logger.info(
                    "Workspace -> '%s' has no members in payload. No need to update permissions. Skipping to next workspace...",
                    workspace_name,
                )
                continue

            workspace_id = workspace["id"]
            workspace_node_id = int(self.determine_workspace_id(workspace=workspace))
            if not workspace_node_id:
                self.logger.warning(
                    "Workspace without node ID cannot cannot get permission changes (workspaces creation may have failed). Skipping to next workspace...",
                )
                continue

            workspace_roles = self._otcs.get_workspace_roles(
                workspace_id=workspace_node_id,
            )
            if workspace_roles is None:
                self.logger.info(
                    "Workspace with payload ID -> %s and node Id -> %s has no roles to update permissions. Skipping to next workspace...",
                    workspace_id,
                    workspace_node_id,
                )
                continue

            for member in members:
                # read user list and role name from payload:
                member_users = (
                    member["users"] if member.get("users") else []
                )  # be careful to avoid key errors as users are optional
                member_groups = (
                    member["groups"] if member.get("groups") else []
                )  # be careful to avoid key errors as groups are optional
                member_role_name = member["role"]

                if member_role_name == "":  # role name is required
                    self.logger.error(
                        "Members of workspace -> '%s' is missing the role name.",
                        workspace_name,
                    )
                    success = False
                    continue
                if member_users == [] and member_groups == []:  # we either need users or groups (or both)
                    self.logger.debug(
                        "Role -> '%s' of workspace -> '%s' does not have any members (no users nor groups).",
                        member_role_name,
                        workspace_name,
                    )
                    continue

                role_id = self._otcs.lookup_result_value(
                    response=workspace_roles,
                    key="name",
                    value=member_role_name,
                    return_key="id",
                )
                if role_id is None:
                    self.logger.error(
                        "Workspace -> '%s' does not have a role -> '%s'",
                        workspace_name,
                        member_role_name,
                    )
                    success = False
                    continue
                self.logger.debug(
                    "Role -> '%s' has ID -> %s",
                    member_role_name,
                    role_id,
                )

                member_permissions = member.get("permissions", [])
                if member_permissions == []:
                    self.logger.debug(
                        "No permission change for workspace -> '%s' and role -> '%s'.",
                        workspace_name,
                        member_role_name,
                    )
                    continue

                self.logger.info(
                    "Update permissions of workspace -> '%s' (%s) and role -> '%s' to -> %s...",
                    workspace_name,
                    str(workspace_node_id),
                    member_role_name,
                    str(member_permissions),
                )
                response = self._otcs.assign_permission(
                    node_id=workspace_node_id,
                    assignee_type="custom",
                    assignee=role_id,
                    permissions=member_permissions,
                    apply_to=2,  # inherit to all sub-folders
                )
                if not response:
                    self.logger.error(
                        "Failed to update permissions of workspace -> '%s' (%s) and role -> '%s' to -> %s!",
                        workspace_name,
                        str(workspace_node_id),
                        member_role_name,
                        str(member_permissions),
                    )
                    success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._workspaces,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_aviators")
    def process_workspace_aviators(
        self,
        section_name: str = "workspaceAviators",
    ) -> bool:
        """Process workspaces Content Aviator settings in payload and enable Aviator for selected workspaces.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._workspaces:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for workspace in self._workspaces:
            # Read name from payload (just for logging):
            workspace_name = workspace.get("name")
            if not workspace_name:
                continue

            # Check if workspace has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not workspace.get("enabled", True):
                self.logger.info(
                    "Payload for workspace -> '%s' is disabled. Skipping...",
                    workspace_name,
                )
                continue

            # Read Aviator setting from payload:
            if not workspace.get("enable_aviator", False):
                self.logger.info(
                    "Aviator is not enabled for workspace -> '%s'. Skipping to next workspace...",
                    workspace_name,
                )
                continue

            # We cannot just lookup with workspace.get("nodeId") as the customizer
            # may have been restarted inbetween - so we use our proper determine_workspace_id
            # here...
            workspace_id = self.determine_workspace_id(workspace=workspace)
            if not workspace_id:
                self.logger.error(
                    "Cannot find node ID for workspace -> '%s'. Workspace creation may have failed. Skipping to next workspace...",
                    workspace_name,
                )
                success = False
                continue

            # Make code idem-potent and check if Aviator is already enabled
            # for this workspace:
            if self._otcs.check_workspace_aviator(workspace_id=workspace_id):
                self.logger.info(
                    "Skip workspace -> '%s' (%s) as Aviator is already enabled...",
                    workspace_name,
                    workspace_id,
                )
                continue

            # Now enable the Content Aviator for the workspace:
            response = self._otcs.update_workspace_aviator(
                workspace_id=workspace_id,
                status=True,
            )
            if not response:
                self.logger.error(
                    "Failed to enable Content Aviator for workspace -> '%s' (%s)!",
                    workspace_name,
                    workspace_id,
                )
                success = False
                continue
            # Also embed the workspace metadata:
            if not self._otcs.aviator_embed_metadata(
                node_id=workspace_id,
                workspace_metadata=True,
                wait_for_completion=False,
            ):
                success = False
                continue
        # end for workspace in self._workspaces

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._workspaces,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_web_reports")
    def process_web_reports(
        self,
        web_reports: list,
        section_name: str = "webReports",
    ) -> bool:
        """Process web reports in payload and run them in Content Server.

        Args:
            web_reports (list):
                The payload list of web reports. As we have two different list (pre and post)
                we need to pass the actual list as parameter.
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if a restart of the OTCS pods is required. False otherwise.

        """

        if not web_reports:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return False  # important to return False here as otherwise we are triggering a restart of services!!

        # If this payload section has been processed successfully before we
        # can return False and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return False  # important to return False here as otherwise we are triggering a restart of services!!

        restart_required: bool = False
        success: bool = True

        for web_report in web_reports:
            nickname = web_report.get("nickname")
            if not nickname:
                self.logger.error("Web Report payload needs a nickname! Skipping...")
                continue

            # Check if web report has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not web_report.get("enabled", True):
                self.logger.info(
                    "Payload for Web Report -> '%s' is disabled. Skipping...",
                    nickname,
                )
                continue

            description = web_report.get("description", "")
            restart = web_report.get("restart", False)

            if not self._otcs.get_node_from_nickname(nickname=nickname):
                self.logger.error(
                    "Web Report with nickname -> '%s' does not exist! Skipping...",
                    nickname,
                )
                success = False
                continue

            # be careful to avoid key errors as Web Report parameters are optional:
            actual_params = web_report["parameters"] if web_report.get("parameters") else {}
            formal_params = self._otcs.get_web_report_parameters(nickname=nickname)
            if actual_params:
                self.logger.info(
                    "Running Web Report -> '%s' (%s) with parameters -> %s ...",
                    nickname,
                    description,
                    actual_params,
                )
                # Do some sanity checks to see if the formal and actual parameters are matching...
                # Check 1: are there formal parameters at all?
                if not formal_params:
                    self.logger.error(
                        "Web Report -> '%s' is called with actual parameters but it does not expect parameters! Skipping...",
                        nickname,
                    )
                    success = False
                    continue
                lets_continue = False
                # Check 2: Iterate through the actual parameters given in the payload
                # and see if there's a matching formal parameter expected by the Web Report:
                for key, value in actual_params.items():
                    # Check if there's a matching formal parameter defined on the Web Report node:
                    formal_param = next(
                        (item for item in formal_params if item["parm_name"] == key),
                        None,
                    )
                    if formal_param is None:
                        self.logger.error(
                            "Web Report -> '%s' is called with parameter -> '%s' that is not expected! Value: %s) Skipping...",
                            nickname,
                            key,
                            value,
                        )
                        success = False
                        lets_continue = True  # we cannot do a "continue" here directly as we are in an inner loop
                # Check 3: Iterate through the formal parameters and validate there's a matching
                # actual parameter defined in the payload for each mandatory formal parameter
                # that does not have a default value:
                for formal_param in formal_params:
                    if (
                        (formal_param["mandatory"] is True)
                        and (formal_param["default_value"] is None)
                        and not actual_params.get(formal_param["parm_name"])
                    ):
                        self.logger.error(
                            "Web Report -> '%s' is called without mandatory parameter -> %s! Skipping...",
                            nickname,
                            formal_param["parm_name"],
                        )
                        success = False
                        lets_continue = True  # we cannot do a "continue" here directly as we are in an inner loop
                # Did any of the checks fail?
                if lets_continue:
                    continue
                # Actual parameters are validated, we can run the Web Report:
                response = self._otcs.run_web_report(
                    nickname=nickname,
                    web_report_parameters=actual_params,
                )
            # end if actual_params
            else:
                self.logger.info(
                    "Running Web Report -> '%s' (%s) without parameters...",
                    nickname,
                    description,
                )
                # Check if there's a formal parameter that is mandatory but
                # does not have a default value:
                if formal_params:
                    required_param = next(
                        (item for item in formal_params if (item["mandatory"] is True) and (not item["default_value"])),
                        None,
                    )
                    if required_param:
                        self.logger.error(
                            "Web Report -> '%s' is called without parameters but has a mandatory parameter -> '%s' without a default value! Skipping...",
                            nickname,
                            required_param["parm_name"],
                        )
                        success = False
                        continue
                    # we are good to proceed!
                    self.logger.debug(
                        "Web Report -> '%s' does not have a mandatory parameter without a default value!",
                        nickname,
                    )
                response = self._otcs.run_web_report(nickname=nickname)
            # end else
            if response is None:
                self.logger.error(
                    "Failed to run web report with nickname -> '%s'!",
                    nickname,
                )
                success = False

            if restart:
                restart_required = True

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=web_reports,
        )

        return restart_required

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_cs_applications")
    def process_cs_applications(
        self,
        otcs_object: OTCS,
        section_name: str = "csApplications",
    ) -> bool:
        """Process CS applications in payload and install them in Content Server.

        The CS Applications need to be installed in all frontend and backends.

        Args:
            otcs_object (object):
                This can either be the OTCS frontend or OTCS backend. If None
                then the otcs_backend is used.
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._cs_applications:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # OTCS backend is the default:
        if not otcs_object:
            otcs_object = self._otcs_backend

        for cs_application in self._cs_applications:
            application_name = cs_application.get("name")
            if not application_name:
                self.logger.error("Missing CS application name in payload! Skipping...")
                continue

            # Check if CS Application has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not cs_application.get("enabled", True):
                self.logger.info(
                    "Payload for CS Application -> '%s' is disabled. Skipping...",
                    application_name,
                )
                continue

            application_description = cs_application.get("description", "")

            self.logger.info(
                "Install CS Application -> '%s'%s...",
                application_name,
                " ({})".format(application_description) if application_description else "",
            )
            response = otcs_object.install_cs_application(
                application_name=application_name,
            )
            if response is None:
                self.logger.error(
                    "Failed to install CS Application -> '%s'!",
                    application_name,
                )
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._cs_applications,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_settings")
    def process_user_settings(self, section_name: str = "userSettings") -> bool:
        """Process user settings in payload and apply them in OTDS.

        This includes password settings and user display settings.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for user in self._users:
            user_name = user.get("name")
            if not user_name:
                self.logger.error(
                    "Missing user name - cannot apply user settings. Skipping user...",
                )
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            self._log_header_callback(
                text="Process settings for user -> '{}'".format(user_name),
                char="-",
            )

            user_partition = self._otcs.config().get("partition", None)
            if not user_partition:
                self.logger.error("User partition not found!")
                success = False
                continue

            # Set the OTDS display name. Content Server does not use this but
            # it makes AppWorks display users correctly (and it doesn't hurt)
            # We only set this if firstname _and_ last name are in the payload:
            if "firstname" in user and "lastname" in user:
                user_display_name = user["firstname"] + " " + user["lastname"]
                response = self._otds.update_user(
                    partition=user_partition,
                    user_id=user_name,
                    attribute_name="displayName",
                    attribute_value=user_display_name,
                )
                if response:
                    self.logger.info(
                        "Successfully updated display name of user -> '%s' to -> '%s'.",
                        user_name,
                        user_display_name,
                    )
                else:
                    self.logger.error(
                        "Failed to update display name of user -> '%s' to -> '%s'!",
                        user_name,
                        user_display_name,
                    )
                    success = False

            # Don't enforce the user to reset password at first login (settings in OTDS):
            self.logger.info(
                "Don't enforce password change for user -> '%s'...",
                user_name,
            )
            response = self._otds.update_user(
                partition=user_partition,
                user_id=user_name,
                attribute_name="UserMustChangePasswordAtNextSignIn",
                attribute_value="False",
            )
            if not response:
                success = False

            response = self._otds.update_user(
                partition=user_partition,
                user_id=user_name,
                attribute_name="UserCannotChangePassword",
                attribute_value="True",
            )
            if not response:
                success = False

            # Set user password to never expire
            response = self._otds.update_user(
                partition=user_partition,
                user_id=user_name,
                attribute_name="PasswordNeverExpires",
                attribute_value="True",
            )
            if not response:
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_favorites_and_profiles")
    def process_user_favorites_and_profiles(
        self,
        section_name: str = "userFavoritesAndProfiles",
    ) -> bool:
        """Process user favorites in payload and create them in Content Server.

        This method also simulates browsing the favorites to populate the
        widgets on the landing pages and sets personal preferences.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        for dic in self._payload_sections:
            if dic.get("name") == "users":
                users_payload = dic
                break
        smartui_theme = "jato" if users_payload.get("jato_enabled", True) else "cf"

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # We can only set favorites if we impersonate / authenticate as the user.
        # The following code (for loop) will change the authenticated user - we need to
        # switch it back to admin user later so we safe the admin credentials for this:

        for user in self._users:
            with tracer.start_as_current_span("process_user_favorites_and_profiles-user") as t:
                t.set_attribute("class", "otcs")
                user_name = user.get("name")
                if not user_name:
                    self.logger.error(
                        "Missing user name - cannot configure user favorites and profile. Skipping user...",
                    )
                    continue

                self._log_header_callback(
                    text="Process Favorites and Profile for user -> '{}'".format(user_name),
                    char="-",
                )

                # Check if user has been explicitly disabled in payload
                # (enabled = false). In this case we skip the element:
                if not user.get("enabled", True):
                    self.logger.info(
                        "Payload for user -> '%s' is disabled. Skipping...",
                        user_name,
                    )
                    continue

                # We skip also user of type "ServiceUser":
                if user.get("type", "User") == "ServiceUser":
                    self.logger.info("Skipping service user -> '%s'...", user_name)
                    continue

                # Impersonate as the user:
                self.logger.info("Impersonate user -> '%s'...", user_name)
                result = self.start_impersonation(username=user_name)
                if not result:
                    self.logger.error("Couldn't impersonate user -> '%s'!", user_name)
                    success = False
                    continue

                # Configure default Theme to be Jato if configured
                user_smartui_theme = user.get("smartui_theme", smartui_theme)
                if user_smartui_theme != "cf":
                    response = self._otcs.update_user_profile(
                        config_section="SmartUI",
                        field="theme",
                        value=user_smartui_theme,
                    )
                    if response is None:
                        self.logger.warning(
                            "Profile for user -> '%s' couldn't be updated to Smart View theme -> '%s'!",
                            user_name,
                            user_smartui_theme,
                        )
                    else:
                        self.logger.info(
                            "Profile for user -> '%s' has been updated to Smart View theme -> '%s'.",
                            user_name,
                            user_smartui_theme,
                        )

                # Update the user profile to activate responsive (dynamic) containers:
                response = self._otcs.update_user_profile(
                    field="responsiveContainerMode",
                    value=True,
                    config_section="SmartUI",
                )
                if response is None:
                    self.logger.warning(
                        "Profile for user -> '%s' couldn't be updated with responsive container mode!",
                        user_name,
                    )
                else:
                    self.logger.info(
                        "Profile for user -> '%s' has been updated to enable responsive container mode.",
                        user_name,
                    )
                response = self._otcs.update_user_profile(
                    field="responsiveContainerMessageMode",
                    value=True,
                    config_section="SmartUI",
                )
                if response is None:
                    self.logger.warning(
                        "Profile for user -> '%s' couldn't be updated with responsive container message mode!",
                        user_name,
                    )
                else:
                    self.logger.info(
                        "Profile for user -> '%s' has been updated to enable messages for responsive container mode.",
                        user_name,
                    )

                restrict_personal_workspace = user.get("restrict_personal_workspace", False)
                if restrict_personal_workspace:
                    # Let the user restrict itself to have read-only access to its
                    # personal workspace:
                    node = self._otcs.get_node_by_volume_and_path(
                        volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
                        path=[],
                    )
                    node_id = self._otcs.get_result_value(response=node, key="id")
                    if node_id:
                        self.logger.info(
                            "Restricting Personal Workspace of user -> '%s' to read-only.",
                            user_name,
                        )
                        response = self._otcs.assign_permission(
                            node_id=int(node_id),
                            assignee_type="owner",
                            assignee=0,
                            permissions=["see", "see_contents"],
                            apply_to=2,
                        )

                # We work through the list of favorites defined for the user:
                favorites = user.get("favorites", [])
                for favorite in favorites:
                    # We try three things to determine the favorite node:
                    # 1. If the favorite is a path (a python list) then we try to resolve
                    #    this path in the Enterprise Workspace.
                    # 2. We try to find the item (string) as a logical
                    #    workspace ID inside the payload.
                    # 3. We try to find the item (string) as a nickname in OTCS.
                    favorite_id = None
                    is_workspace = False
                    # 1. Check if the favorite item is itself a list,
                    #    then we try to interpret it as a path in
                    #    the enterprise workspace:
                    if isinstance(favorite, list):
                        favorite_item = self._otcs.get_node_by_volume_and_path(
                            volume_type=self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE,
                            path=favorite,
                        )
                        if not favorite_item:
                            self.logger.error("Cannot find path -> %s for favorite item!", str(favorite))
                            success = False
                            continue
                        favorite_id = self._otcs.get_result_value(
                            response=favorite_item,
                            key="id",
                        )
                        favorite_name = self._otcs.get_result_value(
                            response=favorite_item,
                            key="name",
                        )
                        favorite_type = self._otcs.get_result_value(
                            response=favorite_item,
                            key="type",
                        )
                        if favorite_type == self._otcs.ITEM_TYPE_BUSINESS_WORKSPACE:
                            is_workspace = True
                    # 2. Check if it a logical workspace identifier in the payload:
                    if not favorite_id:
                        # check if favorite is a logical workspace name
                        favorite_item = next(
                            (item for item in self._workspaces if item["id"] == favorite),
                            None,
                        )
                        if favorite_item:
                            if favorite_item.get("enabled", True):
                                self.logger.debug(
                                    "Found favorite item (workspace) -> '%s' in payload and it is enabled.",
                                    favorite_item["name"],
                                )
                            else:
                                self.logger.info(
                                    "Found favorite item (workspace) -> '%s' in payload but it is not enabled. Skipping...",
                                    favorite_item["name"],
                                )
                                continue
                            favorite_id = self.determine_workspace_id(workspace=favorite_item)
                            if not favorite_id:
                                self.logger.warning(
                                    "Workspace of type -> '%s' and name -> '%s' does not exist. Cannot create favorite. Skipping...",
                                    favorite_item["type_name"],
                                    favorite_item["name"],
                                )
                                continue
                            is_workspace = True
                            favorite_name = favorite_item["name"]
                    # 3. Check if it is a nickname:
                    if not favorite_id:
                        favorite_item = self._otcs.get_node_from_nickname(nickname=favorite)
                        favorite_id = self._otcs.get_result_value(
                            response=favorite_item,
                            key="id",
                        )
                        favorite_name = self._otcs.get_result_value(
                            response=favorite_item,
                            key="name",
                        )
                        favorite_type = self._otcs.get_result_value(
                            response=favorite_item,
                            key="type",
                        )
                        if favorite_type == self._otcs.ITEM_TYPE_BUSINESS_WORKSPACE:
                            is_workspace = True

                    # If nothing has worked then skip this payload favorite.
                    if not favorite_id:
                        self.logger.warning(
                            "Favorite -> '%s' neither found as workspace payload ID, not as a path, nor as nickname. Skipping to next favorite...",
                            favorite,
                        )
                        continue

                    response = self._otcs.add_favorite(node_id=favorite_id)
                    if response is None:
                        self.logger.warning(
                            "Favorite -> '%s' (%s) couldn't be added for user -> '%s'!",
                            favorite_name,
                            favorite_id,
                            user_name,
                        )
                    else:
                        self.logger.info(
                            "Added favorite -> '%s' (%s) for user -> '%s'.",
                            favorite_name,
                            favorite_id,
                            user_name,
                        )
                        self.logger.info(
                            "Simulate user -> '%s' browsing node -> '%s' (%s)...",
                            user_name,
                            favorite_name,
                            favorite_id,
                        )
                        # simulate a browse by the user to populate recently accessed items
                        response = (
                            self._otcs.get_workspace(node_id=favorite_id, fields="properties{id}")
                            if is_workspace
                            else self._otcs.get_node(node_id=favorite_id, fields="properties{id}")
                        )
                # end for favorite in favorites

                # we work through the list of proxies defined for the user
                # (we need to consider that not all users have the proxies element):
                proxies = user.get("proxies", [])

                for proxy in proxies:
                    proxy_user = next(
                        (item for item in self._users if item["name"] == proxy),
                        None,
                    )
                    if not proxy_user or "id" not in proxy_user:
                        self.logger.error(
                            "The proxy -> '%s' for user -> '%s' does not exist! Skipping proxy...",
                            proxy,
                            user_name,
                        )
                        success = False
                        continue
                    proxy_user_id = proxy_user["id"]

                    # Check if the proxy is already set:
                    if not self._otcs.is_proxy(user_name=proxy):
                        self.logger.info(
                            "Set user -> '%s' (%s) as proxy for user -> '%s'.",
                            proxy,
                            proxy_user_id,
                            user_name,
                        )
                        # set the user proxy - currently we don't support time based proxies in payload.
                        # The called method is ready to support this.
                        response = self._otcs.add_user_proxy(proxy_user_id=proxy_user_id)
                    else:
                        self.logger.info(
                            "User -> '%s' (%s) is already proxy for user -> '%s'. Skipping...",
                            proxy,
                            proxy_user_id,
                            user_name,
                        )
            # end for user in self._users

        if self._users:
            # Impersonate as the admin user:
            self.logger.info(
                "Impersonate as admin user -> '%s'...",
                self._otcs.credentials()["username"],
            )
            # Stop the impersonation as a user:
            result = self.stop_impersonation()
            if not result:
                self.logger.error(
                    "Couldn't impersonate as admin user -> '%s'!",
                    self._otcs.credentials()["username"],
                )
                success = False

        # Also for the admin user we want to update the user profile to activate responsive (dynamic) containers:
        response = self._otcs.update_user_profile(
            field="responsiveContainerMode",
            value=True,
            config_section="SmartUI",
        )
        if response is None:
            self.logger.warning(
                "Profile for 'admin' user couldn't be updated with responsive container mode!",
            )
        else:
            self.logger.info(
                "Profile for 'admin' user has been updated to enable responsive container mode.",
            )
        response = self._otcs.update_user_profile(
            field="responsiveContainerMessageMode",
            value=True,
            config_section="SmartUI",
        )
        if response is None:
            self.logger.warning(
                "Profile for 'admin' user couldn't be updated with responsive container message mode!",
            )
        else:
            self.logger.info(
                "Profile for 'admin' user has been updated to enable messages for responsive container mode.",
            )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_security_clearances")
    def process_security_clearances(
        self,
        section_name: str = "securityClearances",
    ) -> bool:
        """Process Security Clearances for Content Server.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._security_clearances:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for security_clearance in self._security_clearances:
            clearance_level = security_clearance.get("level")
            if not clearance_level:
                self.logger.error(
                    "Security clearance requires a level in the payload. Skipping.",
                )
                continue
            clearance_name = security_clearance.get("name")
            if not clearance_name:
                self.logger.error(
                    "Security clearance requires a name in the payload. Skipping.",
                )
                continue

            # Check if security clearance has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not security_clearance.get("enabled", True):
                self.logger.info(
                    "Payload for security clearance -> '%s' is disabled. Skipping...",
                    clearance_name,
                )
                continue

            clearance_description = security_clearance.get("description", "")
            if clearance_level and clearance_name:
                self.logger.info(
                    "Creating security clearance -> '%s' : %s%s...",
                    clearance_level,
                    clearance_name,
                    " ({})".format(clearance_description) if clearance_description else "",
                )
                self._otcs.run_web_report(
                    nickname="web_report_security_clearance",
                    web_report_parameters=security_clearance,
                )
            else:
                self.logger.error(
                    "Cannot create security clearance - either level or name is missing!",
                )
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._security_clearances,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_supplemental_markings")
    def process_supplemental_markings(
        self,
        section_name: str = "supplementalMarkings",
    ) -> bool:
        """Process Supplemental Markings for Content Server.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._supplemental_markings:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for supplemental_marking in self._supplemental_markings:
            code = supplemental_marking.get("code")

            # Check if supplemental marking has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not supplemental_marking.get("enabled", True):
                self.logger.info(
                    "Payload for supplemental marking -> '%s' is disabled. Skipping...",
                    code,
                )
                continue

            description = supplemental_marking.get("description", "")

            if code:
                self.logger.info(
                    "Creating supplemental marking -> '%s'%s...",
                    code,
                    " ({})".format(description) if description else "",
                )
                self._otcs.run_web_report(
                    nickname="web_report_supplemental_marking",
                    web_report_parameters=supplemental_marking,
                )
            else:
                self.logger.error(
                    "Cannot create supplemental marking - either code or description is missing!",
                )
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._supplemental_markings,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_security")
    def process_user_security(self, section_name: str = "userSecurity") -> bool:
        """Process Security Clearance and Supplemental Markings for Content Server users.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for user in self._users:
            user_name = user.get("name")

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            user_id = user.get("id")
            if not user_id:
                self.logger.error(
                    "User is missing an ID. It was likely not created. Skipping to next user...",
                )
                success = False
                continue

            # Read security clearance from user payload (it is optional!)
            user_security_clearance = user.get("security_clearance")
            if user_id and user_security_clearance:
                self._otcs.assign_user_security_clearance(
                    user_id=user_id,
                    security_clearance=user_security_clearance,
                )

            # Read supplemental markings from user payload (it is optional!)
            user_supplemental_markings = user.get("supplemental_markings")
            if user_id and user_supplemental_markings:
                self._otcs.assign_user_supplemental_markings(
                    user_id=user_id,
                    supplemental_markings=user_supplemental_markings,
                )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_records_management_settings")
    def process_records_management_settings(
        self,
        section_name: str = "recordsManagementSettings",
    ) -> bool:
        """Process Records Management Settings for Content Server.

        The setting files need to be placed in the OTCS file system file via
        a transport into the Support Asset Volume.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._records_management_settings:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        if (
            "records_management_system_settings" in self._records_management_settings
            and self._records_management_settings["records_management_system_settings"] != ""
        ):
            filename = (
                self._custom_settings_dir + self._records_management_settings["records_management_system_settings"]
            )
            response = self._otcs.import_records_management_settings(file_path=filename)
            if not response:
                success = False

        if (
            "records_management_codes" in self._records_management_settings
            and self._records_management_settings["records_management_codes"] != ""
        ):
            filename = self._custom_settings_dir + self._records_management_settings["records_management_codes"]
            response = self._otcs.import_records_management_codes(file_path=filename)
            if not response:
                success = False

        if (
            "records_management_rsis" in self._records_management_settings
            and self._records_management_settings["records_management_rsis"] != ""
        ):
            filename = self._custom_settings_dir + self._records_management_settings["records_management_rsis"]
            response = self._otcs.import_records_management_rsis(file_path=filename)
            if not response:
                success = False

        if (
            "physical_objects_system_settings" in self._records_management_settings
            and self._records_management_settings["physical_objects_system_settings"] != ""
        ):
            filename = self._custom_settings_dir + self._records_management_settings["physical_objects_system_settings"]
            response = self._otcs.import_physical_objects_settings(file_path=filename)
            if not response:
                success = False

        if (
            "physical_objects_codes" in self._records_management_settings
            and self._records_management_settings["physical_objects_codes"] != ""
        ):
            filename = self._custom_settings_dir + self._records_management_settings["physical_objects_codes"]
            response = self._otcs.import_physical_objects_codes(file_path=filename)
            if not response:
                success = False

        if (
            "physical_objects_locators" in self._records_management_settings
            and self._records_management_settings["physical_objects_locators"] != ""
        ):
            filename = self._custom_settings_dir + self._records_management_settings["physical_objects_locators"]
            response = self._otcs.import_physical_objects_locators(file_path=filename)
            if not response:
                success = False

        if (
            "security_clearance_codes" in self._records_management_settings
            and self._records_management_settings["security_clearance_codes"] != ""
        ):
            filename = self._custom_settings_dir + self._records_management_settings["security_clearance_codes"]
            response = self._otcs.import_security_clearance_codes(file_path=filename)
            if not response:
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._records_management_settings,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_holds")
    def process_holds(self, section_name: str = "holds") -> bool:
        """Process Records Management Holds.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._holds:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for hold in self._holds:
            hold_name = hold.get("name")
            if not hold_name:
                self.logger.error("Cannot create a hold without a name! Skipping...")
                continue

            hold_type = hold.get("type")
            if not hold_type:
                self.logger.error(
                    "Cannot create hold -> '%s' without a type! Skipping...",
                    hold_name,
                )
                success = False
                continue

            # Check if hold has been explicitly disabled in payload
            # (enabled = false). In this case we skip this payload element:
            if not hold.get("enabled", True):
                self.logger.info(
                    "Payload for hold -> '%s' (%s) is disabled. Skipping...",
                    hold_name,
                    hold_type,
                )
                continue

            hold_group = hold.get("group")
            hold_comment = hold.get("comment", "")
            hold_alternate_id = hold.get("alternate_id")
            hold_date_applied = hold.get("date_applied")
            hold_date_to_remove = hold.get("date_to_remove")

            response = self._otcs.get_node_by_volume_and_path(
                volume_type=self._otcs.VOLUME_TYPE_RECORDS_MANAGEMENT,
                path=["Hold Maintenance"],
            )
            if not response:
                self.logger.error("Cannot find 'Records Management' volume!")
                continue
            holds_maintenance_id = self._otcs.get_result_value(
                response=response,
                key="id",
            )
            if not holds_maintenance_id:
                self.logger.error(
                    "Cannot find 'Holds Maintenance' folder in 'Records Management' volume!",
                )
                continue

            if hold_group:
                # Check if the Hold Group (folder) does already exist.
                response = self._otcs.get_node_by_parent_and_name(
                    parent_id=holds_maintenance_id,
                    name=hold_group,
                )
                parent_id = self._otcs.get_result_value(response=response, key="id")
                if not parent_id:
                    response = self._otcs.create_item(
                        parent_id=holds_maintenance_id,
                        item_type=self._otcs.ITEM_TYPE_HOLD,
                        item_name=hold_group,
                    )
                    parent_id = self._otcs.get_result_value(response=response, key="id")
                    if not parent_id:
                        self.logger.error(
                            "Failed to create hold group -> '%s'!",
                            hold_group,
                        )
                        continue
            else:
                parent_id = holds_maintenance_id

            # Holds are special - they ahve folders that cannot be traversed
            # in the normal way - we need to get the whole list of holds and use
            # specialparameters for the exist_result_items() method as the REST
            # API calls delivers a results->data->holds structure (not properties)
            response = self._otcs.get_records_management_holds()
            if self._otcs.exist_result_item(
                response=response,
                key="HoldName",
                value=hold_name,
                property_name="holds",
            ):
                self.logger.info(
                    "Hold -> '%s' does already exist. Skipping...",
                    hold_name,
                )
                continue

            hold = self._otcs.create_records_management_hold(
                hold_type=hold_type,
                name=hold_name,
                comment=hold_comment,
                alternate_id=hold_alternate_id,
                parent_id=int(parent_id),
                date_applied=hold_date_applied,
                date_to_remove=hold_date_to_remove,
            )

            if hold and hold["holdID"]:
                self.logger.info(
                    "Successfully created hold -> '%s' with ID -> %s.",
                    hold_name,
                    hold["holdID"],
                )
            else:
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._holds,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_additional_group_members")
    def process_additional_group_members(
        self,
        section_name: str = "additionalGroupMemberships",
    ) -> bool:
        """Process additional groups memberships we want to have in OTDS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._additional_group_members:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for additional_group_member in self._additional_group_members:
            parent_group = additional_group_member.get("parent_group")
            if not parent_group:
                self.logger.error("Missing parent group! Skipping...")
                continue

            # Check if additional group member has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not additional_group_member.get("enabled", True):
                self.logger.info(
                    "Payload for additional group member with parent group -> '%s' is disabled. Skipping...",
                    parent_group,
                )
                continue

            if ("user_name" not in additional_group_member) and ("group_name" not in additional_group_member):
                self.logger.error(
                    "Either group name or user name need to be specified! Skipping additional group members...",
                )
                success = False
                continue
            if "group_name" in additional_group_member:
                group_name = additional_group_member["group_name"]
                self.logger.info(
                    "Adding group -> '%s' to parent group -> '%s' in OTDS.",
                    group_name,
                    parent_group,
                )
                response = self._otds.add_group_to_parent_group(
                    group=group_name,
                    parent_group=parent_group,
                )
                if not response:
                    self.logger.error(
                        "Failed to add group -> '%s' to parent group -> '%s' in OTDS!",
                        group_name,
                        parent_group,
                    )
                    success = False
            elif "user_name" in additional_group_member:
                user_name = additional_group_member["user_name"]
                self.logger.info(
                    "Adding user -> '%s' to group -> '%s' in OTDS.",
                    user_name,
                    parent_group,
                )
                response = self._otds.add_user_to_group(
                    user=user_name,
                    group=parent_group,
                )
                if not response:
                    self.logger.error(
                        "Failed to add user -> '%s' to group -> '%s' in OTDS!",
                        user_name,
                        parent_group,
                    )
                    success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._additional_group_members,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(
        attributes=OTEL_TRACING_ATTRIBUTES, name="process_additional_application_role_assignments"
    )
    def process_additional_application_role_assignments(
        self,
        section_name: str = "additionalApplicationRoleAssignments",
    ) -> bool:
        """Process additional application role assignments we want to have in OTDS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._additional_application_role_assignments:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for additional_role_assignment in self._additional_application_role_assignments:
            role = additional_role_assignment.get("role_name")
            role_parts = role.split("@", 1)
            role_name = role_parts[0]
            role_partition = role_parts[1] if len(role_parts) > 1 else "OAuthClients"

            if not role_name:
                self.logger.error(
                    "Missing application role! Skipping additional role members...",
                )
                continue

            # Check if additional access role member has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not additional_role_assignment.get("enabled", True):
                self.logger.info(
                    "Payload for additional assignment for application role -> '%s' is disabled. Skipping...",
                    role_name,
                )
                continue

            if ("user_name" not in additional_role_assignment) and ("group_name" not in additional_role_assignment):
                self.logger.error(
                    "Either group_name or user_name need to be specified for application role assignment! Skipping...",
                )
                success = False
                continue

            if "group_name" in additional_role_assignment:
                group = additional_role_assignment["group_name"]
                group_parts = group.split("@", 1)
                group_name = group_parts[0]
                group_partition = group_parts[1] if len(group_parts) > 1 else self._otcs.config()["partition"]

                self.logger.info(
                    "Adding group -> '%s' in partition -> '%s' to application role -> '%s' in partition -> '%s'.",
                    group_name,
                    group_partition,
                    role_name,
                    role_partition,
                )

                response = self._otds.assign_group_to_application_role(
                    group_id=group_name,
                    group_partition=group_partition,
                    role_name=role_name,
                    role_partition=role_partition,
                )

                if not response:
                    self.logger.error(
                        "Failed to assign group -> '%s' in partition -> '%s' to application role -> '%s' in partition -> '%s'!",
                        group_name,
                        group_partition,
                        role_name,
                        role_partition,
                    )
                    success = False

            elif "user_name" in additional_role_assignment:
                user = additional_role_assignment["user_name"]
                user_parts = user.split("@", 1)
                user_name = user_parts[0]
                user_partition = user_parts[1] if len(user_parts) > 1 else self._otcs.config()["partition"]

                self.logger.info(
                    "Adding user -> '%s' in partition -> '%s' to application role -> '%s' in partition -> '%s'!",
                    user_name,
                    user_partition,
                    role_name,
                    role_partition,
                )

                response = self._otds.assign_user_to_application_role(
                    user_id=user_name,
                    user_partition=user_partition,
                    role_name=role_name,
                    role_partition=role_partition,
                )
                if not response:
                    self.logger.error(
                        "Failed to assign user -> '%s' in partition -> '%s' to application role -> '%s' in partition -> '%s'!",
                        user_name,
                        user_partition,
                        role_name,
                        role_partition,
                    )
                    success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._additional_application_role_assignments,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_additional_access_role_members")
    def process_additional_access_role_members(
        self,
        section_name: str = "additionalAccessRoleMemberships",
    ) -> bool:
        """Process additional access role memberships we want to have in OTDS.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._additional_access_role_members:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for additional_access_role_member in self._additional_access_role_members:
            access_role = additional_access_role_member.get("access_role")
            if not access_role:
                self.logger.error(
                    "Missing access role! Skipping additional role members...",
                )
                continue

            # Check if additional access role member has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not additional_access_role_member.get("enabled", True):
                self.logger.info(
                    "Payload for additional member for access role -> '%s' is disabled. Skipping...",
                    access_role,
                )
                continue

            if (
                ("user_name" not in additional_access_role_member)
                and ("group_name" not in additional_access_role_member)
                and ("partition_name" not in additional_access_role_member)
            ):
                self.logger.error(
                    "Either group_name or user_name need to be specified! Skipping...",
                )
                success = False
                continue
            if "group_name" in additional_access_role_member:
                group_name = additional_access_role_member["group_name"]
                self.logger.info(
                    "Adding group -> '%s' to access role -> '%s' in OTDS.",
                    group_name,
                    access_role,
                )
                response = self._otds.add_group_to_access_role(
                    access_role=access_role,
                    group=group_name,
                )
                if not response:
                    self.logger.error(
                        "Failed to add group -> '%s' to access role -> '%s' in OTDS!",
                        group_name,
                        access_role,
                    )
                    success = False
            elif "user_name" in additional_access_role_member:
                user_name = additional_access_role_member["user_name"]
                self.logger.info(
                    "Adding user -> '%s' to access role -> '%s' in OTDS.",
                    user_name,
                    access_role,
                )
                response = self._otds.add_user_to_access_role(
                    access_role=access_role,
                    user_id=user_name,
                )
                if not response:
                    self.logger.error(
                        "Failed to add user -> '%s' to access role -> '%s' in OTDS!",
                        user_name,
                        access_role,
                    )
                    success = False
            elif "partition_name" in additional_access_role_member:
                partition_name = additional_access_role_member["partition_name"]
                self.logger.info(
                    "Adding partition -> '%s' to access role -> '%s' in OTDS.",
                    partition_name,
                    access_role,
                )
                response = self._otds.add_partition_to_access_role(
                    access_role=access_role,
                    partition=partition_name,
                )
                if not response:
                    self.logger.error(
                        "Failed to add partition -> '%s' to access role -> '%s' in OTDS!",
                        partition_name,
                        access_role,
                    )
                    success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._additional_access_role_members,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_renamings")
    def process_renamings(self, section_name: str = "renamings") -> bool:
        """Process renamings specified in payload and rename existing Content Server items.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._renamings:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for renaming in self._renamings:
            if "name" not in renaming:
                self.logger.error("Renamings require the new name!")
                continue
            if "nodeid" in renaming:
                node_id = renaming["nodeid"]
            elif "volume" in renaming:
                path = renaming.get("path")
                volume = renaming.get("volume")
                if path:
                    self.logger.info(
                        "Found path -> '%s' in renaming payload. Determine node ID by volume and path...",
                        path,
                    )
                    node = self._otcs.get_node_by_volume_and_path(
                        volume_type=volume,
                        path=path,
                    )
                else:
                    # Determine object ID of volume:
                    node = self._otcs.get_volume(volume_type=volume)
                node_id = self._otcs.get_result_value(response=node, key="id")
            elif "nickname" in renaming:
                nickname = renaming["nickname"]
                self.logger.info(
                    "Found nickname -> '%s' in renaming payload. Determine node ID by nickname...",
                    nickname,
                )
                node = self._otcs.get_node_from_nickname(nickname=nickname)
                node_id = self._otcs.get_result_value(response=node, key="id")
            else:
                self.logger.error(
                    "Renamings require either a node ID or a volume (with an optional path) or a nickname! Skipping to next renaming...",
                )
                continue

            # Check if renaming has been explicitly disabled in payload
            # (enabled = false). In this case we skip this payload element:
            if not renaming.get("enabled", True):
                self.logger.info("Payload for renaming is disabled. Skipping...")
                continue

            response = self._otcs.rename_node(
                node_id=int(node_id),
                name=renaming["name"],
                description=renaming.get("description", ""),
            )
            if not response:
                self.logger.error(
                    "Failed to rename node ID -> '%s' to new name -> '%s'!",
                    node_id,
                    renaming["name"],
                )
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._renamings,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_items")
    def process_items(self, items: list, section_name: str = "items") -> bool:
        """Process items specified in payload and create them in Content Server.

        Args:
            items (list):
                List of items to create (need this as parameter as we
                have multiple lists).
                Each list item in the payload is a dict with this structure:
                {
                    enabled = "..."
                    name = "..."
                    description = "..."
                    nickname = "..."
                    parent_nickname = "..."
                    parent_path = [...]
                    parent_volume = ...
                    original_nickname = "..."
                    original_path = []
                    type = ...
                    url = "..."
                    details = {
                        "scheduledbotdetails" : ...
                    } # additional parameters
                }
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not items:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )

            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for item in items:
            item_name = item.get("name")
            if not item_name:
                self.logger.error("Item needs a name. Skipping...")
                continue

            # Check if element has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not item.get("enabled", True):
                self.logger.info(
                    "Payload for item -> '%s' is disabled. Skipping...",
                    item_name,
                )
                continue

            item_description = item.get("description", "")
            item_nickname = item.get("nickname", None)
            parent_nickname = item.get("parent_nickname", None)
            parent_path = item.get("parent_path", None)

            if parent_nickname:  # parent nickname has preference over parent path
                parent_node = self._otcs.get_node_from_nickname(
                    nickname=parent_nickname,
                )
                parent_id = self._otcs.get_result_value(response=parent_node, key="id")
                if not parent_id:
                    self.logger.error(
                        "Item -> '%s' has a parent nickname -> '%s' that does not exist. Skipping...",
                        item_name,
                        parent_nickname,
                    )
                    success = False
                    continue
            elif parent_path is not None:  # parent_path can be [] which is valid for top-level items!
                parent_volume = item.get("parent_volume", self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE)
                parent_node = self._otcs.get_node_by_volume_and_path(
                    volume_type=parent_volume,
                    path=parent_path,
                    create_path=True,
                )
                parent_id = self._otcs.get_result_value(response=parent_node, key="id")
                if not parent_id:
                    # if not parent_node:
                    self.logger.error(
                        "Item -> '%s' has a parent path -> %s that does not exist and couldn't be created in volume -> %d. Skipping...",
                        item_name,
                        parent_path,
                        self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE,
                    )
                    success = False
                    continue
            else:
                self.logger.error("The parent for the item -> '%s' is not specified by nickname nor path!", item_name)
                success = False
                continue

            # Handling for shortcut items that have an orginal node:
            original_nickname = item.get("original_nickname")
            original_path = item.get("original_path")

            if original_nickname:
                original_node = self._otcs.get_node_from_nickname(
                    nickname=original_nickname,
                )
                original_id = self._otcs.get_result_value(
                    response=original_node,
                    key="id",
                )
                if not original_id:
                    # if not original_node:
                    self.logger.error(
                        "Item -> '%s' has a original nickname -> '%s' that does not exist. Skipping...",
                        item_name,
                        original_nickname,
                    )
                    success = False
                    continue
            elif original_path is not None:  # original_path can be [] which is valid for top-level items!
                original_volume = item.get("original_volume", self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE)
                original_node = self._otcs.get_node_by_volume_and_path(
                    volume_type=original_volume,
                    path=original_path,
                )
                original_id = self._otcs.get_result_value(
                    response=original_node,
                    key="id",
                )
                if not original_id:
                    # if not original_node:
                    self.logger.error(
                        "Item -> '%s' has a original path that does not exist. Skipping...",
                        item_name,
                    )
                    success = False
                    continue
            else:
                original_id = 0

            if "type" not in item:
                self.logger.error("Item -> '%s' needs a type. Skipping...", item_name)
                success = False
                continue

            item_type = int(item.get("type", self._otcs.ITEM_TYPE_FOLDER))
            item_url = item.get("url", "")
            item_details = item.get("details", {})
            create_item_details = item.get("create_details", {})
            # check that we have the required information
            # for the given item type:
            match item_type:
                case self._otcs.ITEM_TYPE_URL:  # URL
                    if item_url == "":
                        self.logger.error(
                            "Item -> '%s' has type URL but the URL is not in the payload. Skipping...",
                            item_name,
                        )
                        success = False
                        continue
                case self._otcs.ITEM_TYPE_SHORTCUT:  # Shortcut
                    if not original_id:
                        self.logger.error(
                            "Item -> '%s' has type Shortcut but the original item is not in the payload. Skipping...",
                            item_name,
                        )
                        success = False
                        continue
                case self._otcs.ITEM_TYPE_COLLECTION:  # Collection
                    item_ids = item.get("ids", None)
                    if item_ids is None:
                        self.logger.error(
                            "Item -> '%s' has type Collection but the list of collected items is not provided in payload. Skipping...",
                            item_name,
                        )
                        success = False
                        continue
                case self._otcs.ITEM_TYPE_SCHEDULED_BOT:  # Scheduled Bots
                    if any(
                        key not in item_details for key in ["xecmpfJobType", "scheduledbotdetails"]
                    ):  #  not in item_details:
                        self.logger.error(
                            "Item -> '%s' has type Scheduled Bot but the mandatory details are not provided in payload (xecmpfJobType, scheduledbotdetails). Skipping...",
                            item_name,
                        )
                        success = False
                        continue

                    create_item_details["xecmpfJobType"] = item_details["xecmpfJobType"]

            # Check if an item with the same name does already exist.
            # This can also be the case if the python container runs a 2nd time.
            # For this reason we are also not issuing an error but just an info (False):
            response = self._otcs.get_node_by_parent_and_name(
                parent_id=int(parent_id),
                name=item_name,
                show_error=False,
            )
            if self._otcs.get_result_value(response=response, key="name") == item_name:
                self.logger.info(
                    "Item with name -> '%s' does already exist in parent folder with ID -> %s.",
                    item_name,
                    parent_id,
                )
                continue
            response = self._otcs.create_item(
                parent_id=int(parent_id),
                item_type=item_type,
                item_name=item_name,
                item_description=item_description,
                url=item_url,
                original_id=int(original_id),
                **create_item_details,
            )
            node_id = self._otcs.get_result_value(response=response, key="id")
            if not node_id:
                self.logger.error(
                    "Failed to create item -> '%s' under parent%s!",
                    item_name,
                    " with nickname -> '{}'".format(parent_nickname)
                    if parent_nickname
                    else " path -> {} in volume -> {}".format(parent_path, parent_volume),
                )
                success = False
                continue

            self.logger.info(
                "Successfully created item -> '%s' with ID -> %s under parent%s.",
                item_name,
                node_id,
                " with nickname -> '{}'".format(parent_nickname)
                if parent_nickname
                else " path -> {} in volume -> {}".format(parent_path, parent_volume),
            )

            # Special handling for scheduled bot items:
            if item_type == self._otcs.ITEM_TYPE_SCHEDULED_BOT:
                scheduled_bot_details = item_details.get("scheduledbotdetails", {})
                if not scheduled_bot_details:
                    self.logger.error("Failed to get details of scheduled bot item -> '%s'!", item_name)
                    success = False
                    continue
                start_mode = scheduled_bot_details.get("startmodus")
                if not start_mode:
                    self.logger.error("Failed to get start mode of scheduled bot item -> '%s'!", item_name)
                    success = False
                    continue
                start_mode = start_mode.get("startMode")
                if not start_mode:
                    self.logger.error("Failed to get start mode of scheduled bot item -> '%s'!", item_name)
                    success = False
                    continue
                old_schedule_data = scheduled_bot_details.get("oldscheduleData")
                # Check if this bot should start after another bot:
                if start_mode == "AfterJob":
                    after_job_nickname = scheduled_bot_details["startmodus"].get("afterJob")
                    if not after_job_nickname:
                        after_job_nickname = item_details.get("xecmpfAfterJobDataId")
                    self.logger.info(
                        "Scheduled bot item -> '%s' starts after another scheduled bot with nickname -> '%s'. Resolving nickname...",
                        item_name,
                        after_job_nickname,
                    )
                    # Get the Scheduled Bot node that this bot depends on:
                    response = self._otcs.get_node_from_nickname(nickname=after_job_nickname)
                    after_job_id = self._otcs.get_result_value(
                        response=response,
                        key="id",
                    )
                    # Update the bot details - changing the name coming from the
                    # payload to the actual node ID (both in details and in 'old' details):
                    scheduled_bot_details["startmodus"]["afterJob"] = after_job_id
                    old_schedule_data["afterJob"] = after_job_id

                    item_details["xecmpfAfterJobDataId"] = after_job_id
                # Check if this bot should start based on a schedule.
                # Then we need to configure the agent to run it:
                elif str(start_mode) == "7558":  # 7558 is NOT a object ID but an internal agent type ID!
                    self.logger.info(
                        "Scheduled bot item -> '%s' starts based on a schedule. Setting the agent ID to '7558'...",
                        item_name,
                    )
                    # Make sure the agent ID is configured:
                    item_details["xecmpfAgentId"] = 7558
                self.logger.debug("Scheduled bot details -> %s", str(scheduled_bot_details))

                # The REST API requires to have the scheduled bot details wrapped into a JSON structure:
                item_details["scheduledbotdetails"] = json.dumps(item_details.get("scheduledbotdetails", {}))

                response = self._otcs.update_item(node_id=node_id, body=False, **item_details)
                if not response:
                    self.logger.error("Failed to update scheduled bot item -> '%s'!", item_name)
                    success = False
                    continue

                # Check if we want to execute an action immediately after creation, like "Runnow":
                actions = item_details.get("actions", [])
                for action in actions:
                    self.logger.info("Execute action -> '%s' for scheduled bot -> '%s'...", action, item_name)
                    response = self._otcs.update_item(node_id=node_id, body=False, actionName=action)
                    if not response:
                        self.logger.error(
                            "Failed to execute action -> '%s' for scheduled bot item -> '%s'!", action, item_name
                        )
                        success = False
                        continue
                if not actions:
                    self.logger.info("No immediate actions specified for scheduled bot -> '%s'.", item_name)

            # end if item_type == self._otcs.ITEM_TYPE_SCHEDULED_BOT:

            # Special handling for collection items:
            elif item_type == self._otcs.ITEM_TYPE_COLLECTION:
                item_node_ids = []
                for collection_item in item_ids or []:
                    # First see if the collection item is a workspace we can
                    # lookup in the payload by its logical payload ID
                    member_item = next(
                        (item for item in self._workspaces if item["id"] == collection_item),
                        None,
                    )
                    if member_item:
                        if member_item.get("enabled", True):
                            self.logger.info(
                                "Found collection item (workspace) -> '%s' in payload and it is enabled.",
                                member_item["name"],
                            )
                        else:
                            self.logger.info(
                                "Found collection item (workspace) -> '%s' in payload but it is not enabled. Skipping...",
                                member_item["name"],
                            )
                            continue
                        member_id = self.determine_workspace_id(workspace=member_item)
                        if not member_id:
                            self.logger.warning(
                                "Workspace of type -> '%s' and name -> '%s' does not exist. Cannot create collection item. Skipping...",
                                member_item["type_name"],
                                member_item["name"],
                            )
                            continue
                    else:
                        # alternatively try to find the item as a nickname:
                        member_item = self._otcs.get_node_from_nickname(
                            nickname=collection_item,
                        )
                        member_id = self._otcs.get_result_value(
                            response=member_item,
                            key="id",
                        )
                        if not member_id:
                            self.logger.warning(
                                "Item -> '%s' does not exist. Cannot create collection item. Skipping...",
                                member_item["name"],
                            )
                            continue
                    item_node_ids.append(member_id)
                # end for collection_item in item_ids
                if item_node_ids:
                    response = self._otcs.add_node_to_collection(
                        collection_id=node_id,
                        node_ids=item_node_ids,
                    )
            # end if item_type == self._otcs.ITEM_TYPE_COLLECTION

            # Do we have a nickname for the item in the payload? Then assign it:
            if item_nickname:
                self.logger.info("Assign nickname -> '%s' to item -> '%s' (%s)...", item_nickname, item_name, node_id)
                self._otcs.set_node_nickname(node_id=node_id, nickname=item_nickname)
        # end for item in items:

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=items,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_permission")
    def process_permission(
        self,
        node_id: int,
        node_name: str,
        permission: dict,
        apply_to: int,
        workspace_id: int | None = None,
    ) -> bool:
        """Process a single permission payload item for a given node.

        Args:
            node_id (int):
                The ID of the node.
            node_name (str):
                The name of the node.
            permission (dict):
                The permission payload.
            apply_to (int):
                - 0 = Apply to this item only
                - 1 = Apply to sub-items only
                - 2 = Apply to this item and its sub-items (default)
                - 3 = Apply to this item and its immediate sub-items
            workspace_id (int | None):
                If role permissions should be set we also need the workspace_id.
                Use None if node is not part of a workspace or no role permissions
                should be set.

        Returns:
            bool:
                True = success, False = Error

        """

        # 1. Process Owner Permissions
        if "owner_permissions" in permission:
            owner_permissions = permission["owner_permissions"]
            self.logger.info(
                "Update owner permissions for item -> '%s' (%d) to -> %s.",
                node_name,
                node_id,
                str(owner_permissions),
            )
            response = self._otcs.assign_permission(
                node_id=int(node_id),
                assignee_type="owner",
                assignee=0,
                permissions=owner_permissions,
                apply_to=apply_to,
            )
            if not response:
                self.logger.error(
                    "Failed to update owner permissions for item -> '%s' (%d)!",
                    node_name,
                    node_id,
                )
                return False

        # 2. Process Owner Group Permissions
        if "owner_group_permissions" in permission:
            owner_group_permissions = permission["owner_group_permissions"]
            self.logger.info(
                "Update owner group permissions for item -> '%s' (%d) to -> %s.",
                node_name,
                node_id,
                str(owner_group_permissions),
            )
            response = self._otcs.assign_permission(
                node_id=int(node_id),
                assignee_type="group",
                assignee=0,
                permissions=owner_group_permissions,
                apply_to=apply_to,
            )
            if not response:
                self.logger.error(
                    "Failed to update group permissions for item -> '%s' (%d)!",
                    node_name,
                    node_id,
                )
                return False

        # 3. Process Public Permissions
        if "public_permissions" in permission:
            public_permissions = permission["public_permissions"]
            self.logger.info(
                "Update public permissions for item -> '%s' (%d) to -> %s.",
                node_name,
                node_id,
                str(public_permissions),
            )
            response = self._otcs.assign_permission(
                node_id=int(node_id),
                assignee_type="public",
                assignee=0,
                permissions=public_permissions,
                apply_to=apply_to,
            )
            if not response:
                self.logger.error(
                    "Failed to update public permissions for item -> '%s' (%d)!",
                    node_name,
                    node_id,
                )
                return False
        # end if "public_permissions" in permission

        # 4. Process Assigned User Permissions (if specified and not empty)
        users = permission.get("users", [])
        for user in users:
            if "name" not in user or "permissions" not in user:
                self.logger.error(
                    "Missing user name in user permission specificiation. Cannot set user permissions. Skipping...",
                )
                return False
            user_name = user["name"]
            if "permissions" not in user:
                self.logger.error(
                    "Missing permissions in user -> '%s' permission specificiation. Cannot set user permissions. Skipping...",
                    user_name,
                )
                return False
            user_permissions = user["permissions"]
            response = self._otcs.get_user(name=user_name, show_error=True)
            user_id = self._otcs.get_result_value(response=response, key="id")
            if not user_id:
                self.logger.error(
                    "Cannot find user -> '%s'; cannot set user permissions. Skipping user...",
                    user_name,
                )
                return False
            user["id"] = user_id  # write ID back into payload

            self.logger.info(
                "Update permission of user -> '%s' for item -> '%s' (%d) to -> %s.",
                user_name,
                node_name,
                node_id,
                str(user_permissions),
            )
            response = self._otcs.assign_permission(
                node_id=int(node_id),
                assignee_type="custom",
                assignee=user_id,
                permissions=user_permissions,
                apply_to=apply_to,
            )
            if not response:
                self.logger.error(
                    "Failed to update assigned user permissions for item -> %s!",
                    node_id,
                )
                return False
        # end for user in users

        # 5. Process Assigned Group Permissions (if specified and not empty)
        groups = permission.get("groups", [])
        for group in groups:
            if "name" not in group:
                self.logger.error(
                    "Missing group name in group permission specificiation. Cannot set group permissions. Skipping...",
                )
                return False
            group_name = group["name"]
            if "permissions" not in group:
                self.logger.error(
                    "Missing permissions in group -> '%s' permission specificiation. Cannot set group permissions. Skipping...",
                    group_name,
                )
                return False
            group_permissions = group["permissions"]
            self.logger.info(
                "Update permissions of group -> '%s' for item -> '%s' (%d) to -> %s.",
                group_name,
                node_name,
                node_id,
                str(group_permissions),
            )
            otcs_group = self._otcs.get_group(name=group_name, show_error=True)
            group_id = self._otcs.get_result_value(response=otcs_group, key="id")
            if not group_id:
                self.logger.error(
                    "Cannot find group -> '%s'; cannot set group permissions. Skipping group...",
                    group_name,
                )
                return False
            group["id"] = group_id  # write ID back into payload
            response = self._otcs.assign_permission(
                node_id=int(node_id),
                assignee_type="custom",
                assignee=group_id,
                permissions=group_permissions,
                apply_to=apply_to,
            )
            if not response:
                self.logger.error(
                    "Failed to update assigned group permissions for item -> '%s' (%d)!",
                    node_name,
                    node_id,
                )
                return False
        # end for group in groups

        # 6. Process Workspace Role Permissions (if specified and not empty)
        roles = permission.get("roles", [])
        if roles and not workspace_id:
            self.logger.error(
                "Cannot set workspace roles if no workspace ID is provided!",
            )
            return False
        for role in roles:
            if "name" not in role:
                self.logger.error(
                    "Missing role name in role permission specificiation. Cannot set role permissions. Skipping...",
                )
                return False
            role_name = role["name"]
            if "permissions" not in role:
                self.logger.error(
                    "Missing permissions in role -> '%s' permission specificiation. Cannot set role permissions. Skipping...",
                    group_name,
                )
                return False
            role_permissions = role["permissions"]
            self.logger.info(
                "Update permissions of role -> '%s' for workspace item -> '%s' (%d) in workspace with ID -> %d to -> %s.",
                role_name,
                node_name,
                node_id,
                workspace_id,
                str(role_permissions),
            )
            response = self._otcs.get_workspace_roles(workspace_id=workspace_id)
            role_id = self._otcs.lookup_result_value(
                response=response,
                key="name",
                value=role_name,
                return_key="id",
            )
            if not role_id:
                self.logger.error(
                    "Cannot find role -> '%s'; cannot set role permissions.",
                    role_name,
                )
                return False
            response = self._otcs.assign_permission(
                node_id=int(node_id),
                assignee_type="custom",
                assignee=role_id,
                permissions=role_permissions,
                apply_to=apply_to,
            )
            if not response:
                self.logger.error(
                    "Failed to update role permissions for item -> '%s' (%d)!",
                    node_name,
                    node_id,
                )
                return False
        # end for role in roles

        return True

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_permissions")
    def process_permissions(
        self,
        permissions: list,
        section_name: str = "permissions",
    ) -> bool:
        """Process items specified in payload and upadate permissions.

        Args:
            permissions (list):
                A list of items to apply permissions to.
                Each list item in the payload is a dict with this structure:
                {
                    nodeid = "..."
                    volume = "..."
                    nickname = "..."
                    public_permissions = ["see", "see_content", ...]
                    owner_permissions = []
                    owner_group_permissions = []
                    groups = [
                        {
                            name = "..."
                            permissions = []
                        }
                    ]
                    users = [
                        {
                            name = "..."
                            permissions = []
                        }
                    ]
                    apply_to = 2
                }
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not permissions:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for permission in permissions:
            if "path" not in permission and "volume" not in permission and "nickname" not in permission:
                self.logger.error(
                    "Item to change permission is not specified (needs path, volume, or nickname). Skipping...",
                )
                success = False
                continue

            # Check if permission has been explicitly disabled in payload
            # (enabled = false). In this case we skip this payload element:
            if not permission.get("enabled", True):
                self.logger.info("Payload for Permission is disabled. Skipping...")
                continue

            node_id = 0

            # Check if "volume" is in payload and not empty string
            # we try to get the node ID from the volume type:
            if permission.get("volume"):
                volume_type = permission["volume"]
                self.logger.info(
                    "Found volume type -> '%s' in permission payload. Determine volume ID...",
                    volume_type,
                )
                node = self._otcs.get_volume(volume_type=volume_type)
                node_id = self._otcs.get_result_value(response=node, key="id")
                if not node_id:
                    self.logger.error(
                        "Illegal volume -> '%s' in permission payload. Skipping...",
                        volume_type,
                    )
                    success = False
                    continue
            else:
                # the following path block requires a value for the volume - if it is
                # not specified we take the Enterprise Workspace:
                volume_type = self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE

            # Check if "path" is in payload and not empty list
            # (path can be combined with volume so we need to take volume into account):
            if permission.get("path"):
                path = permission["path"]
                self.logger.info(
                    "Found path -> '%s' in permission payload. Determine node ID...",
                    path,
                )
                node = self._otcs.get_node_by_volume_and_path(
                    volume_type=volume_type,
                    path=path,
                )
                node_id = self._otcs.get_result_value(response=node, key="id")
                if not node_id:
                    self.logger.error("Path -> '%s' does not exist. Skipping...", path)
                    success = False
                    continue

            # Check if "nickname" is in payload and not empty string:
            if permission.get("nickname"):
                nickname = permission["nickname"]
                self.logger.info(
                    "Found nickname -> '%s' in permission payload. Determine node ID...",
                    nickname,
                )
                node = self._otcs.get_node_from_nickname(nickname=nickname)
                node_id = self._otcs.get_result_value(response=node, key="id")
                if not node_id:
                    self.logger.error(
                        "Nickname -> '%s' does not exist. Skipping...",
                        nickname,
                    )
                    success = False
                    continue

            # Now we should have a value for node_id:
            if not node_id:
                self.logger.error("No node ID found! Skipping permission...")
                success = False
                continue

            node_name = self._otcs.get_result_value(response=node, key="name")
            self.logger.info(
                "Found node -> '%s' with ID -> %s to apply permission to.",
                node_name,
                node_id,
            )
            # write node information back into payload
            # for better debugging
            permission["node_name"] = node_name
            permission["node_id"] = node_id

            # Make item + sub-items (2) the default:
            apply_to = permission.get("apply_to", 2)

            # Prcess a single permission payload item:
            if not self.process_permission(
                node_id=node_id,
                node_name=node_name,
                permission=permission,
                apply_to=apply_to,
            ):
                success = False
                continue
        # end for permission in permissions

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=permissions,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_permissions")
    def process_workspace_permissions(
        self,
        section_name: str = "workspacePermissions",
    ) -> bool:
        """Process items specified in payload and upadate workspace permissions.

        Args:
            workspace_permissions (list):
                List of items to apply permissions to.
                Each list item in the payload is a dict with this structure:
                {
                    workspace_type = "..."
                    workspace_folder = "..."
                    regex = True
                    public_permissions = ["see", "see_content", ...]
                    owner_permissions = []
                    owner_group_permissions = []
                    groups = [
                        {
                            name = "..."
                            permissions = []
                        }
                    ]
                    users = [
                        {
                            name = "..."
                            permissions = []
                        }
                    ]
                    roles = [
                        {
                            name = "..."
                            permissions = []
                        }
                    ]
                    apply_to = 2
                }
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections like "permissionsPost").
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise

        """

        if not self._workspace_permissions:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for workspace_permission in self._workspace_permissions:
            workspace_type_name = workspace_permission.get("workspace_type")
            if not workspace_type_name:
                self.logger.error(
                    "Workspaces type to change permissions is not specified. Skipping...",
                )
                success = False
                continue

            # Check if workspace permission has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not workspace_permission.get("enabled", True):
                self.logger.info(
                    "Payload for workspace permission for workspace type -> '%s' is disabled. Skipping...",
                    workspace_type_name,
                )
                continue

            workspace_folder_name = workspace_permission.get("workspace_folder", None)
            regex = workspace_permission.get("regex", False)

            # Make item + sub-items (2) the default:
            apply_to = workspace_permission.get("apply_to", 2)

            # Find the workspace type with the name given in the payload:
            workspace_type_id = next(
                (item["id"] for item in self._workspace_types if item["name"] == workspace_type_name),
                None,
            )
            if workspace_type_id is None:
                self.logger.error(
                    "Workspace type -> '%s' not found. Skipping...",
                    workspace_type_name,
                )
                success = False
                continue

            # The workspace type name is used as a "starts with" search on the
            # workspace type name. This can cause issues, so we prefer to use the type ID
            # if we have it. Otherwise the REST API prefers the name over the type:
            workspace_instances = self._otcs.get_workspace_instances_iterator(
                type_name=workspace_type_name if not workspace_type_id else None,
                type_id=workspace_type_id,
            )
            for workspace_instance in workspace_instances:
                workspace = workspace_instance.get("data").get("properties")
                workspace_id = workspace["id"]
                workspace_name = workspace["name"]
                if workspace_folder_name:
                    if not regex:
                        workspace_folder = self._otcs.get_node_by_parent_and_name(
                            parent_id=workspace_id,
                            name=workspace_folder_name,
                        )
                    else:
                        workspace_folder = self._otcs.lookup_node_by_regex(
                            parent_node_id=workspace_id,
                            regex_list=[workspace_folder_name],
                        )
                    workspace_folder_id = self._otcs.get_result_value(
                        response=workspace_folder,
                        key="id",
                    )
                    if not workspace_folder_id:
                        self.logger.info(
                            "Workspace folder name -> '%s' was not found in workspace -> '%s' (%s). Skipping this workspace...",
                            workspace_folder_name,
                            workspace_name,
                            workspace_id,
                        )
                        continue
                else:
                    # if no folder is specified we apply the permission on the workspace
                    workspace_folder_id = workspace_id
                    workspace_folder_name = workspace_name

                # Process a single workspace permission payload item:
                if not self.process_permission(
                    node_id=workspace_folder_id,
                    node_name=workspace_folder_name,
                    permission=workspace_permission,
                    apply_to=apply_to,
                ):
                    success = False
                    continue
            # end for workspace_instance in workspace_instances:

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._workspace_permissions,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_assignments")
    def process_assignments(self, section_name: str = "assignments") -> bool:
        """Process assignments and assign items (such as workspaces and items with nicknames) to users or groups.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise

        """

        if not self._assignments:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for assignment in self._assignments:
            # Sanity check: we need a subject - it's mandatory:
            subject = assignment.get("subject")
            if not subject:
                self.logger.error("Assignment needs a subject! Skipping assignment...")
                success = False
                continue

            # Check if assignment has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not assignment.get("enabled", True):
                self.logger.info(
                    "Payload for assignment -> '%s' is disabled. Skipping...",
                    subject,
                )
                continue

            # instruction is optional but we give a warning if they are missing:
            instruction = assignment.get("instruction", "")
            if not instruction:
                self.logger.warning(
                    "Assignment -> '%s' should have an instruction!",
                    subject,
                )

            # Sanity check: we either need users or groups (or both):
            if "groups" not in assignment and "users" not in assignment:
                self.logger.error(
                    "Assignment -> '%s' needs groups or users! Skipping assignment...",
                    subject,
                )
                success = False
                continue

            # Check if a workspace is specified for the assignment and check it does exist:
            if assignment.get("workspace"):
                workspace = next(
                    (item for item in self._workspaces if item["id"] == assignment["workspace"]),
                    None,
                )
                if not workspace:
                    self.logger.error(
                        "Assignment -> '%s' has specified a not existing workspace -> %s! Skipping assignment...",
                        subject,
                        assignment["workspace"],
                    )
                    success = False
                    continue
                node_id = self.determine_workspace_id(workspace=workspace)
                if not node_id:
                    self.logger.error(
                        "Assignment -> '%s' has specified a not existing workspace -> %s! Skipping assignment...",
                        subject,
                        assignment["workspace"],
                    )
                    success = False
                    continue
            # If we don't have a workspace then check if a nickname is specified for the assignment:
            elif "nickname" in assignment:
                response = self._otcs.get_node_from_nickname(
                    nickname=assignment["nickname"],
                )
                node_id = self._otcs.get_result_value(response=response, key="id")
                if not node_id:
                    # if response == None:
                    self.logger.error(
                        "Assignment item with nickname -> '%s' not found",
                        assignment["nickname"],
                    )
                    success = False
                    continue
            else:
                self.logger.error(
                    "Assignment -> '%s' needs a workspace or nickname! Skipping assignment...",
                    subject,
                )
                success = False
                continue

            assignees = []

            if "groups" in assignment:
                group_assignees = assignment["groups"]
                for group_assignee in group_assignees:
                    # find the group in the group list
                    group = next(
                        (item for item in self._groups if item["name"] == group_assignee),
                        None,
                    )
                    if not group:
                        self.logger.error(
                            "Assignment group -> '%s' does not exist! Skipping group...",
                            group_assignee,
                        )
                        success = False
                        continue
                    if "id" not in group:
                        self.logger.error(
                            "Assignment group -> '%s' does not have an ID. Skipping group...",
                            group_assignee,
                        )
                        success = False
                        continue
                    group_id = group["id"]
                    # add the group ID to the assignee list:
                    assignees.append(group_id)

            if "users" in assignment:
                user_assignees = assignment["users"]
                for user_assignee in user_assignees:
                    # find the user in the user list
                    user = next(
                        (item for item in self._users if item["name"] == user_assignee),
                        None,
                    )
                    if not user:
                        self.logger.error(
                            "Assignment user -> '%s' does not exist! Skipping user...",
                            user_assignee,
                        )
                        success = False
                        continue
                    if "id" not in user:
                        self.logger.error(
                            "Assignment user -> '%s' does not have an ID. Skipping user...",
                            user_assignee,
                        )
                        success = False
                        continue
                    user_id = user["id"]
                    # add the group ID to the assignee list:
                    assignees.append(user_id)

            if not assignees:
                self.logger.error(
                    "Cannot add assignment -> '%s' for node ID -> %s because no assignee was found.",
                    subject,
                    node_id,
                )
                success = False
                continue

            response = self._otcs.assign_item_to_user_group(
                node_id=int(node_id),
                subject=subject,
                instruction=instruction,
                assignees=assignees,
            )
            if not response:
                self.logger.error(
                    "Failed to add assignment -> '%s' for node ID -> %s with assignees -> %s!",
                    subject,
                    node_id,
                    assignees,
                )
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._assignments,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_licenses")
    def process_user_licenses(
        self,
        resource_name: str,
        license_feature: str,
        license_name: str = "EXTENDED_ECM",
        user_specific_payload_field: str = "licenses",
        section_name: str = "userLicenses",
    ) -> bool:
        """Assign a specific OTDS license feature to all Content Server users.

        This method is used for OTIV and OTCS licenses.

        Args:
            resource_name (str):
                The name of the OTDS resource.
            license_feature (str):
                The license feature to assign to the user (product specific).
            license_name (str):
                The name of the license Key (e.g. "EXTENDED_ECM" or "INTELLIGENT_VIEWING").
            user_specific_payload_field (str, optional):
                The name of the user specific field in payload
                (if empty it will be ignored).
            section_name (str, optional):
                The name of the section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not self._users:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        otds_resource = self._otds.get_resource(resource_name)
        if not otds_resource:
            self.logger.error(
                "OTDS Resource -> '%s' not found. Cannot assign licenses to users.",
                resource_name,
            )
            return False

        user_partition = self._otcs.config()["partition"]
        if not user_partition:
            self.logger.error("OTCS user partition not found in OTDS!")
            return False

        for user in self._users:
            user_name = user.get("name")
            if not user_name:
                self.logger.error(
                    "Missing user name - cannot assign license to user. Skipping user...",
                )
                continue

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            if user_specific_payload_field and user_specific_payload_field in user:
                self.logger.info(
                    "Found specific license feature -> %s for user -> '%s'. Overwriting default license feature -> '%s'.",
                    user[user_specific_payload_field],
                    user_name,
                    license_feature,
                )
                user_license_features = user[user_specific_payload_field]
            else:  # use the default feature from the actual parameter
                user_license_features = [license_feature]

            for user_license_feature in user_license_features:
                if isinstance(user_license_feature, dict):
                    user_license_feature_dict = user_license_feature
                    user_license_feature = user_license_feature_dict.get("feature", license_feature)
                    license_name = user_license_feature_dict.get("name", license_name)
                    if "enabled" in user_license_feature_dict and not user_license_feature_dict["enabled"]:
                        self.logger.info(
                            "Payload for License '%s' -> '%s' is disabled. Skipping...",
                            license_name,
                            license_feature,
                        )
                        continue

                    if "resource" in user_license_feature_dict:
                        try:
                            resource_id = self._otds.get_resource(name=user_license_feature_dict["resource"])[
                                "resourceID"
                            ]
                        except Exception:
                            self.logger.error(
                                "Error retrieving resourceID for -> %s", user_license_feature_dict["resource"]
                            )
                            continue
                            success = False
                    else:
                        resource_id = otds_resource["resourceID"]

                elif isinstance(user_license_feature, str):
                    resource_id = otds_resource["resourceID"]

                else:
                    self.logger.error("Invalid License feature specified -> %s", user_license_feature)
                    continue

                if self._otds.is_user_licensed(
                    user_name=user_name,
                    resource_id=resource_id,
                    license_feature=user_license_feature,
                    license_name=license_name,
                ):
                    self.logger.info(
                        "User -> '%s' is already licensed for -> '%s' (%s). Skipping...",
                        user_name,
                        license_name,
                        user_license_feature,
                    )
                    continue
                assigned_license = self._otds.assign_user_to_license(
                    partition=user_partition,
                    user_id=user_name,  # we want the plain login name here
                    resource_id=resource_id,
                    license_feature=user_license_feature,
                    license_name=license_name,
                )

                if not assigned_license:
                    self.logger.error(
                        "Failed to assign license feature -> '%s' to user -> %s!",
                        user_license_feature,
                        user_name,
                    )
                    success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._users,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_exec_commands")
    def process_exec_commands(self, section_name: str = "execCommands") -> bool:
        """Process Payload items to execute a command.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise

        """

        if not self._exec_commands:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for exec_command in self._exec_commands:
            if "command" not in exec_command or not exec_command.get("command"):
                self.logger.error(
                    "Command is not specified! It needs to be a non-empty list! Skipping to next command...",
                )
                success = False

                continue
            command = exec_command["command"]

            # Check if exec command has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not exec_command.get("enabled", True):
                self.logger.info(
                    "Payload for exec command is disabled. Skipping...",
                )
                continue

            description = exec_command.get("description")
            if not description:
                self.logger.info(
                    "Executing command -> %s...",
                    command,
                )
            else:
                self.logger.info(
                    "Executing command -> %s (%s)...",
                    command,
                    description,
                )

            try:
                output = subprocess.run(command, stdout=subprocess.PIPE, check=False)
                result = output.stdout.decode("utf-8")
            except Exception:
                self.logger.error(
                    "Execution of command -> %s failed!",
                    command,
                )
                success = False

            # we need to differentiate 3 cases here:
            # 1. result = None is returned - this is an error (exception)
            # 2. result is empty string - this is OK
            # 3. result is a non-empty string - this is OK - print it to log
            if result is None:
                self.logger.error(
                    "Execution of command -> %s failed",
                    command,
                )
                success = False
            elif result != "":
                self.logger.info(
                    "Execution of command -> %s returned result -> %s.",
                    command,
                    result,
                )
            else:
                # It is not an error if no result is returned. It depends on the nature of the command
                # if a result is written to stdout or stderr.
                self.logger.info(
                    "Execution of command -> %s did not return a result.",
                    command,
                )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._exec_pod_commands,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_exec_pod_commands")
    def process_exec_pod_commands(self, section_name: str = "execPodCommands") -> bool:
        """Process commands that should be executed in the Kubernetes pods.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise

        """

        self.logger.warning("execPodCommand is deprecated - use kubernetes section with action 'execPodCommands'!")

        if not isinstance(self._k8s, K8s):
            self.logger.error(
                "K8s not setup properly -> Skipping payload section -> '%s'...",
                section_name,
            )
            return False

        if not self._exec_pod_commands:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for exec_pod_command in self._exec_pod_commands:
            pod_name = exec_pod_command.get("pod_name")
            if not pod_name:
                self.logger.error(
                    "To execute a command in a pod the pod name needs to be specified in the payload! Skipping to next pod command...",
                )
                success = False
                continue

            command = exec_pod_command.get("command", [])
            if not command:
                self.logger.error(
                    "Command is not specified for pod -> %s! It needs to be a non-empty list! Skipping to next pod command...",
                    pod_name,
                )
                success = False
                continue

            container = exec_pod_command.get("container", None)
            timeout = int(exec_pod_command.get("timeout", 60))

            # Check if exec pod command has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not exec_pod_command.get("enabled", True):
                self.logger.info(
                    "Payload for exec pod command in pod -> '%s' is disabled. Skipping...",
                    pod_name,
                )
                continue

            if "description" not in exec_pod_command:
                self.logger.info(
                    "Executing command -> %s in pod -> '%s'...",
                    command,
                    pod_name,
                )
            else:
                description = exec_pod_command["description"]
                self.logger.info(
                    "Executing command -> %s in pod -> '%s' (%s)...",
                    command,
                    pod_name,
                    description,
                )

            if "interactive" not in exec_pod_command or exec_pod_command["interactive"] is False:
                result = self._k8s.exec_pod_command(
                    pod_name=pod_name, command=command, container=container, timeout=timeout
                )
            else:
                result = self._k8s.exec_pod_command_interactive(
                    pod_name=pod_name,
                    commands=command,
                    timeout=timeout,
                )

            # we need to differentiate 3 cases here:
            # 1. result = None is returned - this is an error (exception)
            # 2. result is empty string - this is OK
            # 3. result is a non-empty string - this is OK - print it to log
            if result is None:
                self.logger.error(
                    "Execution of command -> %s in pod -> '%s' failed",
                    command,
                    pod_name,
                )
                success = False
            elif result != "":
                self.logger.info(
                    "Execution of command -> %s in pod -> '%s' returned result -> %s.",
                    command,
                    pod_name,
                    result,
                )
            else:
                # It is not an error if no result is returned. It depends on the nature of the command
                # if a result is written to stdout or stderr.
                self.logger.info(
                    "Execution of command -> %s in pod -> '%s' did not return a result.",
                    command,
                    pod_name,
                )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._exec_pod_commands,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_kubernetes")
    def process_kubernetes(self, section_name: str = "kubernetes") -> bool:
        """Process actions that should be executed in the Kubernetess.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not isinstance(self._k8s, K8s):
            self.logger.error(
                "K8s not setup properly -> Skipping payload section -> '%s'...",
                section_name,
            )
            return False

        if not self._kubernetes:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for item in self._kubernetes:
            # Check if element has been disabled in payload (enabled = false).
            # In this case we skip the element:
            if isinstance(item, dict) and not item.get("enabled", True):
                self.logger.info("Skipping disabled item -> %s", item)
                continue

            match item.get("action"):
                case "execPodCommand":
                    pod_name = item.get("pod_name")

                    if not pod_name:
                        self.logger.error(
                            "To execute a command in a pod the pod name needs to be specified in the payload! Skipping to next kubernetes item...",
                        )
                        success = False
                        continue

                    command = item.get("command", [])
                    if not command:
                        self.logger.error(
                            "Command is not specified for pod -> %s! It needs to be a non-empty list! Skipping to next kubernetes item...",
                            pod_name,
                        )
                        success = False
                        continue

                    container = item.get("container", None)
                    timeout = int(item.get("timeout", 60))

                    # Check if exec pod command has been explicitly disabled in payload
                    # (enabled = false). In this case we skip the element:
                    if not item.get("enabled", True):
                        self.logger.info(
                            "Payload for exec pod command in pod -> '%s' is disabled. Skipping...",
                            pod_name,
                        )
                        continue

                    if "description" not in item:
                        self.logger.info(
                            "Executing command -> %s in pod -> '%s'",
                            command,
                            pod_name,
                        )

                    else:
                        description = item["description"]
                        self.logger.info(
                            "Executing command -> %s in pod -> '%s' (%s)...",
                            command,
                            pod_name,
                            description,
                        )

                    if "interactive" not in item or item["interactive"] is False:
                        result = self._k8s.exec_pod_command(
                            pod_name=pod_name, command=command, container=container, timeout=timeout
                        )
                    else:
                        result = self._k8s.exec_pod_command_interactive(
                            pod_name=pod_name,
                            commands=command,
                            timeout=timeout,
                        )

                    # we need to differentiate 3 cases here:
                    # 1. result = None is returned - this is an error (exception)
                    # 2. result is empty string - this is OK
                    # 3. result is a non-empty string - this is OK - print it to log
                    if result is None:
                        self.logger.error(
                            "Execution of command -> %s in pod -> '%s' failed!",
                            command,
                            pod_name,
                        )
                        success = False
                    elif result != "":
                        self.logger.info(
                            "Execution of command -> %s in pod -> '%s' returned result -> %s.",
                            command,
                            pod_name,
                            result,
                        )
                    else:
                        # It is not an error if no result is returned. It depends on the nature of the command
                        # if a result is written to stdout or stderr.
                        self.logger.info(
                            "Execution of command -> %s in pod -> '%s' did not return a result.",
                            command,
                            pod_name,
                        )

                case "restart":
                    k8s_type = item.get("type")
                    name = item.get("name")
                    message = item.get("message")

                    if not name:
                        self.logger.error("Name not specified for kubernetes item -> %s", item)
                    if not k8s_type:
                        self.logger.error("Type not specified for kubernetes item -> %s", item)

                    if message:
                        self.logger.info("%s", message)

                    if k8s_type.lower() == "statefulset":
                        self.logger.info("Restarting statefulset -> %s...", name)
                        restart_result = self._k8s.restart_stateful_set(sts_name=name)

                    elif k8s_type.lower() == "deployment":
                        self.logger.info("Restarting deployment -> %s...", name)
                        restart_result = self._k8s.restart_deployment(deployment_name=name)

                    elif k8s_type.lower() == "pod":
                        self.logger.info("Deleting pod -> %s...", name)
                        restart_result = self._k8s.delete_pod(pod_name=name)

                    if not restart_result:
                        success = False

                case _:
                    self.logger.error("Action not defined for work item -> %s", item)
                    continue

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._kubernetes,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_exec_database_commands")
    def process_exec_database_commands(self, section_name: str = "execDatabaseCommands") -> bool:
        """Process commands that should be executed in the PostgreSQL database.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not self._exec_database_commands:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        if not psycopg_installed:
            self.logger.warning("Python module 'psycopg' not installed. Cannot execute database commands! Skipping...")
            return False

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for database_command_set in self._exec_database_commands:
            # Check if exec pod command has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not database_command_set.get("enabled", True):
                self.logger.info(
                    "Payload for database command set is disabled. Skipping...",
                )
                continue

            db_connection = database_command_set.get("db_connection")
            if not db_connection:
                self.logger.error(
                    "To execute a command in a database the connection information needs to be specified in the payload! Skipping to next database command set...",
                )
                success = False
                continue

            db_name = db_connection.get("db_name", None)
            if not db_name:
                self.logger.error(
                    "Database connection information is missing the database name! Skipping to next database command set...",
                )
                success = False
                continue
            db_hostname = db_connection.get("db_hostname", None)
            if not db_hostname:
                self.logger.error(
                    "Database connection information is missing the database hostname! Skipping to next database command set...",
                )
                success = False
                continue
            db_port = int(db_connection.get("db_port", 5432))
            db_username = db_connection.get("db_username", None)
            if not db_username:
                self.logger.error(
                    "Database connection information is missing the database username! Skipping to next database command set...",
                )
                success = False
                continue
            db_password = db_connection.get("db_password", None)
            if not db_password:
                self.logger.error(
                    "Database connection information is missing the database password! Skipping to next database command set...",
                )
                success = False
                continue

            connect_string = "dbname={} user={} password={} host={} port={}".format(
                db_name, db_username, db_password, db_hostname, db_port
            )

            db_commands = database_command_set.get("db_commands", [])

            # TODO: Add support for sql file

            db_connection = None  # Predefine for safe access in except
            allowed_verbs = {"SELECT", "INSERT", "UPDATE", "CREATE"}

            try:
                # Using a context managers (with ...) for automatic resource management:
                with psycopg.connect(connect_string) as db_connection:
                    self.logger.info(
                        "Connected to database -> '%s' (%s) with user -> '%s'...", db_name, db_hostname, db_username
                    )
                    with db_connection.cursor() as cursor:
                        for db_command in db_commands:
                            cmd = db_command.get("command", None)
                            if not cmd:
                                self.logger.warning(
                                    "Cannot execute database command without SQL statement. Skipping..."
                                )
                                continue
                            params = db_command.get("params", None)
                            if params is not None and isinstance(params, (list, tuple)):
                                self.logger.error(
                                    "Database parameters -> %s must be given as a list or tuple!", str(params)
                                )
                                continue
                            # Get the command verb (like "SELECT", "CREATE")
                            verb = cmd.strip().split()[0].upper()
                            if verb not in allowed_verbs:
                                self.logger.error("Database command -> '%s' is not allowed!", verb)
                                continue
                            if params:
                                self.logger.info(
                                    "Execute database command -> '%s' with parameters -> %s...", cmd, str(params)
                                )
                            else:
                                self.logger.info("Execute database command -> '%s' without parameters...", cmd)
                            cursor.execute(cmd, params)
                            if verb == "SELECT":
                                response = cursor.fetchall()
                                self.logger.debug("Database response -> '%s'", response)
                    db_connection.commit()
            except psycopg.Error as e:
                success = False
                self.logger.error("Database error -> %s", e)
                if db_connection is not None:
                    db_connection.rollback()

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._exec_database_commands,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_document_generators")
    def process_document_generators(
        self,
        section_name: str = "documentGenerators",
    ) -> bool:
        """Generate documents for a defined workspace type based on template.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:,
                True if payload has been processed without errors, False otherwise.

        """

        if not self._doc_generators:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # save admin credentials for later switch back to admin user:
        #        admin_credentials = self._otcs.credentials()
        authenticated_user = "admin"

        for doc_generator in self._doc_generators:
            if "workspace_type" not in doc_generator:
                self.logger.error(
                    "To generate documents for workspaces the workspace type needs to be specified in the payload! Skipping to next document generator...",
                )
                success = False
                continue
            workspace_type = doc_generator["workspace_type"]

            self._log_header_callback(
                text="Process Document Generator for workspace type -> '{}'".format(workspace_type),
                char="-",
            )

            # Check if doc generator has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not doc_generator.get("enabled", True):
                self.logger.info(
                    "Payload for document generator of workspace type -> '%s' is disabled. Skipping...",
                    workspace_type,
                )
                continue

            template_path = doc_generator.get("template_path")
            if not template_path:
                self.logger.error(
                    "To generate documents for workspaces of type -> '%s' the path to the document template needs to be specified in the payload! Skipping to next document generator...",
                    workspace_type,
                )
                success = False
                continue

            template = self._otcs.get_node_by_volume_and_path(
                volume_type=self._otcs.VOLUME_TYPE_CONTENT_SERVER_DOCUMENT_TEMPLATES,
                path=template_path,
            )
            if not template:
                self.logger.error(
                    "Cannot find document template in path -> %s. Skipping to next document generator...",
                    template_path,
                )
                success = False
                continue
            template_id = self._otcs.get_result_value(response=template, key="id")
            template_name = self._otcs.get_result_value(response=template, key="name")

            if "classification_path" not in doc_generator:
                self.logger.error(
                    "To generate documents for workspaces of type -> '%s' the path to the document classification needs to be specified in the payload! Skipping to next document generator...",
                    workspace_type,
                )
                success = False
                continue
            classification_path = doc_generator["classification_path"]
            classification = self._otcs.get_node_by_volume_and_path(
                volume_type=self._otcs.VOLUME_TYPE_CLASSIFICATION_VOLUME,
                path=classification_path,
            )
            if not classification:
                self.logger.error(
                    "Cannot find document classification in path -> %s. Skipping to next document generator...",
                    classification_path,
                )
                success = False
                continue
            classification_id = self._otcs.get_result_value(
                response=classification,
                key="id",
            )

            # "category_name" is optional. But if it is specified
            # then also "attributes" needs to be specified:
            if "category_name" not in doc_generator:
                self.logger.info(
                    "No metadata (category name) specified in the payload for this document generator.",
                )
                category_name = ""
                attributes = {}
                category_data = {}
            else:
                category_name = doc_generator["category_name"]
                if "attributes" not in doc_generator:
                    self.logger.error(
                        "To generate documents for workspaces of type -> '%s' with metadata, the attributes needs to be specified in the payload! Skipping to next document generator...",
                        workspace_type,
                    )
                    success = False
                    continue
                attributes = doc_generator["attributes"]

                # The following method returns two values: the category ID and
                # a dict of the attributes. If the category is not found
                # on the document template it returns -1 for the category ID
                # and an empty dict for the attribute definitions:
                (
                    category_id,
                    attribute_definitions,
                ) = self._otcs.get_node_category_definition(
                    node_id=template_id,
                    category_name=category_name,
                )
                if category_id == -1:
                    self.logger.error(
                        "The document template -> '%s' does not have the specified category -> %s. Skipping to next document generator...",
                        template_name,
                        category_name,
                    )
                    success = False
                    continue

                category_data = {str(category_id): {}}

                # now we fill the prepared (but empty) category_data
                # with the actual attribute values from the payload:
                for attribute in attributes:
                    attribute_name = attribute["name"]
                    attribute_value = attribute["value"]
                    attribute_type = attribute_definitions[attribute_name]["type"]
                    attribute_id = attribute_definitions[attribute_name]["id"]

                    # Special treatment for type user: determine the ID for the login name.
                    # the ID is the actual value we have to put in the attribute:
                    if attribute_type == "user":
                        user = self._otcs.get_user(
                            name=attribute_value,
                            show_error=True,
                        )
                        user_id = self._otcs.get_result_value(response=user, key="id")
                        if not user_id:
                            self.logger.error(
                                "Cannot find user with login name -> '%s'. Skipping...",
                                attribute_value,
                            )
                            success = False
                            continue
                        attribute_value = user_id
                    category_data[str(category_id)][attribute_id] = attribute_value

            if "workspace_folder_path" not in doc_generator:
                self.logger.info(
                    "No workspace folder path defined for workspaces of type -> '%s'. Documents will be stored in workspace root.",
                    workspace_type,
                )
                workspace_folder_path = []
            else:
                workspace_folder_path = doc_generator["workspace_folder_path"]

            if "exec_as_user" in doc_generator:
                exec_as_user = doc_generator["exec_as_user"]

                # Find the user in the users payload:
                exec_user = next(
                    (item for item in self._users if item["name"] == exec_as_user),
                    None,
                )
                # Have we found the user in the payload?
                if exec_user is not None:
                    self.logger.info(
                        "Executing document generator with user -> '%s'...",
                        exec_as_user,
                    )
                    # Impersonate as the user specified in the payload:
                    self.logger.info("Impersonate user -> '%s'...", exec_as_user)
                    result = self.start_impersonation(username=exec_as_user)
                    if not result:
                        self.logger.error(
                            "Couldn't impersonate user -> '%s'!",
                            exec_as_user,
                        )
                        continue
                    admin_context = False
                    authenticated_user = exec_as_user
                else:
                    self.logger.error(
                        "Cannot find user with login name -> '%s' for executing document generator. Executing as admin...",
                        exec_as_user,
                    )
                    admin_context = True
                    success = False
            else:
                admin_context = True
                exec_as_user = "admin"

            if admin_context and authenticated_user != "admin":
                # Impersonate as the admin user:
                self.logger.info(
                    "Impersonate as admin user -> '%s'...",
                    self._otcs.credentials()["username"],
                )
                # Stop the impersonation as a user:
                result = self.stop_impersonation()
                authenticated_user = "admin"

            if category_data:
                self.logger.info(
                    "Generate documents for workspace type -> '%s' based on template -> '%s' with metadata -> %s...",
                    workspace_type,
                    template_name,
                    category_data,
                )
            else:
                self.logger.info(
                    "Generate documents for workspace type -> '%s' based on template -> '%s' without metadata...",
                    workspace_type,
                    template_name,
                )

            # Find the workspace type with the name given in the _workspace_types
            # datastructure that has been generated by process_workspace_types() method before:
            workspace_type_id = next(
                (item["id"] for item in self._workspace_types if item["name"] == workspace_type),
                None,
            )

            # The workspace type name is used as a "starts with" search on the
            # workspace type name. This can cause issues, so we prefer to use the type ID
            # if we have it. Otherwise the REST API prefers the name over the type:
            workspace_instances = self._otcs.get_workspace_instances_iterator(
                type_name=workspace_type if not workspace_type_id else None,
                type_id=workspace_type_id,
            )
            for workspace_instance in workspace_instances:
                workspace = workspace_instance.get("data").get("properties")
                workspace_id = workspace["id"]
                workspace_name = workspace["name"]
                if workspace_folder_path:
                    workspace_folder = self._otcs.get_node_by_workspace_and_path(
                        workspace_id=workspace_id,
                        path=workspace_folder_path,
                    )
                    if workspace_folder:
                        workspace_folder_id = self._otcs.get_result_value(
                            response=workspace_folder,
                            key="id",
                        )
                    else:
                        # If the workspace template is not matching
                        # the path we may have an error here. Then
                        # we fall back to workspace root level.
                        self.logger.warning(
                            "Folder path does not exist in workspace -> '%s'. Using workspace root level instead...",
                            workspace_name,
                        )
                        workspace_folder_id = workspace_id
                else:
                    workspace_folder_id = workspace_id

                document_name = workspace_name + " - " + template_name
                self.logger.info("Generate document -> '%s'...", document_name)

                response = self._otcs.check_node_name(
                    parent_id=int(workspace_folder_id),
                    node_name=document_name,
                )
                if response["results"]:
                    self.logger.warning(
                        "Node -> '%s' does already exist in workspace folder with ID -> %s",
                        document_name,
                        workspace_folder_id,
                    )
                    continue
                response = self._otcs.create_document_from_template(
                    template_id=int(template_id),
                    parent_id=int(workspace_folder_id),
                    classification_id=int(classification_id),
                    category_data=category_data,
                    doc_name=document_name,
                    doc_description="This document has been auto-generated by Terrarium",
                )
                if not response:
                    self.logger.error(
                        "Failed to generate document -> '%s' in workspace -> '%s' (%s) as user -> '%s'!",
                        document_name,
                        workspace_name,
                        workspace_id,
                        exec_as_user,
                    )
                    success = False
                else:
                    self.logger.info(
                        "Successfully generated document -> '%s' in workspace -> '%s' (%s) as user -> '%s'.",
                        document_name,
                        workspace_name,
                        workspace_id,
                        exec_as_user,
                    )

        if authenticated_user != "admin":
            # Impersonate as the admin user:
            self.logger.info(
                "Impersonate as admin user -> '%s'...",
                self._otcs.credentials()["username"],
            )
            result = self.stop_impersonation()
            if not result:
                self.logger.error(
                    "Couldn't impersonate as admin user -> '%s'!",
                    self._otcs.credentials()["username"],
                )
                success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._doc_generators,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workflow_attributes")
    def process_workflow_attributes(
        self,
        attributes: list,
        workflow_attribute_definition: dict,
    ) -> None:
        """Process the attributes in the workflow steps.

        This method adds the IDs for the attribute to the payload dicts.
        The IDs are needed for the workflow REST API calls.

        Args:
            attributes (list):
                The list of attributes (payload) processed in the workflow step.
            workflow_attribute_definition (dict):
                The workflow attribute definition.

        Returns:
            None. The mutable dictionary in the workflow_step is updated with the IDs.

        """

        # now we need to get the technical attribute IDs from
        # the workflow definition and map them
        # with the attribute names from the payload:
        for attribute in attributes:
            attribute_name = attribute["name"]
            attribute_value = attribute["value"]
            attribute_type = attribute.get("type", None)

            # Special treatment for type user: determine the ID for the login name.
            # the ID is the actual value we have to put in the attribute:
            if attribute_type and attribute_type.lower() == "user":
                user = self._otcs.get_user(name=attribute_value, show_error=True)
                user_id = self._otcs.get_result_value(response=user, key="id")
                if not user_id:
                    self.logger.error(
                        "Cannot find user with login name -> '%s'. Skipping...",
                        attribute_value,
                    )
                    continue
                attribute_value = user_id
                attribute["value"] = user_id

            attribute_definition = workflow_attribute_definition.get(attribute_name)
            if not attribute_definition:
                self.logger.error(
                    "Cannot find the attribute -> '%s' in the workflow definition. Skipping...",
                )
                continue
            # Enrich the attribute dictionary with the attribute ID from the workflow definition:
            attribute["id"] = attribute_definition["id"]
            # Enrich the attribute dictionary with the attribute form ID from the workflow definition:
            attribute["form_id"] = attribute_definition["form_id"]

        if attributes:
            self.logger.info(
                "Updated workflow step attributes with IDs -> %s.",
                str(attributes),
            )

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workflow_step")
    def process_workflow_step(
        self,
        workflow_id: int,
        workflow_step: dict,
        workflow_attribute_definition: dict,
        documents: list | None = None,
        process_id: int | None = None,
    ) -> bool:
        """Process a workflow step of a workflow.

        Args:
            workflow_id (int):
                The node ID of the workflow (the workflow map).
            workflow_step (dict):
                Payload dictionary for a single workflow step.
                Keys: "action", "exec_as_user", "attributes"
            workflow_attribute_definition (dict):
                The workflow attribute definition.
            documents (list | None, optional):
                The list of workflow documents (attachments9).
            process_id (int | None, optional):
                The process ID of the workflow.

        Returns:
            bool:
                True, if the workflow step has been processed successfully, False otherwise.

        """

        if "action" not in workflow_step:
            self.logger.error("Missing workflow action in workflow step.")
            return False
        action = workflow_step["action"]

        if "exec_as_user" not in workflow_step:
            self.logger.error("Missing workflow user in workflow step.")
            return False
        exec_as_user = workflow_step["exec_as_user"]

        # Find the user in the users payload:
        exec_user = next(
            (item for item in self._users if item["name"] == exec_as_user),
            None,
        )
        # Have we found the user in the payload?
        if exec_user is None:
            self.logger.error(
                "Cannot find user with login name -> '%s' for workflow processing.",
                exec_as_user,
            )
            return False

        self.logger.info("Executing workflow step as user -> '%s'...", exec_as_user)

        # Impersonate as the user:
        self.logger.info("Impersonate user -> '%s'...", exec_as_user)
        # True = force new login with new user
        result = self.start_impersonation(username=exec_as_user)
        if not result:
            self.logger.error("Couldn't impersonate user -> '%s'", exec_as_user)
            return False

        # "attributes" is optional:
        if "attributes" not in workflow_step:
            self.logger.warning(
                "No workflow attributes specified in the payload for this workflow step.",
            )
            attributes = []
            workflow_step_values = None
        else:
            attributes = workflow_step["attributes"]
            self.logger.info(
                "Workflow step has attributes -> %s. Adding attribute IDs to the payload names...",
                str(attributes),
            )
            # Update / enrich the attributes in the workflow step with the IDs
            # from the workflow definition (this CHANGES the attributes!)
            self.process_workflow_attributes(
                attributes=attributes,
                workflow_attribute_definition=workflow_attribute_definition,
            )
            # Prepare the data for the REST call to
            # update the process:
            workflow_step_values = {
                attr["form_id"]: attr["value"] for attr in attributes if "form_id" in attr and "value" in attr
            }

        if action == "Initiate":
            # Create a draft process in preparation for the workflow initiation:
            response = self._otcs.create_draft_process(
                workflow_id=workflow_id,
                documents=documents,
                attach_documents=True,
            )
            draftprocess_id = self._otcs.get_result_value(
                response=response,
                key="draftprocess_id",
                property_name="",
            )
            if not draftprocess_id:
                self.logger.error(
                    "Failed to create draft process for workflow ID -> %s as user -> '%s'!",
                    str(workflow_id),
                    exec_as_user,
                )
                # Stop the impersonation as a user:
                result = self.stop_impersonation()
                return False
            else:
                self.logger.info(
                    "Successfully generated draft process with ID -> %s%s.",
                    str(draftprocess_id),
                    " attching document IDs -> " + str(documents) if documents else "",
                )
            workflow_step["draftprocess_id"] = draftprocess_id

            # Check if a due date is specified. The payload has
            # a relative offset in number of days that we add to
            # the current date:
            due_in_days = workflow_step.get("due_in_days")
            if due_in_days:
                due_date = datetime.now(UTC) + timedelta(days=int(due_in_days))
                due_date = due_date.strftime("%Y-%m-%d")
            else:
                due_date = None
            # Record the due date in the workflow step dictionary
            workflow_step["due_date"] = due_date

            # Update the draft process with title, due date
            # and workflow attribute values from the payload:
            response = self._otcs.update_draft_process(
                draftprocess_id=draftprocess_id,
                title=workflow_step.get("title"),
                due_date=due_date,
                values=workflow_step_values,
            )

            # Initiate the draft process. This creates
            # the running workflow instance:
            response = self._otcs.initiate_draft_process(
                draftprocess_id=draftprocess_id,
                comment=workflow_step.get("comment"),
            )
            process_id = self._otcs.get_result_value(
                response=response,
                key="process_id",
                property_name="",
            )
            if not process_id:
                self.logger.error(
                    "Failed to initiate process for workflow with ID -> %s as user -> '%s'!",
                    str(workflow_id),
                    exec_as_user,
                )
                # Stop the impersonation as a user:
                result = self.stop_impersonation()
                return False
            self.logger.info(
                "Successfully initiated process with ID -> %s for workflow with ID -> %s as user -> '%s'.",
                str(process_id),
                str(workflow_id),
                exec_as_user,
            )
            workflow_step["process_id"] = process_id
        # end if action == "Initiate"
        else:
            if not process_id:
                self.logger.error(
                    "Workflow step cannot be executed as process is not initiated (process ID not set)",
                )
                # Stop the impersonation as a user:
                result = self.stop_impersonation()
                return False
            response = self._otcs.get_process_task(
                process_id=process_id,
            )
            # Are there any workflow attributes to update with new values?
            if attributes:
                response = self._otcs.update_process_task(
                    process_id=process_id,
                    values=workflow_step_values,
                )
            # Execute the step action defined in the payload
            response = self._otcs.update_process_task(
                process_id=process_id,
                action=action,
            )

        # Impersonate as the admin user:
        self.logger.info(
            "Impersonate as admin user -> '%s'...",
            self._otcs.credentials()["username"],
        )
        # Stop the impersonation as a user:
        result = self.stop_impersonation()

        return True

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workflows")
    def process_workflows(self, section_name: str = "workflows") -> bool:
        """Initiate and process workflows for a defined workspace type and folder path.

        Args:
            section_name (str, optional):
                The name of the section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not self._workflows:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        # save admin credentials for later switch back to admin user:
        admin_credentials = self._otcs.credentials()

        for workflow in self._workflows:
            workflow_nickname = workflow.get("workflow_nickname")
            if not workflow_nickname:
                self.logger.error(
                    "To initiate and process workflows for documents in workspaces the workflow nickname needs to be specified in the payload! Skipping to next workflow initiation...",
                )
                success = False
                continue
            workflow_node = self._otcs.get_node_from_nickname(
                nickname=workflow_nickname,
            )
            workflow_id = self._otcs.get_result_value(response=workflow_node, key="id")
            workflow_name = self._otcs.get_result_value(
                response=workflow_node,
                key="name",
            )
            if not workflow_id:
                self.logger.error(
                    "Cannot find workflow by nickname -> '%s'! Skipping to next workflow...",
                    workflow_nickname,
                )
                success = False
                continue

            workspace_type = workflow.get("workspace_type")
            if not workspace_type:
                self.logger.error(
                    "To process workflow -> '%s' for documents in workspaces the workspace type needs to be specified in the payload! Skipping to next workflow...",
                    workflow_name,
                )
                success = False
                continue

            # Check if workflow has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not workflow.get("enabled", True):
                self.logger.info(
                    "Payload for workflow -> '%s' of workspace type -> '%s' is disabled. Skipping...",
                    workflow_name,
                    workspace_type,
                )
                continue

            # Find the workspace type with the name given in the _workspace_types
            # datastructure that has been generated by process_workspace_types() method before:
            workspace_type_id = next(
                (item["id"] for item in self._workspace_types if item["name"] == workspace_type),
                None,
            )

            workspace_folder_path = workflow.get("workspace_folder_path", [])
            if not workspace_folder_path:
                self.logger.info(
                    "No workspace folder path defined for workspaces of type -> '%s'. Workflows will be started for documents in workspace root.",
                    workspace_type,
                )

            workflow_steps = workflow.get("steps")
            if not workflow_steps:
                self.logger.error(
                    "To process workflow -> '%s', workflow steps ('steps') needs to be specified in the payload! Skipping to next workflow initiation...",
                    workflow_name,
                )
                success = False
                continue

            # Get the attribute details (name, ID, type) from the workflow definition:
            workflow_attribute_definition = self._otcs.get_workflow_attributes(
                workflow_id=workflow_id,
            )

            # The workspace type name is used as a "starts with" search on the
            # workspace type name. This can cause issues, so we prefer to use the type ID
            # if we have it. Otherwise the REST API prefers the name over the type:
            workspace_instances = self._otcs.get_workspace_instances_iterator(
                type_name=workspace_type if not workspace_type_id else None,
                type_id=workspace_type_id,
            )
            for workspace_instance in workspace_instances:
                workspace = workspace_instance.get("data").get("properties")
                workspace_id = workspace["id"]
                workspace_name = workspace["name"]

                if workspace_folder_path:
                    workspace_folder = self._otcs.get_node_by_workspace_and_path(
                        workspace_id=workspace_id,
                        path=workspace_folder_path,
                    )
                    if workspace_folder:
                        workspace_folder_id = self._otcs.get_result_value(
                            response=workspace_folder,
                            key="id",
                        )
                    else:
                        # If the workspace template is not matching
                        # the path we may have an error here. Then
                        # we fall back to workspace root level.
                        self.logger.warning(
                            "Folder path does not exist in workspace -> '%s'. Using workspace root level instead...",
                            workspace_name,
                        )
                        workspace_folder_id = workspace_id
                else:
                    workspace_folder_id = workspace_id

                # Get all documents (-3 = non-container) from the defined folder:
                response = self._otcs.get_subnodes(
                    parent_node_id=workspace_folder_id,
                    filter_node_types=-3,
                )
                documents = self._otcs.get_result_values(response=response, key="id")

                process_id = None
                for workflow_step in workflow_steps:
                    result = self.process_workflow_step(
                        workflow_id=workflow_id,
                        workflow_step=workflow_step,
                        workflow_attribute_definition=workflow_attribute_definition,
                        documents=documents,
                        process_id=process_id,
                    )
                    # If the step fails we are bailing out as it doesn't make
                    # sense to continue with further steps:
                    if not result:
                        success = False
                        break
                    if "process_id" in workflow_step:
                        process_id = workflow_step["process_id"]
            # end for workspace_instance in workspace_instances:
        # end for workflow in self._workflows

        # Set back admin credentials:
        self._otcs.set_credentials(
            username=admin_credentials["username"],
            password=admin_credentials["password"],
        )
        # Authenticate back as the admin user:
        self.logger.info(
            "Authenticate as admin user -> '%s'...",
            admin_credentials["username"],
        )
        # True = force new login with new user
        self._otcs.authenticate(revalidate=True)

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._workflows,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_browser_automations")
    def process_browser_automations(
        self,
        browser_automations: list,
        section_name: str = "browserAutomations",
        check_status: bool = True,
    ) -> bool:
        """Process Playwright-based browser automations and tests.

        Args:
            browser_automations (list):
                A list of browser automations (need this as parameter as we
                have multiple lists).
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.
            check_status (bool, optional):
                Defines whether or not this needs to re-run
                for each customizer run (even if it has been successful before).
                If check_status is True (default) then it is only re-run
                if it has NOT been successfully before.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not browser_automations:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if check_status and self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        automation_type = "Browser" if "browser" in section_name else "Test"

        for browser_automation in browser_automations:
            name = browser_automation.get("name")
            if not name:
                self.logger.error("%s automation is missing a unique name. Skipping...", automation_type)
                success = False
                continue

            self._log_header_callback(
                text="Process {} Automation -> '{}'".format(automation_type, name),
                char="-",
            )

            description = browser_automation.get("description", "")
            if description:
                self.logger.info(
                    "%s automation description -> '%s'.",
                    automation_type,
                    description,
                )

            # Check if browser automation has been explicitly disabled in payload
            # (enabled = false). In this case we skip this payload element:
            if not browser_automation.get("enabled", True):
                self.logger.info(
                    "Payload for %s automation -> '%s' is disabled. Skipping...",
                    automation_type.lower(),
                    name,
                )
                continue

            base_url = browser_automation.get("base_url")
            if not base_url:
                self.logger.error(
                    "%s automation -> '%s' is missing 'base_url' parameter. Skipping...", automation_type, name
                )
                browser_automation["result"] = "failure"
                success = False
                continue

            user_name = browser_automation.get("user_name", "")
            if not user_name:
                self.logger.info("%s automation -> '%s' is not having user name.", automation_type, name)

            password = browser_automation.get("password", "")
            if not password:
                self.logger.info("%s automation -> '%s' is not having password.", automation_type, name)

            automation_steps = browser_automation.get("automations", [])
            if not automation_steps:
                self.logger.error(
                    "%s automation -> '%s' is missing list of automations. Skipping...",
                    automation_type,
                    name,
                )
                browser_automation["result"] = "failure"
                success = False
                continue

            debug_automation: bool = browser_automation.get("debug", False)

            # Create Selenium Browser Automation:
            self.logger.info("%s automation base URL -> %s.", automation_type, base_url)
            self.logger.info("%s automation user -> '%s'.", automation_type, user_name)
            wait_until = browser_automation.get("wait_until")  # it is OK to be None
            if "wait_until" in browser_automation:
                # Only log the "wait until" value if it is specified in the payload:
                self.logger.info(
                    "%s Automation page navigation strategy is to wait until -> '%s'.",
                    automation_type,
                    wait_until,
                )
            browser_automation_object = BrowserAutomation(
                base_url=base_url,
                user_name=user_name,
                user_password=password,
                automation_name=name,
                take_screenshots=debug_automation,
                headless=browser_automation.get("headless", self._browser_headless),
                logger=self.logger,
                wait_until=wait_until,
                browser=browser_automation.get("browser"),  # None is acceptable
            )
            if not browser_automation_object:
                self.logger.error(
                    "Cannot execute browser automation -> '%s'. Initialization of browser automation object failed!",
                    name,
                )
                browser_automation["result"] = "failure"
                success = False
                continue
            # Wait time is a global setting (for whole browser session)
            # This makes sure a page is fully loaded and elements are present
            # before accessing them. We set 30.0 seconds as default if not
            # otherwise specified by "wait_time" in the payload.
            wait_time = float(browser_automation.get("wait_time", 30.0))
            browser_automation_object.set_timeout(wait_time=wait_time)
            if "wait_time" in browser_automation:
                self.logger.info(
                    "%s automation wait time -> '%s' configured.",
                    automation_type,
                    str(wait_time),
                )

            # Initialize overall result status:
            result = True
            first_step = True

            for automation_step in automation_steps:
                automation_step_type = automation_step.get("type", "")
                if not automation_step_type:
                    self.logger.error(
                        "%s automation step -> %s in browser automation -> '%s' is missing 'type' parameter. Stopping automation -> '%s'.",
                        automation_type,
                        str(automation_step),
                        name,
                        name,
                    )
                    success = False
                    break
                # Check if browser automation step has been explicitly disabled in payload
                # (enabled = false). In this case we skip this automation step:
                if not automation_step.get("enabled", True):
                    self.logger.info(
                        "Automation step -> '%s' in automation -> '%s' is disabled. Skipping...",
                        automation_step_type,
                        name,
                    )
                    continue
                dependent = automation_step.get("dependent", True)
                if not dependent and not result:
                    self.logger.warning(
                        "Ignore result of previous step as current step -> '%s' is NOT dependent on it.",
                        automation_step_type,
                    )
                    result = True
                elif not result:
                    # In this case a proceeding automation step has failed
                    # and this step is marked as dependent. Then it does not make sense
                    # to continue with this automation step after the proceeding step failed.
                    self.logger.warning(
                        "Step -> '%s' is dependent on a proceeding step that failed. Skipping this step...",
                        automation_step_type,
                    )
                    continue
                elif not first_step:
                    self.logger.debug(
                        "Current step -> '%s' is %s on proceeding step.",
                        automation_step_type,
                        "dependent" if dependent else "not dependent",
                    )

                match automation_step_type:
                    case "login":
                        page = automation_step.get("page", "")
                        user_field = automation_step.get("user_field", "otds_username")
                        password_field = automation_step.get(
                            "password_field",
                            "otds_password",
                        )
                        login_button = automation_step.get("login_button", "loginbutton")
                        # Do we have a step-specific wait mechanism? If not, we pass None
                        # then the browser automation will take the default configured for
                        # the whole browser automation (see BrowserAutomation() constructor above):
                        wait_until = automation_step.get("wait_until", None)
                        self.logger.info(
                            "Login to -> %s as user -> '%s' (%s page navigation strategy is to wait until -> '%s')...",
                            base_url + page,
                            user_name,
                            "specific" if wait_until is not None else "default",
                            wait_until if wait_until is not None else browser_automation_object.wait_until,
                        )
                        result = browser_automation_object.run_login(
                            page=page,
                            user_field=user_field,
                            password_field=password_field,
                            login_button=login_button,
                            wait_until=wait_until,
                        )
                        if not result:
                            self.logger.error(
                                "Cannot log into -> %s. Skipping to next automation step...",
                                base_url + page,
                            )
                            automation_step["result"] = "failure"
                            success = False
                            continue
                        self.logger.info(
                            "Successfully logged into page -> %s. Page title -> '%s'.",
                            base_url + page,
                            browser_automation_object.get_title(),
                        )
                    case "get_page":
                        page = automation_step.get("page", "")
                        if not page:
                            self.logger.error(
                                "Automation step type -> '%s' requires 'page' parameter. Stopping automation.",
                                automation_step_type,
                            )
                            automation_step["result"] = "failure"
                            success = False
                            break
                        volume = automation_step.get("volume", OTCS.VOLUME_TYPE_ENTERPRISE_WORKSPACE)
                        path = automation_step.get("path", [])
                        if path and volume:
                            page_node = self._otcs.get_node_by_volume_and_path(
                                volume_type=volume,
                                path=path,
                                create_path=False,
                            )
                            page_id = self._otcs.get_result_value(response=page_node, key="id")
                            if not page_id:
                                # if not parent_node:
                                self.logger.error(
                                    "%s automation -> '%s' has a page path that does not exist. Skipping...",
                                    automation_type,
                                    name,
                                )
                                automation_step["result"] = "failure"
                                success = False
                                continue
                            self.logger.info(
                                "Resolved volume -> %s and page path -> %s to node ID -> %s.",
                                str(volume),
                                str(path),
                                str(page_id),
                            )
                        else:
                            page_id = None
                        if "{}" in page and page_id:
                            page = page.format(page_id)
                        # Do we have a step-specific wait mechanism? If not, we pass None
                        # then the browser automation will take the default configured for
                        # the whole browser automation (see BrowserAutomation() constructor called above):
                        wait_until = automation_step.get("wait_until", None)
                        self.logger.info(
                            "Load page -> %s (%s page navigation strategy is to wait until -> '%s')...",
                            base_url + page,
                            "specific" if wait_until is not None else "default",
                            wait_until if wait_until is not None else browser_automation_object.wait_until,
                        )
                        result = browser_automation_object.get_page(url=page, wait_until=wait_until)
                        if not result:
                            self.logger.error(
                                "Cannot load page -> %s! Skipping this step...",
                                page,
                            )
                            automation_step["result"] = "failure"
                            success = False
                            continue
                        self.logger.info(
                            "Successfully loaded page -> %s. Page title -> '%s'.",
                            base_url + page,
                            browser_automation_object.get_title(),
                        )
                    case "click_elem":
                        # We keep the deprecated "elem" syntax supported (for now)
                        selector = automation_step.get("selector", automation_step.get("elem", ""))
                        if not selector:
                            self.logger.error(
                                "Automation step type -> '%s' requires 'selector' parameter. Stopping automation.",
                                automation_step_type,
                            )
                            automation_step["result"] = "failure"
                            success = False
                            break
                        # We keep the deprecated "find" syntax supported (for now)
                        selector_type = automation_step.get("selector_type", automation_step.get("find", "id"))
                        show_error = automation_step.get("show_error", True)
                        # Do we navigate away from the current page with the click?
                        navigation = automation_step.get("navigation", False)
                        # Do we open a new browser (popup) window with the click?
                        popup_window = automation_step.get("popup_window", False)
                        # De we close the current (popup) window with the click?
                        close_window = automation_step.get("close_window", False)
                        # Do we have a 'desired' state for clicking a checkbox?
                        checkbox_state = automation_step.get("checkbox_state", None)
                        wait_until = automation_step.get("wait_until", None)
                        wait_time = automation_step.get("wait_time", 0.0)
                        role_type = automation_step.get("role_type", None)
                        occurrence = automation_step.get("occurrence", 1)
                        scroll_to_element = automation_step.get("scroll_to_element", True)
                        exact_match = automation_step.get("exact_match", None)
                        regex = automation_step.get("regex", None)
                        hover_only = automation_step.get("hover_only", False)
                        iframe = automation_step.get("iframe", None)
                        force = automation_step.get("force", None)
                        click_button = automation_step.get("click_button", None)
                        click_count = automation_step.get("click_count", None)
                        click_modifiers = automation_step.get("click_modifiers", None)
                        repeat_reload = automation_step.get("repeat_reload", None)
                        repeat_reload_delay = automation_step.get("repeat_reload_delay", None)
                        result = browser_automation_object.find_elem_and_click(
                            selector=selector,
                            selector_type=selector_type,
                            role_type=role_type,
                            occurrence=occurrence,
                            scroll_to_element=scroll_to_element,
                            desired_checkbox_state=checkbox_state,
                            is_navigation_trigger=navigation,
                            is_popup_trigger=popup_window,
                            is_page_close_trigger=close_window,
                            wait_until=wait_until,
                            wait_time=wait_time,
                            exact_match=exact_match,
                            regex=regex,
                            hover_only=hover_only,
                            iframe=iframe,
                            force=force,
                            click_button=click_button,
                            click_count=click_count,
                            click_modifiers=click_modifiers,
                            repeat_reload=repeat_reload,
                            repeat_reload_delay=repeat_reload_delay,
                            show_error=show_error,
                        )
                        if not result:
                            message = "Cannot find clickable element with selector -> '{}' ({}) on current page. Skipping this step...".format(
                                selector, selector_type
                            )
                            if show_error:
                                self.logger.error(message)
                                automation_step["result"] = "failure"
                                success = False
                            else:
                                self.logger.warning(message)
                            continue
                        self.logger.info(
                            "Successfully %s %s element selected by -> '%s' (%s%s).",
                            "clicked" if not hover_only else "hovered over",
                            "navigational" if navigation else "non-navigational",
                            selector,
                            "selector type -> '{}'".format(selector_type),
                            ", role type -> '{}'".format(role_type) if role_type else "",
                        )
                    case "set_elem":
                        # We keep the deprecated "elem" syntax supported (for now)
                        selector = automation_step.get("selector", automation_step.get("elem", ""))
                        if not selector:
                            self.logger.error(
                                "Automation step type -> '%s' requires 'selector' parameter. Stopping automation.",
                                automation_step_type,
                            )
                            automation_step["result"] = "failure"
                            success = False
                            break
                        # We keep the deprecated "find" syntax supported (for now)
                        selector_type = automation_step.get("selector_type", automation_step.get("find", "id"))
                        role_type = automation_step.get("role_type", None)
                        occurrence = automation_step.get("occurrence", 1)
                        exact_match = automation_step.get("exact_match", None)
                        regex = automation_step.get("regex", None)
                        iframe = automation_step.get("iframe", None)
                        value = automation_step.get("value", "")
                        typing = automation_step.get("typing", False)
                        press_enter = automation_step.get("press_enter", False)
                        if not value:
                            self.logger.error(
                                "Automation step type -> '%s' for element selected by -> '%s' (%s) requires 'value' parameter. Stopping automation.",
                                automation_step_type,
                                selector,
                                selector_type,
                            )
                            automation_step["result"] = "failure"
                            success = False
                            break
                        # we also support replacing placeholders that are
                        # enclosed in double % characters like %%OTCS_RESOURCE_ID%%:
                        if isinstance(value, str):
                            value = self.replace_placeholders(value)
                        show_error = automation_step.get("show_error", True)
                        result = browser_automation_object.find_elem_and_set(
                            selector=selector,
                            value=value,
                            selector_type=selector_type,
                            role_type=role_type,
                            occurrence=occurrence,
                            press_enter=press_enter,
                            exact_match=exact_match,
                            regex=regex,
                            iframe=iframe,
                            typing=typing,
                            show_error=show_error,
                        )
                        if not result:
                            message = "Cannot set element{} selected by -> '{}' ({}{}) to value -> '{}'. Skipping this step...".format(
                                " (occurrence -> {})".format(occurrence) if occurrence > 1 else "",
                                selector,
                                "selector type -> '{}'".format(selector_type),
                                ", role type -> '{}'".format(role_type) if role_type else "",
                                value,
                            )
                            if show_error:
                                self.logger.error(message)
                                automation_step["result"] = "failure"
                                success = False
                            else:
                                self.logger.warning(message)
                            continue
                        self.logger.info(
                            "Successfully set element%s selected by -> '%s' (%s%s) to value -> '%s'.",
                            " (occurrence -> {})".format(occurrence) if occurrence > 1 else "",
                            selector,
                            "selector type -> '{}'".format(selector_type),
                            ", role type -> '{}'".format(role_type) if role_type else "",
                            value,
                        )
                    case "check_elem":
                        # We keep the deprecated "elem" syntax supported (for now)
                        selector = automation_step.get("selector", automation_step.get("elem", ""))
                        if not selector:
                            self.logger.error(
                                "Automation step type -> '%s' requires 'selector' parameter. Stopping automation.",
                                automation_step_type,
                            )
                            automation_step["result"] = "failure"
                            success = False
                            break
                        # We keep the deprecated "find" syntax supported (for now)
                        selector_type = automation_step.get("selector_type", automation_step.get("find", "id"))
                        role_type = automation_step.get("role_type", None)
                        value = automation_step.get("value", None)
                        attribute = automation_step.get("attribute", "")
                        substring = automation_step.get("substring", False)
                        iframe = automation_step.get("iframe", None)
                        min_count = automation_step.get("min_count", 1)
                        want_exist = automation_step.get("want_exist", True)
                        wait_time = automation_step.get("wait_time", 0.0)
                        if value:
                            # we also support replacing placeholders that are
                            # enclosed in double % characters like %%OTCS_RESOURCE_ID%%:
                            value = self.replace_placeholders(value)
                        (result, count) = browser_automation_object.check_elems_exist(
                            selector=selector,
                            selector_type=selector_type,
                            role_type=role_type,
                            value=value,
                            attribute=attribute,
                            substring=substring,
                            iframe=iframe,
                            min_count=min_count,
                            wait_time=wait_time,  # time to wait before the check is actually done
                            show_error=not want_exist,  # if element is not found that we do not want to find it is not an error
                        )
                        # Check if we didn't get what we want:
                        if (not result and want_exist) or (result and not want_exist):
                            self.logger.error(
                                "%s %s%s%s on current page. Test failed.%s",
                                "Cannot find" if not result and want_exist else "Found",
                                "{} elements with selector -> '{}' ({}{})".format(
                                    min_count if want_exist else count,
                                    selector,
                                    "selector type -> '{}'".format(selector_type),
                                    ", role type -> '{}'".format(role_type) if role_type else "",
                                )
                                if (min_count > 1 and want_exist) or (count > 1 and not want_exist)
                                else "an element with selector -> '{}' ({}{})".format(
                                    selector,
                                    "selector type -> '{}'".format(selector_type),
                                    ", role type -> '{}'".format(role_type) if role_type else "",
                                ),
                                " with {}value -> '{}'".format("substring-" if substring else "", value)
                                if value
                                else "",
                                " in attribute -> '{}'".format(attribute) if attribute else "",
                                " Found {}{} occurences.".format(
                                    count,
                                    " undesirable" if not want_exist else " from a minimum of {}".format(min_count),
                                ),
                            )
                            success = False
                            continue
                            # Don't break here! We want to do all existance tests!
                        self.logger.info(
                            "Successfully passed %sexistence test for %s%s%s on current page.",
                            "non-" if not want_exist else "",
                            "{} elements with selector -> '{}' ({})".format(min_count, selector, selector_type)
                            if min_count > 1
                            else "an element with selector -> '{}' ({})".format(selector, selector_type),
                            " with {}value -> '{}'".format("substring-" if substring else "", value) if value else "",
                            " in attribute -> '{}'".format(attribute) if attribute else "",
                        )
                    case _:
                        self.logger.error(
                            "Illegal automation step type -> '%s' in %s automation!",
                            automation_step_type,
                            automation_type.lower(),
                        )
                        automation_step["result"] = "failure"
                        success = False
                        break
                # end match automation_step_type:
                first_step = False
            # end for automation_step in automation_steps:

            # Cleanup session and and remove reference to the object:
            browser_automation_object.end_session()
            browser_automation_object = None

            browser_automation["result"] = (
                "failure" if any(step.get("result", "success") == "failure" for step in automation_steps) else "success"
            )
        # end for browser_automation in browser_automations:

        if check_status:
            self.write_status_file(
                success=success,
                payload_section_name=section_name,
                payload_section=browser_automations,
            )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_sap")
    def init_sap(
        self,
        sap_external_system: dict,
        direct_application_server_login: bool = True,
    ) -> SAP | None:
        """Initialize SAP object for RFC communication with SAP S/4HANA.

        Args:
            sap_external_system (dict):
                SAP external system created before
            direct_application_server_login (bool, optional):
                Flag to control whether we comminicate directly with
                SAP application server or via a load balancer

        Returns:
            SAP | None:
                The SAP object or None in case of an error.

        """

        if not sap_external_system:
            return None

        username = sap_external_system["username"]
        password = sap_external_system["password"]
        # "external_system_hostname" is extracted from as_url in process_external_systems()
        host = sap_external_system["external_system_hostname"]
        client = sap_external_system.get("client", "100")
        system_number = sap_external_system.get("external_system_number", "00")
        system_id = sap_external_system["external_system_name"]
        group = sap_external_system.get("group", "PUBLIC")
        destination = sap_external_system.get("destination", "")

        self.logger.info("Connection parameters SAP:")
        self.logger.info("SAP Hostname             = %s", host)
        self.logger.info("SAP Client               = %s", client)
        self.logger.info("SAP System Number        = %s", system_number)
        self.logger.info("SAP System ID            = %s", system_id)
        self.logger.info("SAP User Name            = %s", username)
        if not direct_application_server_login:
            self.logger.info("SAP Group Name (for RFC) = %s", group)
        if destination:
            self.logger.info("SAP Destination          = %s", destination)

        if direct_application_server_login:
            self.logger.info(
                "SAP Login                = Direct Application Server (ashost)",
            )
            sap_object = SAP(
                username=username,
                password=password,
                ashost=host,
                client=client,
                system_number=system_number,
                system_id=system_id,
                destination=destination,
                logger=self.logger,
            )
        else:
            self.logger.info(
                "SAP Login                = Logon with load balancing (mshost)",
            )
            sap_object = SAP(
                username=username,
                password=password,
                mshost=host,
                group=group,
                client=client,
                system_number=system_number,
                system_id=system_id,
                destination=destination,
                logger=self.logger,
            )

        self._sap = sap_object

        if (
            "archive_logical_name" in sap_external_system
            and "archive_certificate_file" in sap_external_system
            and self._otac
        ):
            self.logger.info(
                "Put certificate file -> '%s' for logical archive -> '%s' into Archive Center...",
                sap_external_system["archive_certificate_file"],
                sap_external_system["archive_logical_name"],
            )
            response = self._otac.put_cert(
                sap_external_system["external_system_name"],
                sap_external_system["archive_logical_name"],
                sap_external_system["archive_certificate_file"],
            )
            if not response:
                self.logger.error("Failed to install Archive Center certificate!")
            else:
                self.logger.info(
                    "Enable certificate file -> '%s' for logical archive -> '%s'...",
                    sap_external_system["archive_certificate_file"],
                    sap_external_system["archive_logical_name"],
                )
                response = self._otac.enable_cert(
                    auth_id=sap_external_system["external_system_name"],
                    logical_archive=sap_external_system["archive_logical_name"],
                    enable=True,
                )
                if not response:
                    self.logger.debug("Failed to enable Archive Center certificate!")

        return sap_object

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_sap_rfcs")
    def process_sap_rfcs(self, sap_rfcs: list, section_name: str = "sapRFCs") -> bool:
        """Process SAP RFCs in payload and run them in SAP S/4HANA.

        Args:
            sap_rfcs (list):
                The payload list of SAP RFCs. As we have two different list (pre and post)
                we need to pass the actual list as parameter.
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not self._sap:
            self.logger.info("SAP object is undefined. Cannot call RFCs. Bailing out.")
            return False

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for sap_rfc in sap_rfcs:
            rfc_name = sap_rfc.get("name")
            if not rfc_name:
                self.logger.error("SAP RFC needs a name! Skipping...")
                success = False
                continue

            # Check if SAP RFC has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not sap_rfc.get("enabled", True):
                self.logger.info(
                    "Payload for SAP RFC -> '%s' is disabled. Skipping...",
                    rfc_name,
                )
                continue

            rfc_description = sap_rfc.get("description", "")

            # Check if we have SAP RFC call parameters in the payload. They are optional:
            rfc_params = sap_rfc.get("parameters", {})
            # Check if we have SAP RFC call options in the payload. They are optional:
            rfc_call_options = sap_rfc.get("call_options", {})

            if rfc_params:
                self.logger.info(
                    "Calling SAP RFC -> '%s' (%s) with parameters -> %s%s...",
                    rfc_name,
                    rfc_description,
                    str(rfc_params),
                    " and options -> {}".format(rfc_call_options) if rfc_call_options else "",
                )
            else:
                self.logger.info(
                    "Calling SAP RFC -> '%s' (%s)%s...",
                    rfc_name,
                    rfc_description,
                    " with options -> {}".format(rfc_call_options) if rfc_call_options else "",
                )

            if rfc_call_options:
                self.logger.debug("Using call options -> '%s' ...", rfc_call_options)

            result = self._sap.call(rfc_name, rfc_call_options, rfc_params)
            if result is None:
                self.logger.error("Failed to call SAP RFC -> '%s'!", rfc_name)
                success = False
            elif result.get("RESULT") == "WARNING":
                self.logger.warning(
                    "Result of SAP RFC -> '%s' has a warning -> '%s'.",
                    rfc_name,
                    str(result.get("COMMENT")).strip() if result.get("COMMENT") else str(result),
                )
            elif result.get("RESULT") != "OK":
                self.logger.error(
                    "Result of SAP RFC -> '%s' is not OK, %sresult -> %s",
                    rfc_name,
                    "it returned -> '{}' failed items in".format(result.get("FAILED")) if result.get("FAILED") else "",
                    str(result),
                )
                success = False
            else:
                self.logger.info(
                    "Successfully called RFC -> '%s'. Result -> %s.",
                    rfc_name,
                    str(result),
                )
                # Save result for status file content
                sap_rfc["result"] = result

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=sap_rfcs,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_successfactors")
    def init_successfactors(
        self,
        sucessfactors_external_system: dict,
    ) -> SuccessFactors | None:
        """Initialize SuccessFactors object for workspace creation.

        This is needed synchronize user passwords and emails with SuccessFactors.

        Args:
            sucessfactors_external_system (dict):
                The payload of the SuccessFactors external system created before.

        Returns:
            SuccessFactors: The SuccessFactors object.

        """

        def extract_company_from_url(url: str) -> str:
            parsed_url = urlparse(url)
            query_params = parse_qs(parsed_url.query)
            company_value = query_params.get("company", [""])[0]
            return company_value

        if not sucessfactors_external_system:
            return None

        username = sucessfactors_external_system["username"]
        password = sucessfactors_external_system["password"]
        base_url = sucessfactors_external_system["base_url"]
        as_url = sucessfactors_external_system["as_url"]
        saml_url = sucessfactors_external_system.get("saml_url", "")
        company_id = extract_company_from_url(saml_url)
        client_id = sucessfactors_external_system["oauth_client_id"]
        client_secret = sucessfactors_external_system["oauth_client_secret"]

        self.logger.info("Connection parameters SuccessFactors:")
        self.logger.info("SuccessFactors base URL            = %s", base_url)
        self.logger.info("SuccessFactors application URL     = %s", as_url)
        self.logger.info("SuccessFactors username            = %s", username)
        self.logger.debug("SuccessFactors password            = %s", password)
        self.logger.info("SuccessFactors client ID           = %s", client_id)
        self.logger.debug("SuccessFactors client secret       = %s", client_secret)
        self.logger.info("SuccessFactors company ID (tenant) = %s", company_id)
        successfactors_object = SuccessFactors(
            base_url=base_url,
            as_url=as_url,
            client_id=client_id,
            client_secret=client_secret,
            username=username,
            password=password,
            company_id=company_id,
            logger=self.logger,
        )

        self._successfactors = successfactors_object

        return successfactors_object

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_salesforce")
    def init_salesforce(self, salesforce_external_system: dict) -> Salesforce | None:
        """Initialize Salesforce object for workspace creation.

        This is needed to query Salesforce API to lookup IDs of Salesforce objects.

        Args:
            salesforce_external_system (dict):
                The payload of the Salesforce external system created before.

        Returns:
            Salesforce | None:
                Salesforce object or None in case an error occured.

        """

        if not salesforce_external_system:
            return None

        username = salesforce_external_system["username"]
        password = salesforce_external_system["password"]
        base_url = salesforce_external_system["base_url"]
        authorization_url = salesforce_external_system.get("token_endpoint", "")
        client_id = salesforce_external_system["oauth_client_id"]
        client_secret = salesforce_external_system["oauth_client_secret"]

        self.logger.info("Connection parameters Salesforce:")
        self.logger.info("Salesforce base URL          = %s", base_url)
        self.logger.info("Salesforce authorization URL = %s", base_url)
        self.logger.info("Salesforce username          = %s", username)
        self.logger.debug("Salesforce password          = %s", password)
        self.logger.info("Salesforce client ID         = %s", client_id)
        self.logger.debug("Salesforce client secret     = %s", client_secret)
        salesforce_object = Salesforce(
            base_url=base_url,
            client_id=client_id,
            client_secret=client_secret,
            username=username,
            password=password,
            authorization_url=authorization_url,
            logger=self.logger,
        )

        self._salesforce = salesforce_object

        return salesforce_object

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_guidewire")
    def init_guidewire(self, guidewire_external_system: dict) -> Guidewire | None:
        """Initialize Guidewire object for workspace creation.

        This is needed to query Guidewire REST API to lookup IDs of Guidewire objects.

        Args:
            guidewire_external_system (dict):
                The payload of the Guidewire external system created before.

        Returns:
            Guidewire | None:
                Guidewire object or None in case an error occured.

        """

        if not guidewire_external_system:
            return None

        system_name = guidewire_external_system["external_system_name"]
        username = guidewire_external_system["username"]
        password = guidewire_external_system["password"]
        base_url = guidewire_external_system["base_url"]
        as_url = guidewire_external_system["as_url"]
        auth_type = guidewire_external_system.get("auth_type", "basic").lower()

        self.logger.info("Connection parameters for %s:", system_name)
        self.logger.info("Guidewire base URL        = %s", base_url)
        self.logger.info("Guidewire application URL = %s", as_url)
        self.logger.info("Guidewire username        = %s", username)
        self.logger.debug("Guidewire password        = %s", password)
        guidewire_object = Guidewire(
            base_url=base_url,
            as_url=as_url,
            auth_type=auth_type,
            username=username,
            password=password,
            logger=self.logger,
        )

        return guidewire_object

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="write_data_source_file")
    def write_data_source_file(
        self,
        data_source: dict,
        compression: bool = True,
    ) -> bool:
        """Write a data source file as JSON into the Admin Personal Workspace in Content Server.

        This allows for further analysis or to reload it in succeeding customizer runs.

        Args:
            data_source (dict):
                Data source dictionary with embedded "data" Data object.
            compression (bool):
                If True, a compressed JSON file gets saved. If False then
                an uncompressed JSON is saved.

        Returns:
            bool:
                True if the data source file (JSON) as been upladed to
                Content Server successfully, False otherwise.

        """

        data_source_name = data_source.get("name", "")
        if not data_source_name:
            self.logger.error("Missing data source name!")
            return False

        data: Data = data_source.get("data")
        if not data:
            self.logger.error(
                "Data source -> '%s' does not have data! Cannot save it.",
                data_source_name,
            )
            return False

        response = self._otcs.get_node_by_volume_and_path(
            volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
        )  # write to Personal Workspace of Admin (with Volume Type ID = 142)
        target_folder_id = self._otcs.get_result_value(response=response, key="id")
        if not target_folder_id:
            target_folder_id = 2004  # use Personal Workspace of Admin as fallback

        if self._payload_source:
            payload_file_name = os.path.basename(
                self._payload_source,
            )  # remove directories
            # Split once at the first occurance of a dot
            # as the _payload_source may have multiple suffixes
            # such as .yml.gz.b64:
            payload_file_name = payload_file_name.split(".", 1)[0] + "_"
        else:
            payload_file_name = ""

        file_name = "data_source_" + payload_file_name + data_source_name + ".json"
        if compression:
            file_name += ".zip"
        full_path = os.path.join(tempfile.gettempdir(), file_name)

        # We also want to keep the row numbers (index):
        if not data.save_json_data(
            json_path=full_path,
            preserve_index=True,
            index_column="row",
            compression="zip" if compression else None,
        ):
            self.logger.error(
                "Data source -> '%s' could not be saved!",
                data_source_name,
            )
            return False

        # Check if the status file has been uploaded before.
        # This can happen if we re-run the python container.
        # In this case we add a version to the existing document:
        response = self._otcs.get_node_by_parent_and_name(
            parent_id=int(target_folder_id),
            name=file_name,
            show_error=False,
        )
        data_source_node_id = self._otcs.get_result_value(response=response, key="id")
        if data_source_node_id:
            response = self._otcs.add_document_version(
                node_id=int(data_source_node_id),
                file_url=full_path,
                file_name=file_name,
                mime_type="application/zip" if compression else "application/json",
                description="Updated data source file after re-run of customization",
            )
        else:
            response = self._otcs.upload_file_to_parent(
                file_url=full_path,
                file_name=file_name,
                mime_type="application/zip" if compression else "application/json",
                parent_id=int(target_folder_id),
            )

        if response:
            self.logger.info(
                "Data source file -> '%s' has been written to Personal Workspace of admin user.",
                file_name,
            )
            # Don't leave stuff behind:
            os.remove(full_path)

            return True

        self.logger.error(
            "Failed to write data source file -> '%s' to Personal Workspace of admin user!",
            file_name,
        )

        return False

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="read_data_source_file")
    def read_data_source_file(
        self,
        data_source: dict,
        compression: bool = True,
    ) -> bool:
        """Read a data source file from the Admin Personal Workspace in Content Server.

        Args:
            data_source (dict):
                Data source dictionary with embedded "data" Data object.
            compression (bool, optional):
                Use True, if the data source file is compressed.

        Returns:
            bool:
                True if the data source file (JSON) as been loaded from
                Content Server successfully, False otherwise

        """

        data_source_name = data_source.get("name", "")
        if not data_source_name:
            self.logger.error("Missing data source name!")
            return False

        response = self._otcs.get_node_by_volume_and_path(
            volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
        )  # write to Personal Workspace of Admin (with Volume Type ID = 142)
        target_folder_id = self._otcs.get_result_value(response=response, key="id")
        if not target_folder_id:
            target_folder_id = 2004  # use Personal Workspace of Admin as fallback

        if self._payload_source:
            payload_file_name = os.path.basename(
                self._payload_source,
            )  # remove directories
            # Split once at the first occurance of a dot
            # as the _payload_source may have multiple suffixes
            # such as .yml.gz.b64:
            payload_file_name = payload_file_name.split(".", 1)[0] + "_"
        else:
            payload_file_name = ""

        file_name = "data_source_" + payload_file_name + data_source_name + ".json"
        if compression:
            file_name += ".zip"
        full_path = os.path.join(tempfile.gettempdir(), file_name)

        # Check if the data source file has been uploaded before.
        # This can happen if we re-run the python container.
        # In this case we add a version to the existing document:
        response = self._otcs.get_node_by_parent_and_name(
            parent_id=int(target_folder_id),
            name=file_name,
            show_error=False,
        )
        data_source_node_id = self._otcs.get_result_value(response=response, key="id")
        if not data_source_node_id:
            self.logger.warning(
                "Data source -> '%s' file -> '%s' not found!",
                data_source_name,
                file_name,
            )
            return False

        self.logger.info(
            "Download data source to temporary file -> '%s'...",
            full_path,
        )
        if not self._otcs.download_document(
            node_id=int(data_source_node_id),
            file_path=full_path,
        ):
            self.logger.error(
                "Error downloading data source -> '%s' to file -> '%s'!",
                data_source_name,
                full_path,
            )
            return False

        self.logger.info("Load data source file -> '%s' into data frame...", full_path)
        data = Data()
        if not data.load_json_data(
            json_path=full_path,
            index_column="row",
            compression="zip" if compression else None,
        ):
            self.logger.error(
                "Data source -> '%s' could not be loaded!",
                data_source_name,
            )
            return False

        self.logger.info(
            "Data source file -> '%s' has been loaded from Personal Workspace of admin user into the data frame.",
            file_name,
        )

        data_source["data"] = data

        # Don't leave stuff behind:
        os.remove(full_path)

        return True

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_otcs")
    def process_bulk_datasource_otcs(self, data_source: dict) -> Data:
        """Load data from Content Server (OTCS) data source into the data frame.

        See helper/data.py

        Args:
            data_source (dict):
                Payload dict element for the data source.

        Returns:
            Data:
                Data class that includes a Pandas DataFrame

        Side Effects:
            self._otcs_source is set to the OTCS object created by this method.

        """

        # 1. Read and validate values from the data source payload:
        otcs_hostname = data_source.get("otcs_hostname", "")
        if not otcs_hostname:
            self.logger.error(
                "Content Server hostname (otcs_hostname) is not specified in payload of bulk data source. Cannot load data!",
            )
            return None
        otcs_protocol = data_source.get("otcs_protocol", "https")
        otcs_port = data_source.get("otcs_port", "443")
        otcs_basepath = data_source.get("otcs_basepath", "/cs/cs")
        otcs_username = data_source.get("otcs_username", "")
        otcs_password = data_source.get("otcs_password", "")
        if not otcs_username or not otcs_password:
            self.logger.error(
                "Content Server user name (otcs_username) or password (otcs_password) are missing in payload of bulk data source. Cannot load data!",
            )
            return None
        otcs_thread_number = data_source.get("otcs_thread_number", BULK_THREAD_NUMBER)
        otcs_download_dir = data_source.get("otcs_download_dir", "/data/contentserver")
        # Root nodes for the traversal of the data loader (can be a list or single ID)
        # we keep two spellings to remain backwards compatible with the payload syntax:
        otcs_root_node_ids = data_source.get(
            "otcs_root_node_ids",
            data_source.get("otcs_root_node_id"),
        )

        if not otcs_root_node_ids:
            self.logger.error(
                "Content Server root node ID(s) for traversal are missing in payload of bulk data source. Cannot load data!",
            )
            return None

        otcs_include_workspaces = data_source.get("otcs_include_workspaces", True)
        otcs_include_items = data_source.get("otcs_include_items", True)
        otcs_include_workspace_metadata = data_source.get("otcs_include_workspace_metadata", True)
        otcs_include_item_metadata = data_source.get("otcs_include_item_metadata", True)

        # Filter workspace by depth under the given root (only consider items as workspace if they have the right depth in the hierarchy):
        otcs_filter_workspace_depth = data_source.get("otcs_filter_workspace_depth", 0)
        # Filter workspace by subtype (only consider items as workspace if they have the right technical subtype):
        # This is NOT the workspace type but the technical subtype (like 848 for workspaces and 0 for folder)
        otcs_filter_workspace_subtypes = data_source.get(
            "otcs_filter_workspace_subtypes",
        )
        # Filter workspace by category name (only consider items as workspace if they have the category):
        otcs_filter_workspace_category = data_source.get(
            "otcs_filter_workspace_category",
        )
        # Filter workspace by attribute values (only consider items as workspace if they have the attributes with the defined values):
        otcs_filter_workspace_attributes = data_source.get(
            "otcs_filter_workspace_attributes",
        )

        # Filter item by depth under the given root:
        otcs_filter_item_depth = data_source.get("otcs_filter_item_depth")
        # Filter item by subtype (only consider items if they have the right technical subtype):
        # This is the technical subtype (like 0 for folder and 144 for documents)
        otcs_filter_item_subtypes = data_source.get(
            "otcs_filter_item_subtypes",
        )
        # Filter item by category name (only consider items as workspace if they have the category):
        otcs_filter_item_category = data_source.get("otcs_filter_item_category")
        # Filter item by attribute values (only consider items if they have the attributes with the defined values):
        otcs_filter_item_attributes = data_source.get("otcs_filter_item_attributes")
        # Filter item also if the are in workspaces (default is True):
        otcs_filter_item_in_workspace = data_source.get(
            "otcs_filter_item_in_workspace",
            True,
        )
        # List of node IDs to exclude in traversing the folders in the OTCS data source:
        otcs_exclude_node_ids = data_source.get("otcs_exclude_node_ids")

        # Document handling parameters:
        otcs_download_documents = data_source.get("otcs_download_documents", True)
        otcs_skip_existing_downloads = data_source.get("otcs_skip_existing_downloads", True)
        otcs_extract_zip = data_source.get("extract_zip", False)
        # The following parameter controls how column names are constructed. If it is true, then
        # attribute columns for workspaces and items will use the category ID in the column name.
        # Wokspace attributes always start with "workspace_cat_". Item attributes start with item_cat_".
        # If the value of 'otcs_use_numeric_category_identifier' is False then the category name
        # is converted to lower-case and spaces and non-alphanumeric characters are replaced with "_".
        # Example with otcs_use_numeric_category_identifier = True: workspace_cat_47110815_10
        # Example with otcs_use_numeric_category_identifier = False: workspace_cat_customer_use_case_10
        otcs_use_numeric_category_identifier = data_source.get("otcs_use_numeric_category_identifier", True)

        # Ensure Root_node_id is a list of integers
        if not isinstance(otcs_root_node_ids, list):
            otcs_root_node_ids = [otcs_root_node_ids]
        otcs_root_node_ids = [int(item) for item in otcs_root_node_ids]

        # 2. Creating the OTCS object:
        self._otcs_source = OTCS(
            protocol=otcs_protocol,
            hostname=otcs_hostname,
            port=otcs_port,
            base_path=otcs_basepath,
            username=otcs_username,
            password=otcs_password,
            thread_number=otcs_thread_number,
            download_dir=otcs_download_dir,
            use_numeric_category_identifier=otcs_use_numeric_category_identifier,
            logger=self.logger,
        )

        # 3. Authenticate at OTCS
        auth_data = self._otcs_source.authenticate()
        if not auth_data:
            self.logger.error(
                "Failed to authenticate at Content Server -> %s!",
                otcs_protocol + "://" + otcs_hostname + otcs_basepath,
            )
            return None
        else:
            self.logger.info(
                "Successfully authenticated at Content Server -> %s.",
                otcs_protocol + "://" + otcs_hostname + otcs_basepath,
            )

        # 4. Load the Content Server data into the Data object (Pandas DataFrame):

        self.logger.info(
            "Loading data (folder, workspaces, items) from Content Server -> %s using root IDs -> %s...",
            otcs_protocol + "://" + otcs_hostname + otcs_basepath,
            otcs_root_node_ids,
        )

        for otcs_root_node_id in otcs_root_node_ids:
            if not self._otcs_source.load_items(
                node_id=otcs_root_node_id,
                workspaces=otcs_include_workspaces,
                items=otcs_include_items,
                filter_workspace_depth=otcs_filter_workspace_depth,
                filter_workspace_subtypes=otcs_filter_workspace_subtypes,
                filter_workspace_category=otcs_filter_workspace_category,
                filter_workspace_attributes=otcs_filter_workspace_attributes,
                filter_item_depth=otcs_filter_item_depth,
                filter_item_subtypes=otcs_filter_item_subtypes,
                filter_item_category=otcs_filter_item_category,
                filter_item_attributes=otcs_filter_item_attributes,
                filter_item_in_workspace=otcs_filter_item_in_workspace,
                exclude_node_ids=otcs_exclude_node_ids,
                workspace_metadata=otcs_include_workspace_metadata,
                item_metadata=otcs_include_item_metadata,
                download_documents=otcs_download_documents,
                skip_existing_downloads=otcs_skip_existing_downloads,
                extract_zip=otcs_extract_zip,
                workers=otcs_thread_number,
            ):
                self.logger.error(
                    "Failure during load of Content Server items from root node ID -> %s!",
                    str(otcs_root_node_id),
                )
                return None

        data = self._otcs_source.get_data()
        if data is None:
            self.logger.error(
                "Failure during load of Content Server items! No data loaded!",
            )
            return None

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_servicenow")
    def process_bulk_datasource_servicenow(self, data_source: dict) -> Data | None:
        """Load data from ServiceNow data source into the data frame of the Data class.

        See helper/data.py

        Args:
            data_source (dict):
                Payload dict element for the data source.

        Returns:
            Data | None:
                Data class that includes a Pandas data frame. None in case of an error.

        Side Effects:
            self._servicenow is set to the ServiceNow object created by this method

        """

        # 1. Read and validate values from the data source payload:
        sn_base_url = data_source.get("sn_base_url", "")
        if not sn_base_url:
            self.logger.error(
                "ServiceNow base URL (sn_base_url) is not specified in payload of bulk data source. Cannot load data!",
            )
            return None
        sn_auth_type = data_source.get("sn_auth_type", "basic")
        sn_username = data_source.get("sn_username", "")
        sn_password = data_source.get("sn_password", "")
        sn_client_id = data_source.get("sn_client_id")
        sn_client_secret = data_source.get("sn_client_secret")
        sn_table_name = data_source.get(
            "sn_table_name",
            "u_kb_template_technical_article_public",
        )
        sn_queries = data_source.get("sn_queries", [])
        sn_query = data_source.get("sn_query")
        if sn_query is not None:
            sn_queries.append({"table": sn_table_name, "query": sn_query})

        sn_thread_number = data_source.get("sn_thread_number", BULK_THREAD_NUMBER)
        sn_download_dir = data_source.get("sn_download_dir", "/data/knowledgebase")
        sn_skip_existing_downloads = data_source.get("sn_skip_existing_downloads", True)
        sn_product_exclusions = data_source.get("sn_product_exclusions", [])

        if sn_product_exclusions:
            self.logger.info(
                "Excluding products -> %s from ServiceNow data loading.",
                sn_product_exclusions,
            )

        if sn_base_url and (sn_auth_type == "basic") and (not sn_username or not sn_password):
            self.logger.error(
                "ServiceNow basic authentication needs username and password in payload!",
            )
            return None
        if sn_base_url and (sn_auth_type == "oauth") and (not sn_client_id or not sn_client_secret):
            self.logger.error(
                "ServiceNow OAuth authentication needs client ID and client secret in payload!",
            )
            return None

        # 2. Creating the ServiceNow object:
        self._servicenow = ServiceNow(
            base_url=sn_base_url,
            auth_type=sn_auth_type,
            client_id=sn_client_id,
            client_secret=sn_client_secret,
            username=sn_username,
            password=sn_password,
            thread_number=sn_thread_number,
            download_dir=sn_download_dir,
            product_exclusions=sn_product_exclusions,
            logger=self.logger,
        )

        # 3. Authenticate at ServiceNow
        auth_data = self._servicenow.authenticate(auth_type=sn_auth_type)
        if not auth_data:
            self.logger.error("Failed to authenticate at ServiceNow -> %s!", sn_base_url)
            return None
        else:
            self.logger.info(
                "Successfully authenticated at ServiceNow -> %s.",
                sn_base_url,
            )

        # 4. Load the ServiceNow data into the Data object (Pandas DataFrame):
        for item in sn_queries:
            sn_table_name = item["sn_table_name"]
            sn_query = item["sn_query"]

            self.logger.info(
                "Loading data from ServiceNow table -> '%s' with query -> '%s'...",
                sn_table_name,
                sn_query,
            )

            if not self._servicenow.load_articles(
                table_name=sn_table_name,
                query=sn_query,
                skip_existing_downloads=sn_skip_existing_downloads,
            ):
                self.logger.error(
                    "Failure during load of ServiceNow articles from table -> '%s' using query -> '%s' !",
                    sn_table_name,
                    sn_query,
                )
                continue

        data = self._servicenow.get_data()
        if data is None:
            self.logger.error(
                "Failure during load of ServiceNow articles! No articles loaded!",
            )
            return None

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_otmm")
    def process_bulk_datasource_otmm(self, data_source: dict) -> Data | None:
        """Load data from OTMM data source into the data frame of the Data class.

        See helper/data.py

        Args:
            data_source (dict):
                Payload dict element for the data source.

        Returns:
            Data | None:
                Data class that includes a Pandas data frame. None in case of an error.

        Side Effects:
            self._otmm is set to the OTMM object created by this method

        """

        # 1. Read and validate values from the data source payload:
        otmm_base_url = data_source.get("otmm_base_url", "")
        if not otmm_base_url:
            self.logger.error(
                "OTMM base URL (otmm_base_url) is not specified in payload of bulk data source. Cannot load data!",
            )
            return None
        otmm_username = data_source.get("otmm_username", "")
        otmm_password = data_source.get("otmm_password", "")
        otmm_client_id = data_source.get("otmm_client_id")
        otmm_client_secret = data_source.get("otmm_client_secret")
        otmm_thread_number = data_source.get("otmm_thread_number", BULK_THREAD_NUMBER)
        otmm_download_dir = data_source.get("otmm_download_dir", "/data/mediaassets")
        otmm_business_unit_exclusions = data_source.get(
            "otmm_business_unit_exclusions",
            [],
        )
        otmm_business_unit_inclusions = data_source.get("otmm_business_unit_inclusions")
        otmm_product_exclusions = data_source.get("otmm_product_exclusions", [])
        otmm_product_inclusions = data_source.get("otmm_product_inclusions")
        otmm_asset_exclusions = data_source.get("otmm_asset_exclusions", [])
        otmm_asset_inclusions = data_source.get("otmm_asset_inclusions", [])

        self.logger.info(
            "Loading data from OpenText Media Management -> %s (Marketing Assets)...",
            otmm_base_url,
        )

        # 2. Creating the OTMM object:
        self._otmm = OTMM(
            base_url=otmm_base_url,
            client_id=otmm_client_id,
            client_secret=otmm_client_secret,
            username=otmm_username,
            password=otmm_password,
            thread_number=otmm_thread_number,
            download_dir=otmm_download_dir,
            business_unit_exclusions=otmm_business_unit_exclusions,
            business_unit_inclusions=otmm_business_unit_inclusions,
            product_exclusions=otmm_product_exclusions,
            product_inclusions=otmm_product_inclusions,
            asset_exclusions=otmm_asset_exclusions,
            asset_inclusions=otmm_asset_inclusions,
            logger=self.logger,
        )
        if not self._otmm:
            self.logger.error("Failed to initialize OTMM object!")
            return None

        # 3. Authenticate at OTMM
        token = self._otmm.authenticate()
        if not token:
            self.logger.error(
                "Failed to authenticate at OpenText Media Management -> %s!",
                otmm_base_url,
            )
            return None
        else:
            self.logger.info(
                "Successfully authenticated at OpenText Media Management -> %s.",
                otmm_base_url,
            )

        # 4. Load the OTMM assets into the Data object (Pandas DataFrame):
        if not self._otmm.load_assets():
            self.logger.error(
                "Failure during load of OpenText Media Management assets!",
            )
            return None

        data = self._otmm.get_data()
        if data is None:
            self.logger.error(
                "Failure during load of OpenText Media Management assets! No assets loaded!",
            )
            return None

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_pht")
    def process_bulk_datasource_pht(self, data_source: dict) -> Data | None:
        """Load data from OpenText PHT data source into the data frame of the Data class.

        See helper/data.py

        Args:
            data_source (dict):
                Payload dict element for the data source.

        Returns:
            Data | None:
                Data class that includes a Pandas data frame. None in case of an error.

        Side Effects:
            self._pht is set to the PHT object created by this method.

        """

        try:
            from pyxecm_internal.pht import PHT
        except ModuleNotFoundError:
            self.logger.info(
                "Python module 'pyxecm_internal' not installed. Cannot process PHT data source!",
            )
            return None

        # 1. Read and validate values from the data source payload:
        pht_base_url = data_source.get("pht_base_url", "")
        if not pht_base_url:
            self.logger.error(
                "PHT base URL (pht_base_url) is not specified in payload of bulk data source. Cannot load data!",
            )
            return None
        pht_username = data_source.get("pht_username", "")
        if not pht_username:
            self.logger.error(
                "PHT username (pht_username) is not specified in payload of bulk data source. Cannot load data!",
            )
            return None
        pht_password = data_source.get("pht_password", "")
        if not pht_password:
            self.logger.error(
                "PHT password (pht_password) is not specified in payload of bulk data source. Cannot load data!",
            )
            return None

        pht_business_unit_exclusions = data_source.get(
            "pht_business_unit_exclusions",
            [],
        )
        pht_business_unit_inclusions = data_source.get(
            "pht_business_unit_inclusions",
            [],
        )
        pht_product_exclusions = data_source.get("pht_product_exclusions", [])
        pht_product_inclusions = data_source.get("pht_product_inclusions", [])
        pht_product_category_exclusions = data_source.get(
            "pht_product_category_exclusions",
            [],
        )
        pht_product_category_inclusions = data_source.get(
            "pht_product_category_inclusions",
            [],
        )
        pht_product_status_exclusions = data_source.get(
            "pht_product_status_exclusions",
            [],
        )
        pht_product_status_inclusions = data_source.get(
            "pht_product_status_inclusions",
            [],
        )
        pht_product_attributes = data_source.get(
            "pht_product_attributes",
            [],
        )

        self.logger.info(
            "Loading data from OpenText PHT -> %s (Product Hierarchy)...",
            pht_base_url,
        )

        # 2. Creating the PHT object:
        self._pht = PHT(
            base_url=pht_base_url,
            username=pht_username,
            password=pht_password,
            business_unit_exclusions=pht_business_unit_exclusions,
            business_unit_inclusions=pht_business_unit_inclusions,
            product_exclusions=pht_product_exclusions,
            product_inclusions=pht_product_inclusions,
            product_category_exclusions=pht_product_category_exclusions,
            product_category_inclusions=pht_product_category_inclusions,
            product_status_exclusions=pht_product_status_exclusions,
            product_status_inclusions=pht_product_status_inclusions,
            logger=self.logger,
        )
        if not self._pht:
            self.logger.error("Failed to initialize PHT object!")
            return None

        # 3. Authenticate at PHT
        token = self._pht.authenticate()
        if not token:
            self.logger.error(
                "Failed to authenticate at OpenText PHT -> %s!",
                pht_base_url,
            )
            return None
        else:
            self.logger.info(
                "Successfully authenticated at OpenText PHT -> %s.",
                pht_base_url,
            )

        # 4. Load the PHT product information into the Data object (Pandas DataFrame):
        if not self._pht.load_products(attributes_to_extract=pht_product_attributes):
            self.logger.error("Failure during load of OpenText PHT products!")
            return None

        data = self._pht.get_data()
        if data is None:
            self.logger.error("Failure during load of OpenText PHT product data!")
            return None

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="prepare_datasource_file")
    def prepare_datasource_file(self, data_source: dict, filename: str) -> str:
        """Download data source from HTTP/HTTPS link and store it in a local temp file.

        Args:
            data_source (dict):
                The data source object from payload.
            filename (str):
                The filename to check if it is a reference to an external file.

        Returns:
            str:
                Filename or Filename of the local temp file.

        """

        if not filename.startswith(("http://", "https://")):
            return filename

        name = data_source.get("name", "")
        tmp_filename = os.path.join(tempfile.gettempdir(), "{}_{}".format(name, os.path.basename(filename)))

        if os.path.isfile(tmp_filename):
            self.logger.info("Reusing previously downloaded file -> '%s'.", tmp_filename)
            return tmp_filename

        try:
            self.logger.info(
                "Downloading data source from -> %s to -> %s...",
                filename,
                tmp_filename,
            )
            response = requests.get(filename, stream=True, timeout=10)

            with open(tmp_filename, "wb") as f:
                for chunk in response.iter_content(chunk_size=1024):
                    if chunk:
                        f.write(chunk)

        except Exception:
            self.logger.error("Error downloading JSON data source -> '%s'!", filename)

        return tmp_filename

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_excel")
    def process_bulk_datasource_excel(self, data_source: dict) -> Data | None:
        """Load data from Excel files into the data frame of the Data class.

        See helper/data.py

        Args:
            data_source (dict):
                Payload dict element for the data source.

        Returns:
            Data | None:
                Data class that includes a Pandas data frame. None in case of an error.

        """

        # 1. Read and validate values from the data source payload:
        xlsx_files = data_source.get("xlsx_files", [])
        if not xlsx_files:
            self.logger.error(
                "Excel files not specified in payload of bulk data source. Cannot load data!",
            )
            return None
        xlsx_sheets = data_source.get("xlsx_sheets", 0)  # use 0 not None!
        xlsx_columns = data_source.get("xlsx_columns")
        xlsx_skip_rows = data_source.get("xlsx_skip_rows", 0)
        xlsx_na_values = data_source.get("xlsx_na_values")

        # 2. Initialize Data object:
        data = Data(logger=self.logger)

        # 3. Iterate of Excel files and load them into the Data object:
        for xlsx_file in xlsx_files:
            if not data.load_excel_data(
                xlsx_path=xlsx_file,
                sheet_names=xlsx_sheets,
                usecols=xlsx_columns,
                skip_rows=xlsx_skip_rows,
                na_values=xlsx_na_values,
            ):
                self.logger.error("Failed to load Excel file -> '%s'!", xlsx_file)
                return None

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_xml")
    def process_bulk_datasource_xml(self, data_source: dict) -> Data | None:
        """Load data from XML files or directories or zip files into the data frame of the Data class.

        See helper/data.py

        Args:
            data_source (dict):
                Payload dict element for the data source.

        Returns:
            Data | None:
                Data class that includes a Pandas data frame. None in case of an error.

        """

        # 1. Read and validate values from the data source payload:
        xml_files = data_source.get("xml_files", [])
        xml_directories = data_source.get(
            "xml_directories",
            [],
        )  # can also be zip files
        xml_xpath = data_source.get("xml_xpath")

        # 2. Initialize Data object:
        data = Data(logger=self.logger)

        # 3. If no XML directories are specified we interpret the "xml_files"
        if not xml_directories:
            for xml_file in xml_files:
                self.logger.info("Loading XML file -> '%s'...", xml_file)
                if not data.load_xml_data(xml_path=xml_file, xpath=xml_xpath):
                    self.logger.error("Failed to load XML file -> '%s'!", xml_file)
                    return None

        # 4. If XML directories or zip files of XML files are given we traverse them instead:
        else:
            # we now produce a list of dictionaries:
            xml_data = XML.load_xml_files_from_directories(
                directories=xml_directories,
                filenames=xml_files,
                xpath=xml_xpath,
                logger=self.logger.getChild("xml"),
            )
            if xml_data is None:  # test on error
                self.logger.error(
                    "Failed to load XML files from directories or ZIP files -> %s!",
                    xml_directories,
                )
                return None
            if not xml_data:  # test on empty result
                self.logger.warning(
                    "Found no XML files in directory or ZIP files -> %s!",
                    xml_directories,
                )
            else:
                data.append(add_data=xml_data)

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_json")
    def process_bulk_datasource_json(self, data_source: dict) -> Data | None:
        """Load data from JSON files into the data frame of the Data class.

        See helper/data.py

        Args:
            data_source (dict):
                Payload dict element for the data source.

        Returns:
            Data | None:
                Data class that includes a Pandas data frame. None in case of an error.

        """

        # 1. Read and validate values from the data source payload:
        json_files = data_source.get("json_files", [])
        if not json_files:
            self.logger.error(
                "JSON files not specified in payload of bulk data source. Cannot load data!",
            )
            return None

        # 2. Initialize Data object:
        data = Data(logger=self.logger)

        # 3. Iterate JSON files and load data into Data object:
        for json_file in json_files:
            json_file = self.prepare_datasource_file(
                data_source=data_source,
                filename=json_file,
            )

            if not data.load_json_data(json_path=json_file):
                self.logger.error(
                    "Invalid JSON file -> '%s'. Cannot load it!",
                    json_file,
                )
                return None

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_csv")
    def process_bulk_datasource_csv(self, data_source: dict) -> Data | None:
        """Load data from CSV files (comma-separated values) into the data frame of the Data class.

        See helper/data.py

        Args:
            data_source (dict):
                Payload dict element for the data source.

        Returns:
            Data | None:
                Data class that includes a Pandas data frame. None in case of an error.

        """

        # 1. Read and validate values from the data source payload:
        csv_files = data_source.get("csv_files", [])
        if not csv_files:
            self.logger.error(
                "CSV files not specified in payload of bulk data source. Cannot load data!",
            )
            return None
        csv_delimiter = data_source.get("csv_delimiter", ",")
        # The header index is an integer. The first row / line has the index 0.
        # But 0 is not the default! The default is the CSV does not have a header line
        # at all (None):
        csv_header_index = data_source.get("csv_header_index")
        csv_column_names = data_source.get("csv_column_names")
        csv_use_columns = data_source.get("csv_use_columns")

        # If no headers is specified it means the CSV does not have column
        # names in a row (typically row 0 = first row). If we also don't
        # have the names for the columns we will end with having coumn names
        # that a index values (1, 2, 3, ...). This may not be what the payload
        # author wants - so we issue a warning:
        if not csv_column_names and csv_header_index is None:  # "is None" is important here as the index can be 0
            self.logger.warning(
                "CSV loader parameters are missing both, header line index (no 'csv_header_index') and column names (no 'csv_column_names'). This could lead to numeric column names.",
            )

        # 2. Initialize Data object:
        data = Data(logger=self.logger)

        # 3. Iterate CSV files and load data into Data object:
        for csv_file in csv_files:
            if not data.load_csv_data(
                csv_path=csv_file,
                delimiter=csv_delimiter,
                names=csv_column_names,
                header=csv_header_index,
                usecols=csv_use_columns,
            ):
                self.logger.error("failed to load CSV file -> '%s'!", csv_file)
                return None

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_web")
    def process_bulk_datasource_web(self, data_source: dict) -> Data | None:
        """Load data from Web page into the data frame of the Data class.

        See helper/data.py

        Args:
            data_source (dict):
                Payload dict element for the data source

        Returns:
            Data | None:
                Data class that includes a Pandas DataFrame. None in case of an error.

        """

        # 1. Read and validate values from the data source payload:
        web_url_templates = data_source.get("web_url_templates")
        if not web_url_templates:
            self.logger.error(
                "Web URLs (templates) not specified in payload of bulk data source. Cannot load data!",
            )
            return None
        web_start_value = data_source.get("web_start_value")
        web_end_value = data_source.get("web_end_value")
        web_value_name = data_source.get(
            "web_value_name",
        )  # which column name to use for the value
        web_special_url_templates = data_source.get("web_special_url_templates", [])
        # Pattern to filter file links on the page
        web_file_url_pattern = data_source.get("web_file_url_pattern")

        web_values = list(range(web_start_value, web_end_value + 1)) if web_start_value and web_end_value else None
        web_special_values = data_source.get("web_special_values", [])

        # 2. Initialize Data object:
        data = Data(logger=self.logger)

        # 3. Iterate over Web pages and load data into Data object:
        if not data.load_web(
            values=web_values,
            value_name=web_value_name,
            url_templates=web_url_templates,
            special_values=web_special_values,
            special_url_templates=web_special_url_templates,
            pattern=web_file_url_pattern,
        ):
            self.logger.error(
                "Failed to load Web data from URLs -> %s!",
                str(web_url_templates),
            )
            return None

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_nhc")
    def process_bulk_datasource_nhc(self, data_source: dict) -> Data | None:
        """Load data from National Hurricane Center data source into the data frame of the Data class.

        See helper/data.py

        Args:
            data_source (dict):
                Payload dict element for the data source

        Returns:
            Data | None:
                Data class that includes a Pandas data frame. None in cause of an error.

        Side Effects:
            self._nhc is set to the NHC object created by this method.

        """
        try:
            from pyxecm_internal.nhc import (
                NHC,
            )
        except ModuleNotFoundError:
            self.logger.info(
                "Python module 'pyxecm_internal' not installed. Cannot process NHC data source!",
            )
            return None

        # 1. Read and validate values from the data source payload:
        nhc_year_start = data_source.get("nhc_year_start", "2023")
        nhc_year_end = data_source.get("nhc_year_end", "2024")
        nhc_basin = data_source.get("nhc_basin", "north_atlantic")
        nhc_track_images = data_source.get("nhc_track_images", ["png"])
        nhc_track_data = data_source.get("nhc_track_data", [])
        nhc_rain_images = data_source.get("nhc_rain_images", ["png"])
        nhc_rain_data = data_source.get("nhc_rain_data", [])
        nhc_skip_existing_files = data_source.get("nhc_skip_existing_files", True)
        nhc_async = data_source.get("nhc_async", True)
        nhc_async_processes = data_source.get("nhc_async_processes", 5)
        nhc_async_processes = data_source.get("nhc_async_processes", 5)
        nhc_storm_plot_exclusions = data_source.get("nhc_storm_plot_exclusions", [])
        nhc_download_dir = data_source.get("nhc_download_dir", "/data/nhc")
        # We either get the download dir for images from the payload
        # directly or we construct it from the general download dir:
        nhc_download_dir_images = data_source.get(
            "nhc_download_dir_images",
            os.path.join(nhc_download_dir, "images"),
        )  # don't use "/images"
        # We either get the download dir for data files (JSON) from the
        # payload directly or we construct it from the general download dir:
        nhc_download_dir_data = data_source.get(
            "nhc_download_dir_images",
            os.path.join(nhc_download_dir, "json"),
        )  # don't use "/data"

        # 2. Creating the NHC object:
        self._nhc = NHC(
            basin=nhc_basin,
            storm_plot_exclusions=nhc_storm_plot_exclusions,
            download_dir_images=nhc_download_dir_images,
            download_dir_data=nhc_download_dir_data,
            logger=self.logger,
        )
        if not self._nhc:
            self.logger.error("Failed to initialize NHC object!")
            return None

        # 3. Load the NHC storms into the Data object (Pandas DataFrame):
        if not self._nhc.load_storms(
            year_start=nhc_year_start,
            year_end=nhc_year_end,
            save_track_images=nhc_track_images,
            save_track_data=nhc_track_data,
            save_rain_images=nhc_rain_images,
            save_rain_data=nhc_rain_data,
            skip_existing_files=nhc_skip_existing_files,
            load_async=nhc_async,
            async_processes=nhc_async_processes,
        ):
            self.logger.error("Failure during load of NHC storms!")
            return None

        data = self._nhc.get_data()
        if data is None:
            self.logger.error("Failure during load of NHC storm data!")
            return None

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_filesystem")
    def process_bulk_datasource_filesystem(self, data_source: dict) -> Data | None:
        """Load data from filesystem into the data frame of the Data class.

        See helper/data.py

        Args:
            data_source (dict):
                Payload dict element for the data source.

        Returns:
            Data | None:
                Data class that includes a Pandas data frame. None in case of an error.

        """

        # 1. Read and validate values from the data source payload:
        root_folders = data_source.get("root_folders", [])
        if not root_folders:
            self.logger.error(
                "Root folders not specified in payload of bulk data source. Cannot load data from filesystem!",
            )
            return None

        if not isinstance(root_folders, list):
            self.logger.warning(
                "The payload for root folders of the 'filesytem' data source should be a list! You should adjust the 'root_folders' setting.",
            )
            root_folders: list = [root_folders]

        # 2. Initialize Data object:
        data = Data(logger=self.logger)

        # 3. Iterate root folders and load data into Data object:
        for folder in root_folders:
            if not data.load_directory(path_to_root=folder):
                self.logger.error(
                    "Failed to load data from root folder -> '%s'!",
                    folder,
                )
                return None

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource")
    def process_bulk_datasource(
        self,
        data_source_name: str,
        force_reload: bool = True,
    ) -> Data | None:
        """Process a data source that is given by a payload element.

        Parse its properties and deliver a 'Data' object which is a wrapper for
        a Pandas data frame.

        Args:
            data_source_name (str):
                The data source name.
            force_reload (bool):
                Force a reload of the data source if True.

        Returns:
            Data | None:
                Data source object of type Data. None in case of an error.

        """

        if not data_source_name:
            self.logger.error("Missing data source name!")
            return None

        self._log_header_callback(
            text="Process Bulk Data Source -> '{}'".format(data_source_name),
            char="-",
        )

        self.logger.info(
            "Found specified data source name -> '%s'. Lookup the data source payload...",
            data_source_name,
        )
        data_source = next(
            (item for item in self._bulk_datasources if item["name"] == data_source_name),
            None,
        )
        if not data_source:  # does this data source not exist?
            self.logger.error(
                "Cannot find specified data source -> '%s' in payload!",
                data_source_name,
            )
            return None

        # Check if data has already been loaded for the data source:
        if "data" in data_source and not force_reload:
            self.logger.info(
                "Data for data source -> '%s' is already loaded and reload is not enforced. Return existing data...",
                data_source_name,
            )
            return data_source["data"]
        elif force_reload:
            self.logger.info(
                "Reload of data from data source -> '%s' is enforced. Building data...",
                data_source_name,
            )
        else:
            self.logger.info(
                "Data for data source -> '%s' is not yet available. Building data...",
                data_source_name,
            )
            # Try to load the data source in the Admin Personal Workspace for investigations:
            if data_source.get("load_data_source", False):
                self.logger.info(
                    "Payload allows to reload existing data source. Check if the data source -> '%s' can be reloaded...",
                    data_source_name,
                )
                if self.read_data_source_file(data_source=data_source):
                    return data_source["data"]
                else:
                    self.logger.warning(
                        "Couldn't load the data source -> '%s' from file. Fall back to reloading it from the source...",
                        data_source_name,
                    )

        data_source_type = data_source.get("type", None)
        if not data_source_type:
            self.logger.error(
                "Data source needs a type parameter. This is new - you may need to add it to your bulkDataSource payload definition file! Cannot load data.",
            )
            return None

        match data_source_type:
            case "excel":
                data = self.process_bulk_datasource_excel(data_source=data_source)
                if data is None:
                    self.logger.error("Failure during load of Excel file!")
                    return None
            case "servicenow":
                data = self.process_bulk_datasource_servicenow(data_source=data_source)
                if data is None:
                    self.logger.error("Failure during load of ServiceNow articles!")
                    return None
            case "otmm":
                data = self.process_bulk_datasource_otmm(data_source=data_source)
                if data is None:
                    self.logger.error(
                        "Failure during load of OpenText Media Management assets!",
                    )
                    return None
            case "otcs":
                data = self.process_bulk_datasource_otcs(data_source=data_source)
                if data is None:
                    self.logger.error(
                        "Failure during load of OpenText Content Server items!",
                    )
                    return None
            case "pht":
                data = self.process_bulk_datasource_pht(data_source=data_source)
                if data is None:
                    self.logger.error(
                        "Failure during load of OpenText Product Hierarchy (PHT)!",
                    )
                    return None
            case "nhc":
                data = self.process_bulk_datasource_nhc(data_source=data_source)
                if data is None:
                    self.logger.error(
                        "Failure during load of National Hurricane Center data (NHC)!",
                    )
                    return None
            case "json":
                data = self.process_bulk_datasource_json(data_source=data_source)
                if data is None:
                    self.logger.error("Failure during load of JSON data source!")
                    return None
            case "xml":
                data = self.process_bulk_datasource_xml(data_source=data_source)
                if data is None:
                    self.logger.error("Failure during load of XML data source!")
                    return None
            case "csv":
                data = self.process_bulk_datasource_csv(data_source=data_source)
                if data is None:
                    self.logger.error("Failure during load of CSV data source!")
                    return None
            case "web":
                data = self.process_bulk_datasource_web(data_source=data_source)
                if data is None:
                    self.logger.error("Failure during load of Web data source!")
                    return None
            case "filesystem":
                data = self.process_bulk_datasource_filesystem(data_source=data_source)
                if data is None:
                    self.logger.error("Failure during load of Web data source!")
                    return None
            case _:
                self.logger.error(
                    "Illegal data source type. Types supported: 'excel', 'servicenow', 'otmm', 'otcs', 'pht', 'json', 'csv', 'xml', 'web', or 'filesystem'",
                )
                return None

        if data.get_data_frame() is None or data.get_data_frame().empty:
            self.logger.warning("Data source is empty - nothing loaded.")
            return None

        self.logger.info(
            "Data Frame for source -> '%s' has %s row(s) and %s column(s) after data loading.",
            data_source_name,
            data.get_data_frame().shape[0],
            data.get_data_frame().shape[1],
        )

        cleansings = data_source.get("cleansings", {})
        columns_to_drop = data_source.get("columns_to_drop", [])
        columns_to_keep = data_source.get("columns_to_keep", [])
        columns_to_add = data_source.get("columns_to_add", [])
        columns_to_add_list = data_source.get("columns_to_add_list", [])
        columns_to_add_concat = data_source.get("columns_to_add_concat", [])
        columns_to_add_table = data_source.get("columns_to_add_table", [])
        filters = data_source.get("filters", [])
        if not filters:
            # Backward compatibility. This used to be called "conditions" before
            # but we don't want to mix this with the conditions on row level
            # we have for bulkWorkspaces and bulkDocuments:
            filters = data_source.get("conditions", [])
        explosions = data_source.get("explosions", [])

        # Cleanup data if specified in data_source. We do this first!
        if cleansings:
            self.logger.info(
                "Start cleansing for data source -> '%s'...",
                data_source_name,
            )
            data.cleanse(cleansings=cleansings)
            self.logger.info(
                "Cleansing for data source -> '%s' completed...",
                data_source_name,
            )

        # Add columns if specified in data_source:
        for add_column in columns_to_add:
            if "source_column" not in add_column or "name" not in add_column:
                self.logger.error(
                    "Add columns is missing name or source column. Column will not be added.",
                )
                continue
            data.add_column(
                source_column=add_column["source_column"],
                new_column=add_column["name"],
                reg_exp=add_column.get("regex", add_column.get("reg_exp", None)),
                prefix=add_column.get("prefix", ""),
                suffix=add_column.get("suffix", ""),
                length=add_column.get("length", None),
                group_chars=add_column.get("group_chars", None),
                group_separator=add_column.get("group_separator", "."),
            )

        # Add columns with list values from a list of other columns
        # if specified in data_source:
        for add_column in columns_to_add_list:
            if "source_columns" not in add_column or "name" not in add_column:
                self.logger.error(
                    "Add list columns is missing name or source columns. Column will not be added.",
                )
                continue
            data.add_column_list(
                source_columns=add_column["source_columns"],
                new_column=add_column["name"],
            )

        # Add columns with list values from a list of other columns
        # if specified in data_source:
        for add_column in columns_to_add_concat:
            if "source_columns" not in add_column or "name" not in add_column:
                self.logger.error(
                    "Add concatenation columns is missing name or source columns. Column will not be added.",
                )
                continue
            data.add_column_concat(
                source_columns=add_column["source_columns"],
                new_column=add_column["name"],
                concat_char=add_column.get("concat_chars", ""),
                lower=add_column.get("lower", False),
                upper=add_column.get("upper", False),
                capitalize=add_column.get("capitalize", False),
                title=add_column.get("title", False),
            )

        # Add columns with list values from a list of other columns
        # if specified in data_source:
        for add_column in columns_to_add_table:
            if "source_columns" not in add_column or "name" not in add_column:
                self.logger.error(
                    "Add table columns is missing name or source columns. Column will not be added.",
                )
                continue
            data.add_column_table(
                source_columns=add_column["source_columns"],
                new_column=add_column["name"],
                delimiter=add_column.get("list_splitter", ","),
            )

        # Drop columns if specified in data_source:
        if columns_to_drop:
            data.drop_columns(columns_to_drop)

        # Keep only selected columns if specified in data_source:
        if columns_to_keep:
            data.keep_columns(columns_to_keep)

        # Check if fields with list substructures should be exploded:
        for explosion in explosions:
            # The explode field parameter can be a string or a list.
            # Exploding multiple fields at once avoids combinatorial explosions -
            # this is VERY different from exploding columns one after the other!
            if "explode_field" not in explosion and "explode_fields" not in explosion:
                self.logger.error("Missing explosion field(s)!")
                continue
            # We support both syntax (singular + plural):
            explode_fields = (
                explosion["explode_field"] if "explode_fields" not in explosion else explosion["explode_fields"]
            )

            flatten_fields = explosion.get("flatten_fields", [])
            split_string_to_list = explosion.get("split_string_to_list", False)
            list_splitter = explosion.get("list_splitter", None)
            self.logger.info(
                "Starting explosion of data source -> '%s' by field(s) -> '%s' (type -> '%s'). Size of data frame before explosion -> %s.",
                data_source_name,
                str(explode_fields),
                type(explode_fields),
                str(len(data)),
            )
            data.explode_and_flatten(
                explode_fields=explode_fields,
                flatten_fields=flatten_fields,
                make_unique=False,
                split_string_to_list=split_string_to_list,
                separator=list_splitter,
                reset_index=True,
            )
            self.logger.info("Size of data frame after explosion -> %s.", str(len(data)))

        # Keep only selected rows if filters are specified in data_source
        # We have this after "explosions" to allow access to subfields as well:
        if filters:
            data.filter(conditions=filters)

        # Keep the Data Frame for later lookups:
        data_source["data"] = data

        self._log_header_callback(
            text="Completed Bulk Data Source -> '{}'".format(data_source_name),
            char="-",
        )

        # Save the data source in the Admin Personal Workspace for investigations:
        if data_source.get("save_data_source", False):
            self.write_data_source_file(data_source=data_source)

        return data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspaces")
    def process_bulk_workspaces(self, section_name: str = "bulkWorkspaces") -> bool:
        """Process workspaces in payload and bulk create them in Content Server (multi-threaded).

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not self._bulk_workspaces:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        # For efficient idem-potent operation we may want to see which workspaces
        # have already been processed before:
        if self.check_status_file(
            payload_section_name=section_name,
            payload_specific=True,
            prefix="failure_",
        ):
            # Read payload from where we left it last time
            self._bulk_workspaces = self.get_status_file(
                payload_section_name=section_name,
                prefix="failure_",
            )
            if not self._bulk_workspaces:
                self.logger.error(
                    "Cannot load existing bulkWorkspaces failure file. Bailing out!",
                )
                return False

        success: bool = True

        for bulk_workspace in self._bulk_workspaces:
            # Check if element has been disabled in payload (enabled = false).
            # In this case we skip the element:
            enabled = bulk_workspace.get("enabled", True)
            if not enabled:
                self.logger.info("Payload for bulk workspace is disabled. Skipping...")
                continue

            # Read workspace type name from payload:
            type_name = bulk_workspace.get("type_name", None)
            if not type_name:
                self.logger.error(
                    "Bulk workspace needs a workspace type name! Skipping to next workspace...",
                )
                success = False
                continue

            # The payload element must have a "data_source" key:
            data_source_name = bulk_workspace.get("data_source", None)
            if not data_source_name:
                self.logger.error("Missing data source name in bulk workspace!")
                success = False
                continue

            self._log_header_callback(
                text="Process bulk workspaces for -> '{}' using data source -> '{}'".format(
                    type_name,
                    data_source_name,
                ),
                char="-",
            )

            copy_data_source = bulk_workspace.get("copy_data_source", False)
            force_reload = bulk_workspace.get("force_reload", True)

            # Load and prepare the data source for the bulk processing:
            if copy_data_source:
                self.logger.info(
                    "Take a copy of data source -> '%s' to avoid side-effects for repeative usage of the data source...",
                    data_source_name,
                )
                data = Data(
                    self.process_bulk_datasource(
                        data_source_name=data_source_name,
                        force_reload=force_reload,
                    ),
                    logger=self.logger,
                )
            else:
                self.logger.info(
                    "Use original data source -> '%s' and not do a copy.",
                    bulk_workspace["data_source"],
                )
                data = self.process_bulk_datasource(
                    data_source_name=data_source_name,
                    force_reload=force_reload,
                )
            if data is None:  # important to use "is None" here!
                self.logger.error(
                    "Failed to load data source -> '%s' for bulk workspace type -> '%s'!",
                    data_source_name,
                    type_name,
                )
                success = False
                continue
            if data.get_data_frame() is None:  # important to use "is None" here!
                self.logger.error(
                    "Data source for bulk workspace type -> '%s' is empty!",
                    type_name,
                )
                continue

            # Check if fields with list substructures should be exploded.
            # We may want to do this outside the bulkDatasource to only
            # explode for bulkDocuments and not for bulkWorkspaces or
            # bulkWorkspaceRelationships:
            explosions = bulk_workspace.get("explosions", [])
            for explosion in explosions:
                # explode field can be a string or a list
                # exploding multiple fields at once avoids
                # combinatorial explosions - this is VERY
                # different from exploding columns one after the other!
                if "explode_field" not in explosion and "explode_fields" not in explosion:
                    self.logger.error("Missing explosion field(s)!")
                    continue
                # we want to be backwards compatible...
                explode_fields = (
                    explosion["explode_field"] if "explode_field" in explosion else explosion["explode_fields"]
                )
                flatten_fields = explosion.get("flatten_fields", [])
                split_string_to_list = explosion.get("split_string_to_list", False)
                list_splitter = explosion.get(
                    "list_splitter",
                    ",",
                )  # don't have None as default!
                self.logger.info(
                    "Starting explosion of bulk workspaces by field(s) -> %s (type -> %s). Size of data frame before explosion -> %s.",
                    explode_fields,
                    type(explode_fields),
                    str(len(data)),
                )
                data.explode_and_flatten(
                    explode_fields=explode_fields,
                    flatten_fields=flatten_fields,
                    make_unique=False,
                    split_string_to_list=split_string_to_list,
                    separator=list_splitter,
                    reset_index=True,
                )
                self.logger.info(
                    "Size of data frame after explosion -> %s.",
                    str(len(data)),
                )

            # Keep only selected rows if filters are specified in bulkWorkspaces.
            # We have this _after_ "explosions" to allow access to subfields as well.
            # We have this _before_ "sorting" and "deduplication" as may keep the wrong
            # rows otherwise (unique / deduplication always keeps the first matching row).
            # We may want to do this outside the bulkDatasource to only
            # filter for bulkDocuments and not for bulkWorkspaces or
            # bulkWorkspaceRelationships:
            filters = bulk_workspace.get("filters", [])
            if filters:
                data.filter(conditions=filters)

            # Sort the data frame if "sort" specified in payload. We may want to do this to have a
            # higher chance that rows with workspace names that may collapse into
            # one name are put into the same partition. This can avoid race conditions
            # between different Python threads.
            sort_fields = bulk_workspace.get("sort", [])
            if sort_fields:
                self.logger.info(
                    "Start sorting of data frame for workspace type -> '%s' based on fields (columns) -> %s...",
                    type_name,
                    str(sort_fields),
                )
                data.sort(sort_fields=sort_fields, inplace=True)
                self.logger.info(
                    "Sorting of data frame for workspace type -> '%s' based on fields (columns) -> %s completed.",
                    type_name,
                    str(sort_fields),
                )

            # Check if duplicate rows for given fields should be removed. It is
            # important to do this after sorting as Pandas always keep the first occurance,
            # so ordering plays an important role in deduplication!
            unique_fields = bulk_workspace.get("unique", [])
            if unique_fields:
                self.logger.info(
                    "Starting deduplication of data frame for workspace type -> '%s' with unique fields -> %s. Size of data frame before deduplication -> %s.",
                    type_name,
                    str(unique_fields),
                    str(len(data)),
                )
                data.deduplicate(unique_fields=unique_fields, inplace=True)
                self.logger.info(
                    "Size of data frame after deduplication -> %s.",
                    str(len(data)),
                )

            # Read name field from payload:
            workspace_name_field = bulk_workspace.get("name", None)
            if not workspace_name_field:
                self.logger.error(
                    "Bulk workspace needs a name field! Skipping to next workspace...",
                )
                success = False
                continue

            # Read optional description field from payload:
            workspace_description_field = bulk_workspace.get("description", None)

            # Find the workspace type with the name given in the payload:
            workspace_type = next(
                (item for item in self._workspace_types if item["name"] == type_name),
                None,
            )
            if workspace_type is None:
                self.logger.error(
                    "Workspace type -> '%s' not found. Skipping to next bulk workspace...",
                    type_name,
                )
                success = False
                continue
            if workspace_type["templates"] == []:
                self.logger.error(
                    "Workspace type -> '%s' does not have templates. Skipping to next bulk workspace...",
                    type_name,
                )
                success = False
                continue

            # check if the template to be used is specified in the payload:
            if "template_name" in bulk_workspace:
                template_name_field = bulk_workspace["template_name"]
                workspace_template = next(
                    (item for item in workspace_type["templates"] if item["name"] == template_name_field),
                    None,
                )
                if workspace_template:  # does this template exist?
                    self.logger.info(
                        "Workspace template -> '%s' has been specified in payload and it does exist.",
                        template_name_field,
                    )
                elif "{" in template_name_field and "}" in template_name_field:
                    self.logger.info(
                        "Workspace template -> '%s' has been specified in payload and contains placeholders, validation cannot be performed.",
                        template_name_field,
                    )
                else:
                    self.logger.error(
                        "Workspace template -> '%s' has been specified in payload but it doesn't exist!",
                        template_name_field,
                    )
                    self.logger.error(
                        "Workspace type -> '%s' has only these templates -> %s.",
                        type_name,
                        workspace_type["templates"],
                    )
                    success = False
                    continue
            # template to be used is NOT specified in the payload - then we just take the first one:
            else:
                workspace_template = workspace_type["templates"][0]
                template_name_field = None
                self.logger.info(
                    "Workspace template has not been specified in payload - taking the first one (%s)...",
                    workspace_template,
                )

            # Read the optional categories payload:
            categories = bulk_workspace.get("categories", None)
            if not categories:
                self.logger.info(
                    "Bulk workspace payload has no category data! Will leave category attributes empty...",
                )

            # Should existing workspaces be updated? No is the default.
            operations = bulk_workspace.get("operations", ["create"])

            self.logger.info(
                "Bulk create workspaces (name field -> %s, type -> '%s') from workspace template -> '%s'. Operations -> %s.",
                workspace_name_field,
                type_name,
                template_name_field,
                str(operations),
            )

            # See if bulkWorkspace definition has a specific thread number
            # otherwise it is read from a global environment variable
            bulk_thread_number = int(
                bulk_workspace.get("thread_number", BULK_THREAD_NUMBER),
            )

            partitions = data.partitionate(bulk_thread_number)

            # Create a list to hold the threads
            threads = []
            results = []

            # Create and start a thread for each partition
            for index, partition in enumerate(partitions, start=1):
                # start a thread executing the process_bulk_workspaces_worker() method below:
                thread = threading.Thread(
                    name=f"{section_name}_{index:02}",
                    target=self.thread_wrapper,
                    args=(
                        self.process_bulk_workspaces_worker,
                        bulk_workspace,
                        partition,
                        workspace_type,
                        template_name_field,
                        workspace_name_field,
                        workspace_description_field,
                        categories,
                        operations,
                        results,
                    ),
                )
                self.logger.info("Starting thread -> '%s'...", str(thread.name))
                thread.start()
                threads.append(thread)
                # Avoid that all threads start at the exact same time with
                # potentially expired cookies that cases race conditions:
                time.sleep(1)
            # end for index, partition in enumerate(partitions, start=1)

            # Wait for all threads to complete
            for thread in threads:
                self.logger.info(
                    "Waiting for thread -> '%s' to complete...",
                    str(thread.name),
                )
                thread.join()
                self.logger.info("Thread -> '%s' has completed.", str(thread.name))

            if "workspaces" not in bulk_workspace:
                bulk_workspace["workspaces"] = {}

            # Print statistics for each thread. In addition,
            # check if all threads have completed without error / failure.
            # If there's a single failure in on of the thread results we
            # set 'success' variable to False.
            results.sort(key=lambda x: x["thread_name"])
            for result in results:
                self.logger.info(
                    "Thread -> '%s' completed with overall status '%s'.",
                    str(result["thread_name"]),
                    "successful" if result["success"] else "failed",
                )
                self.logger.info(
                    "Thread -> '%s' processed %s data rows with %s successful, %s failed, and %s skipped.",
                    str(result["thread_name"]),
                    str(
                        result["success_counter"] + result["failure_counter"] + result["skipped_counter"],
                    ),
                    str(result["success_counter"]),
                    str(result["failure_counter"]),
                    str(result["skipped_counter"]),
                )
                self.logger.info(
                    "Thread -> '%s' created %s workspaces, updated %s workspaces, and deleted %s workspaces.",
                    str(result["thread_name"]),
                    str(result["create_counter"]),
                    str(result["update_counter"]),
                    str(result["delete_counter"]),
                )

                if not result["success"]:
                    success = False
                # Record all generated workspaces. This should allow us
                # to restart in case of failures and avoid trying to
                # create workspaces that have been created before.
                bulk_workspace["workspaces"].update(result["workspaces"])
            self._log_header_callback(
                text="Completed processing of bulk workspaces for -> '{}' using data source -> '{}'".format(
                    type_name,
                    data_source_name,
                ),
                char="-",
            )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._bulk_workspaces,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_categories")
    def process_bulk_categories(
        self,
        row: pd.Series,
        index: str,
        categories: list,
        replacements: list,
    ) -> list:
        """Replace the value placeholders of the bulk category structures.

        The payload placeholders are replaced with values from the Pandas Series (row).
        It also processes value mappings + lookups and special treatment of list and table values.

        Args:
            row (pd.Series):
                The current row in the Pandas data frame.
            index (str):
                The index of the Pandas data frame. Just used here for logging.
            categories (list):
                List of payload categories.
            replacements (list):
                List of replacements.

        Returns:
            list:
                List of categories.

        """

        # Make sure the threads are not changing data structures that
        # are shared between threads. categories is a list of dicts.
        # list and dicts are "mutable" data structures in Python!
        worker_categories = copy.deepcopy(categories)

        # In this first loop we expand table-value attributes into a new
        # list of category / attribute payload. The value of table-value attributes
        # is a list of dictionaries (in a string we evaluate into a Python
        # datastructure)
        worker_categories_expanded = []
        for category_item in worker_categories:
            if "value_type" in category_item and category_item["value_type"] == "table":
                value_field = category_item["value_field"]

                # The following method always returns a string even if the value is actually a list.
                # This is required as we want to support multiple placeholders in one string...
                value = self.replace_bulk_placeholders(
                    input_string=value_field,
                    row=row,
                    index=None,
                    replacements=replacements,
                )
                # If value is empty (probably because placeholders could not be resolved)
                # and if the payload provides an alternative value field we try this:
                if not value and "value_field_alt" in category_item:
                    value = self.replace_bulk_placeholders(
                        input_string=category_item["value_field_alt"],
                        row=row,
                        index=None,
                        replacements=replacements,
                    )
                if not value:
                    self.logger.info(
                        "Table-type attribute -> '%s' is empty (value field -> '%s'). Cannot parse table. Skipping...",
                        category_item.get("name", ""),
                        value_field,
                    )
                    continue

                try:
                    value_table = literal_eval(value)
                except (SyntaxError, ValueError):
                    self.logger.error(
                        "Cannot parse table-type attribute -> '%s'; value field -> '%s'",
                        category_item.get("name", ""),
                        value_field,
                    )
                    continue

                if not isinstance(value_table, list):
                    self.logger.error(
                        "Table-type value -> '%s' requires a list of dictionaries!",
                        value_field,
                    )
                    continue

                """
                Get the mapping of the loader generated columns in the Data Frame to the
                attribute names in the target OTCS category. If no mapping
                is in the payload, then it is assumed that the category
                attribute names are identical to the column names in the Data Frame

                Example mapping:

                attribute_mapping = {
                  "Application": "u_product_model",
                  "Version": "u_version_name"
                }
                """

                attribute_mapping = category_item.get("attribute_mapping", None)

                row_index = 1
                for value_row in value_table:
                    if not isinstance(value_row, dict):
                        self.logger.error(
                            "Illegal data type for table row -> %s. Expected 'dict', got -> '%s' with value -> %s",
                            str(row_index),
                            type(value_row),
                            value_row,
                        )
                        continue
                    for key, value in value_row.items():
                        attribute = {}
                        attribute["name"] = category_item.get("name", "")
                        attribute["set"] = category_item.get("set", "")
                        attribute["row"] = row_index
                        # check if we have a mapping for this attribute in the payload:
                        if attribute_mapping and key in attribute_mapping:
                            attribute["attribute"] = attribute_mapping[key]
                        else:
                            attribute["attribute"] = key
                        # For tables values can be None if the number of
                        # list items in the source columns are not equal.
                        # To avoid the warning below we set the value to empty string
                        # if it is None:
                        attribute["value"] = value if value is not None else ""
                        worker_categories_expanded.append(attribute)
                    row_index += 1
            # end if "value_type" in category_item and category_item["value_type"] == "table"
            else:
                worker_categories_expanded.append(category_item)
        # end for category_item in worker_categories

        # this loop generates a "value" for each
        # "value_field". "value_field" may also contain lists
        # that are either delimited by [...] or by a "value_type" with value "list"
        for category_item in worker_categories_expanded:
            if "attribute" not in category_item:
                self.logger.error(
                    "Category item -> %s is missing the attribute field!",
                    category_item,
                )
                continue

            # per default the value is in the "value" item:
            value = category_item.get("value", None)

            # is there a value replacement for the current attribute?
            if "value_field" in category_item:
                value_field = category_item["value_field"]
                # If no row is specified we set the set index to None and not 0 to handle
                # cases where we have multi-value attributes that take a list as parameter
                set_index = int(category_item["row"]) - 1 if "row" in category_item else None

                # this method always returns a string even if the value is
                # actually a list.
                value = self.replace_bulk_placeholders(
                    input_string=value_field,
                    row=row,
                    index=set_index,
                    replacements=replacements,
                )
                # If value is empty (probably because placeholders could not be resolved)
                # and if the payload provides an alternative value field we try this:
                if not value and "value_field_alt" in category_item:
                    value = self.replace_bulk_placeholders(
                        input_string=category_item["value_field_alt"],
                        row=row,
                        index=set_index,
                        replacements=replacements,
                    )
            else:
                value_field = None

            # if we don't have a value now, then there's an issue:
            if value is None:
                value = ""
                self.logger.warning(
                    "Category item needs either a value or value_field! Skipping attribute -> '%s'",
                    category_item["attribute"],
                )

            # We have an empty string value (this is different from None!)
            if value == "":
                category_item["value"] = value
                # We can continue as any further processing (below) does not make sense for an empty string value:
                continue

            # This variable should only be true if we don't have
            # a native python string but a delimiter separated
            # value list in a string, e.g. "a, b, c" or "a | b | c" or "x;y;z":
            is_list_in_string = False

            # The data source loader may have written a real python list into the value
            # In this case the string value includes square brackets [...]
            # We only do  this if the actual type of the value is string and the
            # proposed value (value_type) is not string:
            if (
                isinstance(value, str)  # it is a string
                and value.startswith("[")  # it starts with a list indicator character
                and value.endswith("]")  # it ends with a list indicator character
                and category_item.get("value_type", "")
                != "string"  # it is NOT explicitly stated it should remain a string
            ):
                # Remove the square brackets and declare it is a list!
                try:
                    value = literal_eval(value)
                except (SyntaxError, ValueError) as e:
                    self.logger.warning(
                        "Cannot directly parse list-type attribute; value field -> %s; error -> %s. Try string processing...",
                        value_field,
                        str(e),
                    )
                    self.logger.warning(
                        "Value string -> %s has [...] - remove brackets and interpret as delimiter separated values in a string...",
                        value,
                    )
                    # In this failure case we try to remove the square brackets and hope the inner part
                    # can be treated as a string of values delimited with a delimiter (e.g. comma or semicolon)
                    value = value.strip("[]")
                    is_list_in_string = True

            # Handle this special case where we get a string that actually represents a date time format and convert it.
            # We only do this if the attribute has a "value_type" of "datetime" in the payload:
            if category_item.get("value_type", "string") == "datetime":
                old_value = value
                try:
                    date_obj = parse(value)
                    value = datetime.strftime(date_obj, "%Y-%m-%dT%H:%M:%SZ")
                except (OverflowError, ValueError):
                    self.logger.error(
                        "Cannot convert value -> '%s' into a date for attribute -> '%s'. Value will be empty.",
                        old_value,
                        category_item["attribute"],
                    )
                    value = ""
                else:
                    self.logger.debug(
                        "Attribute -> %s is declared in payload to be a datetime (convert format). Converted it from -> %s to -> %s",
                        category_item.get("attribute"),
                        old_value,
                        value,
                    )

            # Handle special case where we get a string that actually represents a list but is
            # not yet a Python list but a string. This requires that value_type == "list". The list splitter
            # can be specified via "list_splitter" in the payload:
            # Now we try to convert the string to a Python list:
            if (category_item.get("value_type", "string") == "list" or is_list_in_string) and isinstance(value, str):
                # we split the string after splitter characters:
                list_splitter = category_item.get("list_splitter", ";,")
                self.logger.info(
                    "Value -> '%s' is declared in payload to be a list but it is provided as a string. Splitting it after these characters -> '%s'.",
                    value,
                    list_splitter,
                )

                # Escape the split characters to ensure they are treated literally in the regex pattern
                escaped_splitter = re.escape(list_splitter)

                # Construct the regex pattern dynamically
                pattern = rf"[{escaped_splitter}]\s*"

                # Split the value string at the defined splitter characters:
                elements = re.split(pattern, value)

                # Remove the quotes around each element
                elements = [element.strip("'") for element in elements]
                value = elements
                self.logger.info(
                    "Found list for a multi-value category attribute -> '%s' from field -> '%s' in data row -> %s. Value -> '%s'.",
                    category_item["attribute"],
                    value_field,
                    index,
                    str(value),
                )

            # now we check if there's a data lookup configured in the payload:
            lookup_data_source = category_item.get("lookup_data_source", None)
            # Do we want to drop / clear values that fail to lookup?
            drop_value = category_item.get("lookup_data_failure_drop", False)

            if lookup_data_source:
                self.logger.info(
                    "Found lookup data source -> '%s' for attribute -> '%s'. Processing...",
                    lookup_data_source,
                    category_item["attribute"],
                )
                if not isinstance(value, list):
                    # value is a single value and not a list:
                    (_, synonym) = self.process_bulk_workspaces_synonym_lookup(
                        data_source_name=lookup_data_source,
                        workspace_name_synonym=value,
                    )
                    if synonym:
                        self.logger.info(
                            "Found synonym -> '%s' for attribute -> '%s' and value -> '%s' in data source -> '%s'.",
                            synonym,
                            category_item["attribute"],
                            value,
                            lookup_data_source,
                        )
                        value = synonym
                    elif drop_value:
                        self.logger.warning(
                            "Cannot lookup the value for attribute -> '%s' and value -> '%s' in data source -> '%s'. Clear existing value.",
                            category_item["attribute"],
                            value,
                            lookup_data_source,
                        )
                        # Clear the value:
                        value = ""
                    else:
                        self.logger.warning(
                            "Cannot lookup the value for attribute -> '%s' and value -> '%s' in data source -> '%s'. Keep existing value.",
                            category_item["attribute"],
                            value,
                            lookup_data_source,
                        )
                # end if not isinstance(value, list)
                else:
                    # value is a list - so we apply the lookup to each item:
                    # Iterate backwards to avoid index issues while popping items:
                    for i in range(len(value) - 1, -1, -1):
                        # Make sure the value does not have leading or trailing spaces:
                        value[i] = value[i].strip()
                        (_, synonym) = self.process_bulk_workspaces_synonym_lookup(
                            data_source_name=lookup_data_source,
                            workspace_name_synonym=value[i],
                            workspace_type=None,  # we don't need the workspace ID, just the workspace name
                        )
                        if synonym:
                            self.logger.info(
                                "Found synonym -> '%s' for attribute -> '%s' and value -> '%s' in data source -> '%s'.",
                                synonym,
                                category_item["attribute"],
                                value[i],
                                lookup_data_source,
                            )
                            value[i] = synonym
                        elif drop_value:
                            self.logger.warning(
                                "Cannot lookup the value -> '%s' for attribute -> '%s' in data source -> '%s'! Drop existing value from list.",
                                value[i],
                                category_item["attribute"],
                                lookup_data_source,
                            )
                            # Remove the list item we couldn't lookup as drop_value is True:
                            value.pop(i)
                        else:
                            self.logger.warning(
                                "Cannot lookup the value -> '%s' for attribute -> '%s' in data source -> '%s'! Keep existing value.",
                                value[i],
                                category_item["attribute"],
                                lookup_data_source,
                            )
            # end if lookup_data_source

            # If value is a list then we have a multi-value attribute.
            # We now want to make sure that we don't have duplicates in
            # the value list:
            if isinstance(value, list) and len(value) != len(set(value)):
                self.logger.info(
                    "The multi-value attribute -> '%s' has duplicates in its values -> %s.",
                    category_item["attribute"],
                    value,
                )
                # Remove duplicates from the list while preserving order.
                # Uses `dict.fromkeys()` since dictionaries maintain insertion order (Python 3.7+).
                # This ensures that only the first occurrence of each element is kept.
                value = list(dict.fromkeys(value))
                self.logger.info("The attribute values got de-duplicated to -> %s.", value)

            # now we check if there's a values mapping configured in the payload.
            # This is a dictionary for keys being the original values and
            # values being the mapped values. This makes most sense for
            # values with a limited / fixed domain of values. Example
            # `value_mapping = { "TS": "Tropical Storm", "HU": "Hurricane"}`
            value_mapping = category_item.get("value_mapping", None)
            if value_mapping:
                if not isinstance(value, list):
                    # value is a single value and not a list:
                    if value in value_mapping:
                        self.logger.info(
                            "Found value mapping for attribute -> '%s' from value -> '%s' to value -> '%s'.",
                            category_item["attribute"],
                            value,
                            value_mapping[value],
                        )
                        value = value_mapping[value]
                else:
                    # value is a list - so we apply the lookup to each item:
                    # Iterate backwards to avoid index issues while popping items:
                    for i in range(len(value) - 1, -1, -1):
                        # Make sure the value does not have leading or trailing spaces:
                        value[i] = value[i].strip()

                        if value[i] in value_mapping:
                            self.logger.info(
                                "Found value mapping for attribute -> '%s' from value -> '%s' to value -> '%s'",
                                category_item["attribute"],
                                value[i],
                                value_mapping[value[i]],
                            )
                            value[i] = value_mapping[value[i]]

            # If value is a list then we have a multi-value attribute.
            # If "sort_multi_values" is specified in payload and evaluate to True
            # we sort the list alphabetically (note that numbers are not necessarily sorted numerically):
            if isinstance(value, list) and len(value) > 1 and category_item.get("sort_multi_values", False):
                value.sort(key=str)
                self.logger.info(
                    "Sorting the values of multi-value attribute -> '%s' to -> %s.",
                    category_item["attribute"],
                    value,
                )

            if value_field:
                self.logger.debug(
                    "Reading category -> '%s', attribute -> '%s' from field -> '%s' in data row -> %s. Value -> '%s'.",
                    category_item["name"],
                    category_item["attribute"],
                    value_field,
                    index,
                    str(value),
                )
            else:
                self.logger.debug(
                    "Setting category -> '%s', attribute -> '%s' to value -> '%s'.",
                    category_item["name"],
                    category_item["attribute"],
                    str(value),
                )
            category_item["value"] = value
        # end for category_item in worker_categories_expanded

        # Cleanup categories_payload to remove empty rows of sets:
        rows_to_remove = {}
        for attribute in worker_categories_expanded:
            if attribute.get("set") is not None and attribute.get("row") is not None:
                set_name = attribute["set"]
                row_number = attribute["row"]
                value = attribute["value"]

                # If value is empty, mark it for removal:
                if not value or value == [""]:  # Treat empty strings or None as empty
                    # The following if statement is important to not mark a set row
                    # as True = removable that has been set to False in the else case below!
                    if (set_name, row_number) not in rows_to_remove:
                        rows_to_remove[(set_name, row_number)] = True
                else:
                    # If any value in the row is not empty, mark the row as not removable.
                    # This may change the marking that have been applied in the if case
                    # above!
                    rows_to_remove[(set_name, row_number)] = False

        # Keep only the rows marked as True (empty rows to be removed)
        rows_to_remove = {k: v for k, v in rows_to_remove.items() if v is True}

        if rows_to_remove:
            self.logger.debug("Empty rows to remove from sets -> %s.", rows_to_remove)
        else:
            self.logger.debug("No empty rows to remove from sets.")

        cleaned_categories = [
            item
            for item in worker_categories_expanded
            if "set" not in item or "row" not in item or (item["set"], item["row"]) not in rows_to_remove
        ]

        return cleaned_categories

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_classification_assignments")
    def process_bulk_classification_assignments(
        self,
        row: pd.Series,
        index: str,
        classifications: list,
        replacements: list,
    ) -> list:
        """Replace the value placeholders of the bulk classification structures.

        The payload placeholders are replaced with values from the Pandas Series (row).

        Args:
            row (pd.Series):
                The current row in the Pandas data frame.
            index (str):
                The index of the Pandas data frame. Just used here for logging.
            classifications (list):
                List of payload classifications.
            replacements (list):
                List of replacements.

        Returns:
            list:
                List of classification IDs.

        """

        result_list = []

        for classification in classifications:
            # Do we have a classification path?
            if isinstance(classification, list):
                # Replace placeholders in the list. As list is a mutable data type,
                # the replacement happens in-place:
                self.replace_bulk_placeholders_list(input_list=classification, row=row, replacements=replacements)
                class_node = self._otcs_frontend.get_node_by_volume_and_path(
                    volume_type=self._otcs.VOLUME_TYPE_CLASSIFICATION_VOLUME,
                    path=classification,
                )
            elif isinstance(classification, string):
                nickname = self.replace_bulk_placeholders(
                    input_string=classification,
                    row=row,
                    replacements=replacements,
                )
                class_node = self._otcs_frontend.get_node_from_nickname(nickname=nickname)

            class_node_id = self._otcs.get_result_value(
                response=class_node,
                key="id",
            )
            if class_node_id:
                result_list.append(class_node_id)

        if result_list:
            self.logger.debug(
                "Found classifications for data row -> %s. Value -> %s",
                index,
                str(result_list),
            )

        return result_list

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspaces_worker")
    def process_bulk_workspaces_worker(
        self,
        bulk_workspace: dict,
        partition: pd.DataFrame,
        workspace_type: dict,
        template_name_field: str | None,
        workspace_name_field: str,
        workspace_description_field: str,
        categories: list | None = None,
        operations: list | None = None,
        results: list | None = None,
    ) -> None:
        """Thread worker to create workspaces in bulk.

        Each worker thread gets a partition of the rows that include
        the data required for the workspace creation.

        Args:
            bulk_workspace (dict):
                The payload of the bulkWorkspace.
            partition (pd.DataFrame):
                Data partition with rows to process.
            template_id (int):
                ID of the workspace template to use.
            workspace_type (dict):
                Workspace type data.
            template_name_field (str | None):
                Field where the template name is stored.
            workspace_name_field (str):
                Field where the workspace name is stored.
            workspace_description_field (str):
                Field where the workspace description is stored.
            categories (list, optional):
                List of category dictionaries.
            operations (list, optional):
                Defines which operations should be applyed on workspaces.
                Possible values are "create", "update", "delete", "recreate".
            results (list, optional):
                A mutable list of thread results.

        """

        thread_id = threading.get_ident()
        thread_name = threading.current_thread().name

        t = trace.get_current_span()
        t.set_attributes(
            {
                "thread.id": thread_id,
                "thread.name": thread_name,
            }
        )

        self.logger.info(
            "Start working on data frame partition of size -> %s to bulk create workspaces...",
            str(len(partition)),
        )

        # Avoid linter warnings - so make parameter default None while we
        # actually want ["create"] to be the default:
        if operations is None:
            operations = ["create"]

        result = {}
        result["thread_id"] = thread_id
        result["thread_name"] = thread_name
        result["success_counter"] = 0
        result["failure_counter"] = 0
        result["skipped_counter"] = 0
        result["create_counter"] = 0
        result["update_counter"] = 0
        result["delete_counter"] = 0
        result["workspaces"] = {}
        result["success"] = True

        # Check if workspaces have been processed before, e.i. testing
        # if a "workspaces" key exists and if it is pointing to a non-empty list.
        # Additionally we check that workspace updates are not enforced:
        if (
            bulk_workspace.get("workspaces")
            and "update" not in operations
            and "delete" not in operations
            and "recreate" not in operations
        ):
            existing_workspaces = bulk_workspace["workspaces"]
            self.logger.info(
                "Found %s already processed workspaces. Try to complete the job...",
                str(len(existing_workspaces)),
            )
        else:
            existing_workspaces = {}

        # See if external creation and modification dates are in the payload data:
        external_modify_date_field = bulk_workspace.get("external_modify_date")
        external_create_date_field = bulk_workspace.get("external_create_date")

        # See if we have a key field to uniquely identify an existing workspace:
        key_field = bulk_workspace.get("key")

        # Get dictionary of replacements for bulk workspace creations
        # this we will be used of all places data is read from the
        # data frame. Each dictionary item has the field name as the
        # dictionary key and a list of regular expressions as dictionary value
        replacements = bulk_workspace.get("replacements")

        # In case the name cannot be resolved we allow to
        # specify an alternative name field in the payload.
        name_field_alt = bulk_workspace.get("name_alt")

        # In case the description cannot be resolved we allow to
        # specify an alternative description field in the payload.
        description_field_alt = bulk_workspace.get("description_alt")

        # Fetch the nickname field from the payload (if it is specified):
        nickname_field = bulk_workspace.get("nickname")

        # In case the nickname cannot be resolved we allow to
        # specify an alternative nickname field in the payload.
        nickname_field_alt = bulk_workspace.get("nickname_alt")

        # Nicknames are very limited in terms of allowed characters.
        # For nicknames we need additional regexp as we need to
        # replace all non-alphanumeric, non-space characters with ""
        # We also preserve hyphens in the first step to replace
        # them below with underscores. This is important to avoid
        # that different spellings of names produce different nicknames.
        # We want spellings with spaces match spellings with hyphens.
        # For this the workspace names have a regexp "-| " in the payload.
        nickname_additional_regex_list = [r"[^\w\s-]"]

        # Classification can either be provided by classification pathes
        # or by nicknames:
        classification_pathes = bulk_workspace.get("classification_pathes", [])
        classification_nicknames = bulk_workspace.get("classification_nicknames", [])

        total_rows = len(partition)

        # Process all rows in the partition that was given to this thread:
        for index, row in partition.iterrows():
            with tracer.start_as_current_span("process_bulk_workspaces_worker-workspace") as t:
                # Calculate percentage of completion:
                percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

                self.logger.info(
                    "Processing data row -> %s for bulk workspace creation (%.2f %% complete)...",
                    str(index),
                    percent_complete,
                )

                # Clear variables to esure clean state for each row:
                workspace_id = None

                workspace_template = None
                if template_name_field is None:
                    workspace_template = workspace_type["templates"][0]

                else:
                    workspace_template_name = self.replace_bulk_placeholders(
                        input_string=template_name_field,
                        row=row,
                        replacements=replacements,
                    )

                    workspace_template = next(
                        (item for item in workspace_type["templates"] if item["name"] == workspace_template_name),
                        None,
                    )

                if workspace_template is None:
                    self.logger.error(
                        "Workspace template -> '%s' has been specified in payload but it doesn't exist!",
                        workspace_template_name,
                    )
                    self.logger.error(
                        "Workspace type -> '%s' has only these templates -> %s",
                        workspace_type["name"],
                        workspace_type["templates"],
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue

                template_id = workspace_template["id"]
                template_name = workspace_template["name"]
                workspace_type_id = workspace_type["id"]

                # Determine the workspace name:
                workspace_name = self.replace_bulk_placeholders(
                    input_string=workspace_name_field,
                    row=row,
                    replacements=replacements,
                )
                # If we cannot get the workspace name from the
                # workspace_name_field we try the alternative name field
                # (if specified in payload via "name_alt"):
                if not workspace_name and name_field_alt:
                    self.logger.debug(
                        "Row -> %s does not have the data to resolve the placeholders in workspace name -> %s! Trying alternative name field -> %s...",
                        str(index),
                        workspace_name_field,
                        name_field_alt,
                    )
                    workspace_name = self.replace_bulk_placeholders(
                        input_string=name_field_alt,
                        row=row,
                        replacements=replacements,
                    )

                if not workspace_name:
                    self.logger.warning(
                        "Row -> %s does not have the required data to resolve -> %s for the workspace name!%s",
                        str(index),
                        workspace_name_field,
                        " There's no alternative field name given in the payload (via 'name_alt')."
                        if not name_field_alt
                        else " The alternative field -> '{}' could not be resolved either!".format(
                            name_field_alt,
                        ),
                    )
                    result["skipped_counter"] += 1
                    continue

                # Cleanse the workspace name (allowed characters, maximum length):
                workspace_name = OTCS.cleanse_item_name(workspace_name)

                # Check if workspace has been created before (either in this run
                # or in a former run of the customizer):
                if (
                    workspace_name in existing_workspaces  # processed in former run?
                    or workspace_name in result["workspaces"]  # processed in current run?
                ):
                    self.logger.info(
                        "Workspace -> '%s' does already exist. Skipping...",
                        workspace_name,
                    )
                    result["skipped_counter"] += 1
                    continue

                # Determine the description field:
                if workspace_description_field:
                    description = self.replace_bulk_placeholders(
                        input_string=workspace_description_field,
                        row=row,
                    )
                    # If we cannot get the description from the
                    # workspace_description_field we try the alternative name field
                    # (if specified in payload via "description_alt"):
                    if not description and description_field_alt:
                        self.logger.debug(
                            "Row -> %s does not have the data to resolve the placeholders in workspace description -> %s! Trying alternative description field -> %s...",
                            str(index),
                            workspace_name_field,
                            description_field_alt,
                        )
                        description = self.replace_bulk_placeholders(
                            input_string=description_field_alt,
                            row=row,
                        )
                else:
                    description = ""

                # Create a copy of the mutable operations list as we may
                # want to modify it per row:
                row_operations = list(operations)

                # Check if all data conditions to create the workspace are met
                conditions = bulk_workspace.get("conditions")
                if conditions:
                    evaluated_condition = self.evaluate_conditions(
                        conditions=conditions,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_condition:
                        self.logger.info(
                            "Condition for bulk workspace row -> %s not met. Skipping row for workspace creation...",
                            str(index),
                        )
                        result["skipped_counter"] += 1
                        continue

                # Check if all data conditions to create or recreate the workspace are met:
                if "create" in row_operations or "recreate" in row_operations:
                    conditions_create = bulk_workspace.get("conditions_create")
                    if conditions_create:
                        evaluated_conditions_create = self.evaluate_conditions(
                            conditions=conditions_create,
                            row=row,
                            replacements=replacements,
                        )
                        if not evaluated_conditions_create:
                            self.logger.info(
                                "Create condition for bulk workspace row -> %s not met. Excluding create operation for current row...",
                                str(index),
                            )
                            if "create" in row_operations:
                                row_operations.remove("create")
                            if "recreate" in row_operations:
                                row_operations.remove("recreate")
                    elif (
                        "recreate" in row_operations
                    ):  # we still create and recreate without conditions_create. But give a warning for 'recreate' without condition.
                        self.logger.warning(
                            "No create condition provided but 'recreate' operation requested. This will recreate all existing workspaces!",
                        )

                # Check if all data conditions to delete the workspace are met:
                if "delete" in row_operations:
                    conditions_delete = bulk_workspace.get("conditions_delete")
                    if conditions_delete:
                        evaluated_conditions_delete = self.evaluate_conditions(
                            conditions=conditions_delete,
                            row=row,
                            replacements=replacements,
                        )
                        if not evaluated_conditions_delete:
                            self.logger.info(
                                "Delete condition for bulk workspace row -> %s not met. Excluding delete operation for current row...",
                                str(index),
                            )
                            row_operations.remove("delete")
                    else:  # without delete_conditions we don't delete!!
                        self.logger.warning(
                            "Delete operation requested for bulk workspaces but conditions for deletion are missing! (specify 'conditions_delete')!",
                        )
                        row_operations.remove("delete")

                # Check if all data conditions to delete the workspace are met:
                if "update" in row_operations:
                    conditions_update = bulk_workspace.get("conditions_update")
                    if conditions_update:
                        evaluated_conditions_update = self.evaluate_conditions(
                            conditions=conditions_update,
                            row=row,
                            replacements=replacements,
                        )
                        if not evaluated_conditions_update:
                            self.logger.info(
                                "Update condition for bulk workspace row -> %s not met. Excluding update operation for current row...",
                                str(index),
                            )
                            row_operations.remove("update")

                # Determine the external modification date (if any):
                if external_modify_date_field:
                    external_modify_date = self.replace_bulk_placeholders(
                        input_string=external_modify_date_field,
                        row=row,
                    )
                else:
                    external_modify_date = None

                # Determine the external creation date (if any):
                if external_create_date_field:
                    external_create_date = self.replace_bulk_placeholders(
                        input_string=external_create_date_field,
                        row=row,
                    )
                else:
                    external_create_date = None

                # Determine the key field (if any):
                key = self.replace_bulk_placeholders(input_string=key_field, row=row) if key_field else None

                # check if workspace with this nickname does already exist.
                # we also store the nickname to assign it to the new workspace:
                found_workspace_name = None
                found_workspace_id = None
                if nickname_field:
                    nickname = self.replace_bulk_placeholders(
                        input_string=nickname_field,
                        row=row,
                        replacements=replacements,
                        additional_regex_list=nickname_additional_regex_list,
                    )
                    # If we cannot get the nickname from the
                    # workspace_nickname_field we try the alternative nickname field
                    # (if specified in payload via "nickname_alt"):
                    if not nickname and nickname_field_alt:
                        nickname = self.replace_bulk_placeholders(
                            input_string=nickname_field_alt,
                            row=row,
                            replacements=replacements,
                            additional_regex_list=nickname_additional_regex_list,
                        )

                    # Nicknames for sure should not have leading or trailing spaces:
                    nickname = nickname.strip()
                    # Nicknames for sure are not allowed to include spaces:
                    nickname = nickname.replace(" ", "_")
                    # We also want to replace hyphens with underscores
                    # to make sure that workspace name spellings with
                    # spaces and with hyphens are mapped to the same
                    # workspace nicknames (aligned with the workspace names
                    # that have a regexp rule for this in the payload):
                    nickname = nickname.replace("-", "_")
                    nickname = nickname.replace("___", "_")
                    nickname = nickname.lower()
                    response = self._otcs_frontend.get_node_from_nickname(nickname=nickname)
                    if response:
                        found_workspace_name = self._otcs_frontend.get_result_value(
                            response=response,
                            key="name",
                        )
                        found_workspace_id = self._otcs_frontend.get_result_value(
                            response=response,
                            key="id",
                        )
                        if found_workspace_name != workspace_name:
                            self.logger.warning(
                                "Clash of nicknames for -> '%s' and -> '%s' (%s)! We will not create the workspace but allow for updates and deletes",
                                workspace_name,
                                found_workspace_name,
                                found_workspace_id,
                            )
                            # Remove 'create' from row operations as it would produce errors about uniqueness of nicknames.
                            if "create" in row_operations:
                                row_operations.remove("create")
                        # Only skip, if workspace 'update' or 'delete' is NOT requested:
                        elif "update" not in row_operations and "delete" not in row_operations:
                            self.logger.info(
                                "Workspace -> '%s' with nickname -> '%s' does already exist (found -> '%s'). No update or delete operations requested or allowed. Skipping...",
                                workspace_name,
                                nickname,
                                found_workspace_name,
                            )
                            result["skipped_counter"] += 1
                            continue
                # end if nickname_field
                else:
                    nickname = None

                t.add_event(name="Preparations done.", timestamp=time.time_ns())

                # Based on the evaluation of conditions_create, conditions_delete,
                # and conditions_update we could end up with an empty operations list.
                # In such cases we skip the further processing of this row:
                if not row_operations:
                    self.logger.info(
                        "Skip workspace -> '%s' as row operations is empty (no create, update, delete or recreate). This may be because conditions_create, conditions_delete, and conditions_update have been evaluated to false for this row.",
                        workspace_name,
                    )
                    result["skipped_counter"] += 1
                    continue

                self.logger.info(
                    "Bulk process workspace -> '%s' using effective operations -> %s...",
                    workspace_name,
                    str(row_operations),
                )

                # Check if the workspace does already exist.
                # First we try to look up the workspace by a unique
                # key that does not change over time.
                # For this we expect a "key" value to be defined for the
                # bulkWorkspace and one of the category / attribute item
                # to be marked with "is_key" = True. If we don't find the
                # workspace via key we try via name and type.
                self.logger.info(
                    "Check if workspace -> '%s' does already exist...",
                    workspace_name,
                )
                # Initialize variables - this is important!
                workspace_old_name = None
                workspace_id = None
                handle_name_clash = False
                workspace_modify_date = None
                parent_id = None

                # We have 4 cases to differentiate:

                # 1. key given + key found = name irrelevant, item exist
                # 2. key given + key not found = if name exist it is a name clash
                # 3. no key given + name found = item exist
                # 4. no key given + name not found = item does not exist

                # We are NOT trying to compensate for a failed key lookup with a name lookup!
                # Names are only relevant if no key is in payload!

                if key:
                    # if we have a key we need to also know which category attribute is
                    # storing the value for the key:
                    key_attribute = next(
                        (cat_attr for cat_attr in categories if cat_attr.get("is_key", False) is True),
                        None,
                    )
                    if not key_attribute:
                        self.logger.error(
                            "Bulk Workspace has key -> '%s' defined but none of the category attributes is marked as key ('is_key' is missing)!",
                            key,
                        )
                        result["success"] = False
                        result["failure_counter"] += 1
                        continue
                    cat_name = key_attribute.get("name", None)
                    att_name = key_attribute.get("attribute", None)
                    set_name = key_attribute.get("set", None)

                    # determine where workspaces of this type typically reside (IMPORTANT: this
                    # may return None if no instances of this workspace type exist!):
                    parent_id = self._otcs_frontend.get_workspace_type_location(
                        type_id=workspace_type_id,
                    )
                    if parent_id:
                        # Try to find the node that has the given key attribute value:
                        response = self._otcs_frontend.lookup_nodes(
                            parent_node_id=parent_id,
                            category=cat_name,
                            attribute=att_name,
                            attribute_set=set_name,
                            value=key,
                        )
                        workspace_id = self._otcs_frontend.get_result_value(
                            response=response,
                            key="id",
                        )
                    else:
                        # if we couldn't determine the parent ID this means there are
                        # now workspace instances for this workspace type. Then we set
                        # workspace_id = None and let the code go into the else case below:
                        workspace_id = None

                    if workspace_id:
                        # Case 1: key given + key found = name irrelevant, item exist
                        workspace_old_name = self._otcs_frontend.get_result_value(
                            response=response,
                            key="name",
                        )
                        self.logger.info(
                            "Found existing workspace -> %s (%s) in folder with ID -> %s using key value -> '%s', category -> '%s', and attribute -> '%s'.",
                            workspace_old_name,
                            workspace_id,
                            parent_id,
                            key,
                            cat_name,
                            att_name,
                        )
                        if workspace_old_name != workspace_name:
                            self.logger.info(
                                "Existing workspace has the old name -> '%s' which is different from the new name -> '%s'.",
                                workspace_old_name,
                                workspace_name,
                            )
                        else:
                            workspace_old_name = None
                        # We get the modify date of the existing workspace.
                        workspace_modify_date = self._otcs_frontend.get_result_value(
                            response=response,
                            key="modify_date",
                        )
                    else:
                        # Case 2: key given + key not found = if name exist it is a name clash
                        self.logger.info(
                            "Couldn't find existing workspace with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                            key,
                            cat_name,
                            att_name,
                            parent_id,
                        )
                        handle_name_clash = True
                    # end if key_attribute
                # end if key
                else:
                    # If we haven't a key we try by type + name
                    response = self._otcs_frontend.get_workspace_by_type_and_name(
                        type_id=workspace_type_id,
                        name=workspace_name,
                    )
                    workspace_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                    if workspace_id:
                        # Case 3: no key given + name found = item exist
                        self.logger.info(
                            "Found existing workspace -> '%s' (%s) with type ID -> %s.",
                            workspace_name,
                            workspace_id,
                            workspace_type_id,
                        )
                        # We get the modify date of the existing workspace.
                        workspace_modify_date = self._otcs_frontend.get_result_value(
                            response=response,
                            key="modify_date",
                        )
                    else:
                        # Case 4: no key given + name not found = item does not exist
                        self.logger.info(
                            "No existing workspace with name -> '%s' and type ID -> %s.",
                            workspace_name,
                            workspace_type_id,
                        )
                        # Check if we found an existing workspace by the same nickname:
                        if found_workspace_id and found_workspace_name:
                            self.logger.info(
                                "But there's a workspace -> '%s' (%s) that has a matching nickname -> '%s'. Using this workspace instead...",
                                found_workspace_name,
                                found_workspace_id,
                                nickname,
                            )
                            workspace_id = found_workspace_id

                # Check if we want to recreate an existing workspace
                # then we handle the "delete" part of "recreate" first:
                if workspace_id and "recreate" in row_operations:
                    response = self._otcs_frontend.delete_node(
                        node_id=workspace_id,
                        purge=True,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to recreate existing workspace -> '%s' (%s) with type ID -> %s! Delete failed.",
                            (workspace_name if workspace_old_name is None else workspace_old_name),
                            workspace_id,
                            workspace_type_id,
                        )
                        result["success"] = False
                        result["failure_counter"] += 1
                        continue
                    result["delete_counter"] += 1
                    self.logger.info(
                        "Deleted existing workspace -> '%s' (%s) as part of the recreate operation.",
                        (workspace_name if workspace_old_name is None else workspace_old_name),
                        workspace_id,
                    )
                    workspace_id = None

                # Check if workspace does not exists - then we create a new workspace
                # if this is requested ("create" or "recreate" value in operations list in payload)
                if not workspace_id and ("create" in row_operations or "recreate" in row_operations):
                    # for Case 2 (we looked up the workspace by key but could have name collisions)
                    # we need to see if we have a name collision:
                    if handle_name_clash and parent_id:
                        response = self._otcs_frontend.check_node_name(
                            parent_id=parent_id,
                            node_name=workspace_name,
                        )
                        # result is a list of names that clash - if it is empty we have no clash
                        if response["results"]:
                            # We add the suffix with the key which should be unique:
                            self.logger.warning(
                                "Workspace -> '%s' does already exist in folder with ID -> %s and we need to handle the name clash by using name -> '%s'",
                                workspace_name,
                                parent_id,
                                workspace_name + " (" + key + ")",
                            )
                            workspace_name = workspace_name + " (" + key + ")"

                    # If category data is in payload we substitute
                    # the values with data from the current data row:
                    if categories:
                        # Make sure the threads are not changing data structures that
                        # are shared between threads. categories is a list of dicts.
                        # list and dicts are "mutable" data structures in Python!
                        worker_categories = self.process_bulk_categories(
                            row=row,
                            index=index,
                            categories=categories,
                            replacements=replacements,
                        )
                        workspace_category_data = self.prepare_workspace_create_form(
                            categories=worker_categories,
                            template_id=template_id,
                        )
                        if not workspace_category_data:
                            self.logger.error(
                                "Failed to prepare the category data for new workspace -> '%s'!",
                                workspace_name,
                            )
                            result["success"] = False
                            result["failure_counter"] += 1
                            continue  # for index, row in partition.iterrows()
                    else:
                        workspace_category_data = {}

                    self.logger.info(
                        "Bulk create workspace -> '%s'...",
                        workspace_name,
                    )

                    if classification_nicknames or classification_pathes:
                        classification_ids = self.process_bulk_classification_assignments(
                            row=row,
                            index=index,
                            classifications=classification_pathes + classification_nicknames,
                            replacements=replacements,
                        )
                    else:
                        classification_ids = None

                    # Create the workspace with all provided information:
                    response = self._otcs_frontend.create_workspace(
                        workspace_template_id=template_id,
                        workspace_name=workspace_name,
                        workspace_description=description,
                        workspace_type=workspace_type_id,
                        category_data=workspace_category_data,
                        classifications=classification_ids,
                        external_create_date=external_create_date,
                        external_modify_date=external_modify_date,
                        show_error=False,
                    )
                    if response is None:
                        # Potential race condition: see if the workspace has been created by a concurrent thread.
                        # So we better check if the workspace is there even if the create_workspace() call delivered
                        # a 'None' response:
                        response = self._otcs_frontend.get_workspace_by_type_and_name(
                            type_id=workspace_type_id,
                            name=workspace_name,
                        )
                    workspace_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                    if not workspace_id:
                        self.logger.error(
                            "Failed to bulk create workspace -> '%s' with type ID -> %s from template -> '%s' (%s)!",
                            workspace_name,
                            workspace_type_id,
                            template_name,
                            template_id,
                        )
                        result["success"] = False
                        result["failure_counter"] += 1
                        continue
                    self.logger.info(
                        "Successfully created workspace -> '%s' with ID -> %s from template -> '%s' (%s).",
                        workspace_name,
                        workspace_id,
                        template_name,
                        template_id,
                    )
                    result["create_counter"] += 1

                    # Is Content Aviator enabled system-wide and is it enabled for this
                    # bulkWorkspaces?
                    if self._aviator_enabled and bulk_workspace.get("enable_aviator", False):
                        response = self._otcs_frontend.update_workspace_aviator(
                            workspace_id=workspace_id,
                            status=True,
                        )
                        if not response:
                            self.logger.error(
                                "Failed to enable Content Aviator for workspace -> '%s' (%s)!",
                                workspace_name,
                                workspace_id,
                            )

                    # Check if metadata embeddings need to be updated
                    aviator_metadata = bulk_workspace.get("aviator_metadata", False)
                    if aviator_metadata:
                        self.logger.info(
                            "Trigger Content Aviator metadata embedding for workspace -> '%s' (%s)...",
                            workspace_name,
                            workspace_id,
                        )

                        self._otcs_frontend.aviator_embed_metadata(
                            node_id=workspace_id,
                            wait_for_completion=False,
                        )

                # end if not workspace_id and "create" or "recreate" in row_operations

                # If "updates" are an requested row operation we update the existing workspace with
                # fresh metadata from the payload. Additionally we check the external
                # modify date to support incremental load for content that has really
                # changed.
                # In addition we check that "delete" is not requested as otherwise it will
                # never go in elif "delete" ... below (and it does not make sense to update a workspace
                # that is deleted in the next step...)
                elif (
                    workspace_id
                    and "update" in row_operations
                    and "delete" not in row_operations  # note the NOT !
                    and OTCS.date_is_newer(
                        date_old=workspace_modify_date,
                        date_new=external_modify_date,
                    )
                ):
                    # get the specific update operations given in the payload
                    # if not specified we do all 4 update operations (name, description, categories and nickname)
                    update_operations = bulk_workspace.get(
                        "update_operations",
                        ["name", "description", "categories", "nickname", "classifications", "members"],
                    )

                    # If category data is in payload we substitute
                    # the values with data from the current data row.
                    # This is only done if "categories" update is not
                    # excluded from the update_operations:
                    if categories and "categories" in update_operations:
                        # Make sure the threads are not changing data structures that
                        # are shared between threads. categories is a list of dicts.
                        # list and dicts are "mutable" data structures in Python!
                        worker_categories = self.process_bulk_categories(
                            row=row,
                            index=index,
                            categories=categories,
                            replacements=replacements,
                        )
                        # response = self._otcs_frontend.get_node(node_id=workspace_id)
                        # parent_id = self._otcs_frontend.get_result_value(response=response, key="parent_id")
                        # workspace_category_data = self.prepare_item_create_form(
                        #     parent_id=parent_id,
                        #     categories=worker_categories,
                        #     subtype=self._otcs_frontend.ITEM_TYPE_BUSINESS_WORKSPACE,
                        # )
                        # Transform the payload structure into the format
                        # the OTCS REST API requires:
                        workspace_category_data = self.prepare_category_data(
                            categories_payload=worker_categories,
                            source_node_id=workspace_id,
                        )
                        if not workspace_category_data:
                            self.logger.error(
                                "Failed to prepare the updated category data for workspace -> '%s'!",
                                workspace_name,
                            )
                            result["success"] = False
                            result["failure_counter"] += 1
                            continue  # for index, row in partition.iterrows()
                    # end if categories
                    else:
                        workspace_category_data = {}

                    self.logger.info(
                        "Update existing workspace -> '%s' (%s) with operations -> %s...",
                        workspace_old_name if workspace_old_name else workspace_name,
                        str(workspace_id),
                        str(update_operations),
                    )
                    # Update the workspace with all provided information:
                    response = self._otcs_frontend.update_workspace(
                        workspace_id=workspace_id,
                        workspace_name=(workspace_name if "name" in update_operations else None),
                        workspace_description=(description if "description" in update_operations else None),
                        category_data=(workspace_category_data if "categories" in update_operations else None),
                        external_create_date=external_create_date,
                        external_modify_date=external_modify_date,
                        show_error=True,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to update existing workspace -> '%s' (%s) with type ID -> %s!",
                            (workspace_old_name if workspace_old_name else workspace_name),
                            workspace_id,
                            workspace_type_id,
                        )
                        result["success"] = False
                        result["failure_counter"] += 1
                        continue
                    self.logger.info(
                        "Updated existing workspace -> '%s' (%s) with type ID -> %s.",
                        workspace_name if "name" in update_operations or not workspace_old_name else workspace_old_name,
                        workspace_id,
                        workspace_type_id,
                    )
                    result["update_counter"] += 1

                    if "classifications" in update_operations and (classification_nicknames or classification_pathes):
                        classification_ids = self.process_bulk_classification_assignments(
                            row=row,
                            index=index,
                            classifications=classification_pathes + classification_nicknames,
                        )
                        response = self._otcs.assign_classifications(
                            node_id=workspace_id,
                            classifications=classification_ids,
                            apply_to_sub_items=False,
                        )
                        if response is None:
                            self.logger.error(
                                "Failed to assign classifications -> %s to workspace -> '%s' (%s)!",
                                classification_ids,
                                workspace_name
                                if "name" in update_operations or not workspace_old_name
                                else workspace_old_name,
                                workspace_id,
                            )
                        else:
                            self.logger.info(
                                "Successfully assigned classifications -> %s to workspace -> '%s' (%s).",
                                classification_ids,
                                workspace_name
                                if "name" in update_operations or not workspace_old_name
                                else workspace_old_name,
                                workspace_id,
                            )

                    # Check if metadata embeddings need to be updated
                    aviator_metadata = bulk_workspace.get("aviator_metadata", False)
                    if aviator_metadata:
                        self.logger.info(
                            "Update Content Aviator metadata embedding for workspace -> %s (%s)...",
                            workspace_name,
                            workspace_id,
                        )

                        self._otcs_frontend.aviator_embed_metadata(
                            node_id=workspace_id,
                            wait_for_completion=False,
                        )
                # end elif "update" in row_operations...
                elif workspace_id and "delete" in row_operations:
                    # We delete with immediate purging to keep recycle bin clean
                    # and to not run into issues with nicknames used in deleted items:
                    response = self._otcs_frontend.delete_node(
                        node_id=workspace_id,
                        purge=True,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to delete existing workspace -> '%s' (%s) with type ID -> %s!",
                            (workspace_old_name if workspace_old_name else workspace_name),
                            workspace_id,
                            workspace_type_id,
                        )
                        result["success"] = False
                        result["failure_counter"] += 1
                        continue
                    self.logger.info(
                        "Successfully deleted existing workspace -> '%s' (%s).",
                        workspace_old_name if workspace_old_name else workspace_name,
                        workspace_id,
                    )
                    result["delete_counter"] += 1
                    workspace_id = None
                # end elif workspace_id and "delete" in row_operations

                # this is the plain old "it does exist and we just skip it" case:
                elif workspace_id:
                    result["skipped_counter"] += 1
                    self.logger.info(
                        "Skipped existing workspace -> '%s' (%s)",
                        workspace_old_name if workspace_old_name else workspace_name,
                        workspace_id,
                    )
                # this is the case where we just want to operate on existing workspaces (update or delete)
                # but they do not exist:
                elif not workspace_id and ("update" in row_operations or "delete" in row_operations):
                    result["skipped_counter"] += 1
                    self.logger.info(
                        "Skipped update/delete of non-existing workspace -> '%s'.",
                        workspace_old_name if workspace_old_name else workspace_name,
                    )

                # The following code is executed for all operations
                # (create, recreate, update, delete):

                # Check if we want to set / update the nickname of the workspace.
                # Precondition is we have determined a nickname, we have the workspace ID
                # and the update of the nickname is either required (create, recreate)
                # or requested (update_operations include "nickname"):
                if (
                    nickname
                    and workspace_id
                    and (
                        "create" in row_operations
                        or "recreate" in row_operations
                        or ("update" in row_operations and "nickname" in update_operations)
                    )
                ):
                    response = self._otcs_frontend.set_node_nickname(
                        node_id=workspace_id,
                        nickname=nickname,
                        show_error=True,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to assign nickname -> '%s' to workspace -> '%s'!",
                            nickname,
                            workspace_name,
                        )

                # Check if we want to set / update the members for the workspace roles.
                # Precondition is we have members configured in the payload and setting
                # of roles is requested (workspaces is created or update_operations
                # include "members" operation):
                members = bulk_workspace.get("members")
                if (
                    members
                    and workspace_id
                    and (
                        "create" in row_operations
                        or "recreate" in row_operations
                        or ("update" in row_operations and "members" in update_operations)
                    )
                ):
                    self.looger.info(
                        "Update workspace role members for workspace -> '%s' (%s)...",
                        workspace_name,
                        workspace_id,
                    )
                    workspace_roles = self._otcs_frontend.get_workspace_roles(
                        workspace_id=workspace_id,
                    )

                    # Traverse the members payload:
                    for member in members:
                        # Get the member role from payload first. It is mandatory:
                        member_role = member.get("role", None)
                        if not member_role:
                            self.logger.warning(
                                "Members of workspace -> '%s' is missing the role name.",
                                workspace_name,
                            )
                            continue
                        member_role = self.replace_bulk_placeholders(input_string=member_role, row=row)
                        if not member_role:
                            continue
                        role_id = self._otcs.lookup_result_value(
                            response=workspace_roles,
                            key="name",
                            value=member_role,
                            return_key="id",
                        )
                        if not role_id:
                            self.logger.warning(
                                "Cannot find workspace role -> '%s' for workspace -> '%s' (%s). Skipping...",
                                member_role,
                                workspace_name,
                                workspace_id,
                            )
                            continue

                        # read user list and role name from payload:
                        member_users = member.get("users", [])
                        member_groups = member.get("groups", [])

                        if member_users:
                            self.replace_bulk_placeholders_list(input_list=member_users, row=row)
                        if member_groups:
                            self.replace_bulk_placeholders_list(input_list=member_groups, row=row)

                        if not member_users and not member_groups:
                            self.logger.debug(
                                "Role -> '%s' of workspace -> '%s' does not have any members (no users nor groups).",
                                member_role,
                                workspace_name,
                            )
                            continue

                        # check if we want to clear (remove) existing members of this role:
                        member_clear = member.get("clear", False)
                        if member_clear:
                            self.logger.info(
                                "Clear existing members of role -> '%s' (%s) for workspace -> '%s' (%s)...",
                                member_role,
                                role_id,
                                workspace_name,
                                workspace_id,
                            )
                            self._otcs.remove_workspace_members(
                                workspace_id=workspace_id,
                                role_id=role_id,
                            )
                    # TODO: complete the implementation...
                # end if members...

                # Depending on the bulk operations (create, update, delete)
                # and the related conditions it may well be that workspace_id is None.
                # Only if workspace ID is not none we want to count this row as success:
                if workspace_id is not None:
                    result["success_counter"] += 1
                    # Record the workspace name and ID to allow to read it from failure file
                    # and speedup the process.
                    result["workspaces"][workspace_name] = workspace_id
        # end for index...

        self.logger.info("End working...")

        results.append(result)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="lookup_data_source_value")
    def lookup_data_source_value(
        self,
        data_source: dict,
        lookup_column: str,
        lookup_value: str,
        separator: str = "|",
    ) -> pd.Series | None:
        """Lookup a value in a given data source (specified by payload dict).

        If the data source has not been loaded before then load the data source.
        As this runs in a multi-threading environment we need to protect
        the data source update from multiple threads doing it at the same time.
        A global data_load_lock variable acts as a mutex.

        Args:
            data_source (dict):
                The payload dictionary of the data source definition.
            lookup_column (str):
                The name of the column in the data frame (see Data class).
            lookup_value (str):
                The value to lookup - selection criteria for the result row.
            separator (str, optional):
                The string list delimiter / separator. The pipe symbol | is the default
                as it is unlikely to appear in a normal string (other than a plain comma).
                The separator is NOT looked for in the lookup_value but in the column that
                is given by lookup_column!

        Returns:
            pd.Series | None:
                Row that matches the lookup value in the lookup column.

        """

        data_source_name = data_source.get("name")
        if not data_source_name:
            self.logger.error("Data source has no name!")
            return None

        # We don't want multiple threads to trigger a data source load at the same time,
        # so we use a lock (mutex) to avoid this:
        data_load_lock.acquire()
        try:
            # First we check if the data source has been loaded already.
            # If not, we load the data source on the fly:
            data_source_data: Data = data_source.get("data")
            if not data_source_data:
                self.logger.info(
                    "Lookup data source -> '%s' has no data yet. Reloading...",
                    data_source_name,
                )
                data_source_data = self.process_bulk_datasource(
                    data_source_name=data_source_name,
                    force_reload=True,
                )
        finally:
            # Ensure the lock is released even if an error occurs
            data_load_lock.release()

        # If we still don't have data from this data source we bail out:
        if data_source_data is None:  # important to use "is None" here!
            self.logger.error(
                "Lookup data source -> '%s' has no data and reload did not work. Cannot lookup value -> '%s' in column -> '%s'!",
                data_source_name,
                lookup_value,
                lookup_column,
            )
            return None

        # Lookup the data frame row (pd.Series) in the given
        # column with the given lookup value:
        lookup_row: pd.Series = data_source_data.lookup_value(
            lookup_column=lookup_column,
            lookup_value=lookup_value,
            separator=separator,
        )

        return lookup_row

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspaces_synonym_lookup")
    def process_bulk_workspaces_synonym_lookup(
        self,
        data_source_name: str,
        workspace_name_synonym: str = "",
        workspace_type: str = "",
    ) -> tuple[int | None, str | None] | None:
        """Use a data source to lookup the workspace name (or all fields) and ID using a given synonym.

        Args:
            data_source_name (str):
                The data source name.
            workspace_name_synonym (str, optional):
                The synonym of the workspace name as input for lookup.
            workspace_type (str, optional):
                The name of the workspace type. Default is "".

        Returns:
            tuple[int | None, int | None]:
                The workspace ID and the looked up workspace name or None in case the loomkup has failed.

        """

        # Determine the data source to use for synonym lookup:
        if not data_source_name:
            self.logger.error(
                "No workspace data source name specified. Cannot lookup the workspace by synonym -> '%s'!",
                workspace_name_synonym,
            )
            return (None, None)
        workspace_data_source = next(
            (item for item in self._bulk_datasources if item["name"] == data_source_name),
            None,
        )
        if not workspace_data_source:
            self.logger.error(
                "Workspace data source -> '%s' not found in payload. Cannot lookup the workspace by synonym -> '%s'!",
                data_source_name,
                workspace_name_synonym,
            )
            return (None, None)

        # Read the synonym column and the name column from the data source payload item:
        workspace_data_source_name_column = workspace_data_source.get(
            "name_column",
            None,  # e.g. "Name"
        )
        workspace_data_source_synonyms_column = workspace_data_source.get(
            "synonyms_column",
            None,  # e.g. "Synonyms"
        )

        if not workspace_data_source_name_column or not workspace_data_source_synonyms_column:
            self.logger.warning(
                "Workspace data source -> '%s' has no synonym lookup columns. Cannot find the workspace by synonym -> '%s'!",
                data_source_name,
                workspace_name_synonym,
            )
            return (None, None)

        # Get the row that has the synonym in the synonym column:
        lookup_row = self.lookup_data_source_value(
            data_source=workspace_data_source,
            lookup_column=workspace_data_source_synonyms_column,
            lookup_value=workspace_name_synonym,
        )

        if lookup_row is None:
            # Handle an edge case where the actual workspace name
            # is already correct (using the name column):
            lookup_row = self.lookup_data_source_value(
                data_source=workspace_data_source,
                lookup_column=workspace_data_source_name_column,
                lookup_value=workspace_name_synonym,
            )

        if lookup_row is not None:
            # Now we determine the real workspace name be taking it from
            # the name column in the result row:
            workspace_name = lookup_row[workspace_data_source_name_column]
            self.logger.info(
                "Found workspace name -> '%s' using synonym -> '%s'.",
                workspace_name,
                workspace_name_synonym,
            )

            # We now have the real name. If the workspace type name is
            # provided as well we should be able to lookup the workspace ID now:
            if workspace_type:
                response = self._otcs_frontend.get_workspace_by_type_and_name(
                    type_name=workspace_type,
                    name=workspace_name,
                )
                workspace_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
            else:
                # This method may be called with workspace_type=None.
                # In this case we can return the synonym but cannot
                # lookup the workspace ID:
                workspace_id = None

            # Return the tuple with workspace_id and the real workspace name
            return (workspace_id, workspace_name)

        return (None, None)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspaces_lookup")
    def process_bulk_workspaces_lookup(
        self,
        workspace_nickname: str | None = None,
        workspace_name: str | None = None,
        workspace_type: str | None = None,
        parent_id: int | None = None,
        data_source_name: str | None = None,
        show_error: bool = True,
    ) -> tuple[int | None, str | None]:
        """Look the workspace name and ID.

        Use a combination of workspace name, workspace type, and workspace
        data source (using synonyms) to do so.

        Args:
            workspace_nickname (str, optional):
                The nickname of the workspace.
            workspace_name (str, optional):
                The name as input for lookup. This must be either the name of
                an existing workspace instance or one of the synonyms of the workspace name.
            workspace_type (str, optional):
                The Name of the workspace type.
            parent_id (int, optional):
                ID of parent workspace (if it is a sub-workspace) or parent folder.
            data_source_name (str, optional):
                The workspace data source name.
            show_error (bool, optional):
                Whether or not to log an error if it occurs. If False, just log a warning.

        Returns:
            tuple[int | None, str | None]:
                The workspace ID and the looked up workspace name.

        """

        # First we try to find the workspace by a nickname (if provided)
        if workspace_nickname:
            # Nicknames for sure are not allowed to include spaces and dashes:
            workspace_nickname = workspace_nickname.replace(" ", "_")
            workspace_nickname = workspace_nickname.replace("-", "_")
            workspace_nickname = workspace_nickname.replace("___", "_")
            workspace_nickname = workspace_nickname.lower()

            response = self._otcs_frontend.get_node_from_nickname(
                nickname=workspace_nickname,
                show_error=False,
            )
            workspace_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
            if workspace_id:
                # If we don't have the name yet, we can get it from the response above:
                if not workspace_name:
                    workspace_name = self._otcs_frontend.get_result_value(
                        response=response,
                        key="name",
                    )
                return (workspace_id, workspace_name)
            # DON'T RETURN FAILURE AT THIS POINT!

        # Our 2nd try is to find the workspace by a workspace name and workspace type combination:
        if workspace_name:
            workspace_name = workspace_name.strip()
        else:
            self.logger.error(
                "No workspace name specified. Cannot find the workspace by nickname%s, nor by type and name, nor by parent ID and name, nor by synonym.",
                " -> {}".format(workspace_nickname) if workspace_nickname else "",
            )
            return (None, None)

        # If we have workspace name and workspace parent ID then we try this:
        if workspace_name and parent_id is not None:
            response = self._otcs_frontend.get_node_by_parent_and_name(
                parent_id=parent_id,
                name=workspace_name,
            )
            workspace_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
            if workspace_id:
                return (workspace_id, workspace_name)

        # If we have workspace name and workspace type then we try to get
        # the workspace by type + name:
        if workspace_name and workspace_type:
            response = self._otcs_frontend.get_workspace_by_type_and_name(
                type_name=workspace_type,
                name=workspace_name,
            )
            workspace_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
            if workspace_id:
                return (workspace_id, workspace_name)

        # if the code gets to here we dont have a nickname and the workspace with given name
        # type, or parent ID was not found either. Now we see if we can find the workspace name
        # as a synonym in the workspace data source to find the real/correct workspace name:
        if data_source_name:
            self.logger.info(
                "Try to find the workspace with the synonym -> '%s' using data source -> '%s'...",
                workspace_name,
                data_source_name,
            )

            (workspace_id, workspace_synonym_name) = self.process_bulk_workspaces_synonym_lookup(
                data_source_name=data_source_name,
                workspace_name_synonym=workspace_name,  # see if workspace_name is a synonym
                workspace_type=workspace_type,
            )
            if workspace_id is not None:
                return (workspace_id, workspace_synonym_name)

        # As this message may be hunderds of times in the log
        # we invest some effort to make it look nice:
        message = "Couldn't find a workspace "
        concat_string = ""
        if workspace_nickname:
            message += "by nickname -> '{}'".format(workspace_nickname)
            concat_string = ", nor "
        if workspace_name:
            message += "{}by name -> '{}'".format(concat_string, workspace_name)
            concat_string = ", nor "
        if parent_id:
            message += "{}by parent ID -> {}".format(concat_string, parent_id)
            concat_string = ", nor "
        if data_source_name:
            message += "{}as synonym in data source -> '{}'".format(
                concat_string,
                data_source_name,
            )
        if show_error:
            self.logger.error(message)
        else:
            self.logger.warning(message)

        return (
            None,
            workspace_name,
        )  # it is important to return the name - used by process_bulk_categories()

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspace_relationships")
    def process_bulk_workspace_relationships(
        self,
        section_name: str = "bulkWorkspaceRelationships",
    ) -> bool:
        """Process workspaces in payload and bulk create them in Content Server (multi-threaded).

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not self._bulk_workspace_relationships:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        # For efficient idem-potent operation we may want to see which workspaces
        # have already been processed before:
        if self.check_status_file(
            payload_section_name=section_name,
            payload_specific=True,
            prefix="failure_",
        ):
            # Read payload from where we left it last time
            self._bulk_workspace_relationships = self.get_status_file(
                payload_section_name=section_name,
                prefix="failure_",
            )
            if not self._bulk_workspace_relationships:
                self.logger.error(
                    "Cannot load existing bulkWorkspaceRelationships failure file. Bailing out!",
                )
                return False

        success: bool = True

        for bulk_workspace_relationship in self._bulk_workspace_relationships:
            # Check if element has been disabled in payload (enabled = false).
            # In this case we skip the element:
            enabled = bulk_workspace_relationship.get("enabled", True)
            if not enabled:
                self.logger.info(
                    "Payload for bulk workspace relationship is disabled. Skipping...",
                )
                continue

            # Read Pattern for "From" Workspace from payload:
            from_workspace = bulk_workspace_relationship.get("from_workspace", None)
            if not from_workspace:
                self.logger.error(
                    "Bulk workspace relationship creation needs a workspace nickname in from_workspace! Skipping to next bulk workspace relationship...",
                )
                success = False
                continue
            from_sub_workspace = bulk_workspace_relationship.get(
                "from_sub_workspace_name",
                None,
            )

            # Read Pattern for "To" Workspace from payload:
            to_workspace = bulk_workspace_relationship.get("to_workspace", None)
            if not to_workspace:
                self.logger.error(
                    "Bulk workspace relationship creation needs a workspace nickname in to_workspace! Skipping to next bulk workspace relationship...",
                )
                success = False
                continue
            to_sub_workspace = bulk_workspace_relationship.get(
                "to_sub_workspace_name",
                None,
            )

            # The payload element must have a "data_source" key:
            data_source_name = bulk_workspace_relationship.get("data_source", None)
            if not data_source_name:
                self.logger.error(
                    "Missing data source name in bulk workspace relationship!",
                )
                success = False
                continue

            self._log_header_callback(
                text="Process bulk workspace relationships from -> '{}' to -> '{}'".format(
                    from_sub_workspace if from_sub_workspace else from_workspace,
                    to_sub_workspace if to_sub_workspace else to_workspace,
                ),
                char="-",
            )

            # Determine which operations should be done for this bulk workspace relationship:
            operations = bulk_workspace_relationship.get("operations", ["create"])
            if "delete" in operations and "create" in operations:
                self.logger.error(
                    "Bulk workspace relationships can either have 'create' or 'delete' operation - not both. Use separate payloads for this purpose!",
                )
                success = False
                continue

            copy_data_source = bulk_workspace_relationship.get(
                "copy_data_source",
                False,
            )
            force_reload = bulk_workspace_relationship.get("force_reload", True)

            # Load and prepare the data source for the bulk processing:
            if copy_data_source:
                self.logger.info(
                    "Take a copy of data source -> '%s' to avoid side-effects for repeative usage of the data source...",
                    bulk_workspace_relationship["data_source"],
                )
                data = Data(
                    self.process_bulk_datasource(
                        data_source_name=data_source_name,
                        force_reload=force_reload,
                    ),
                    logger=self.logger,
                )
            else:
                self.logger.info(
                    "Use original data source -> '%s' and not do a copy.",
                    bulk_workspace_relationship["data_source"],
                )
                # Load and prepare the data source for the bulk processing:
                data = self.process_bulk_datasource(
                    data_source_name=data_source_name,
                    force_reload=force_reload,
                )
            if data is None:  # important to use "is None" here!
                self.logger.error(
                    "Failed to load data source -> '%s' for bulk workspace relationships from -> '%s' to -> '%s'",
                    data_source_name,
                    from_sub_workspace if from_sub_workspace else from_workspace,
                    to_sub_workspace if to_sub_workspace else to_workspace,
                )
                success = False
                continue
            if data.get_data_frame() is None:  # important to use "is None" here!
                self.logger.warning(
                    "Data source for bulk workspace relationships from -> '%s' to -> '%s' is empty!",
                    from_sub_workspace if from_sub_workspace else from_workspace,
                    to_sub_workspace if to_sub_workspace else to_workspace,
                )
                continue

            # Check if fields with list substructures should be exploded.
            # We may want to do this outside the bulkDatasource to only
            # explode for bulkDocuments and not for bulkWorkspaces or
            # bulkWorkspaceRelationships:
            explosions = bulk_workspace_relationship.get("explosions", [])
            for explosion in explosions:
                # The explode field parameter can be a string or a list.
                # Exploding multiple fields at once avoids combinatorial explosions -
                # this is VERY different from exploding columns one after the other!
                if "explode_field" not in explosion and "explode_fields" not in explosion:
                    self.logger.error("Missing explosion field(s)!")
                    continue
                # we want to be backwards compatible...
                explode_fields = (
                    explosion["explode_field"] if "explode_field" in explosion else explosion["explode_fields"]
                )
                flatten_fields = explosion.get("flatten_fields", [])
                split_string_to_list = explosion.get("split_string_to_list", False)
                list_splitter = explosion.get(
                    "list_splitter",
                    ",",
                )  # don't have None as default!
                self.logger.info(
                    "Starting explosion of bulk relationships by field(s) -> %s (type -> %s). Size of data frame before explosion -> %s.",
                    str(explode_fields),
                    type(explode_fields),
                    str(len(data)),
                )
                data.explode_and_flatten(
                    explode_fields=explode_fields,
                    flatten_fields=flatten_fields,
                    make_unique=False,
                    split_string_to_list=split_string_to_list,
                    separator=list_splitter,
                    reset_index=True,
                )
                self.logger.info(
                    "Size of data frame after explosion -> %s.",
                    str(len(data)),
                )
            # end for explosion in explosions

            # Keep only selected rows if filters are specified in bulkWorkspaceRelationship.
            # We have this _after_ "explosions" to allow access to subfields as well.
            # We have this _before_ "sorting" and "deduplication" as we may keep the wrong
            # rows otherwise (unique / deduplication always keeps the first matching row).
            # We may want to do this filtering outside the bulkDatasource to only
            # filter the data frame for bulkDocuments and not for bulkWorkspaces or
            # bulkWorkspaceRelationships:
            filters = bulk_workspace_relationship.get("filters", [])
            if filters:
                data.filter(conditions=filters)

            # Sort the data frame if "sort" specified in payload. We may want to do this to have a
            # higher chance that rows with common values that may collapse into
            # one name are put into the same partition. This can avoid race conditions
            # between different Python threads.
            sort_fields = bulk_workspace_relationship.get("sort", [])
            if sort_fields:
                self.logger.info(
                    "Start sorting of bulk workspace relationships data frame based on fields (columns) -> %s...",
                    str(sort_fields),
                )
                data.sort(sort_fields=sort_fields, inplace=True)
                self.logger.info(
                    "Sorting of bulk workspace relationships data frame based on fields (columns) -> %s completed.",
                    str(sort_fields),
                )

            # Check if duplicate rows for given fields should be removed. It is
            # important to do this after sorting as Pandas always keep the first occurance,
            # so ordering plays an important role in deduplication!
            unique_fields = bulk_workspace_relationship.get("unique", [])
            if unique_fields:
                self.logger.info(
                    "Starting deduplication of data frame for bulk workspace relationships with unique fields -> %s. Size of data frame before deduplication -> %s",
                    str(unique_fields),
                    str(len(data)),
                )
                data.deduplicate(unique_fields=unique_fields, inplace=True)
                self.logger.info(
                    "Size of data frame after deduplication -> %s.",
                    str(len(data)),
                )

            self.logger.info(
                "Bulk create workspace relationships (from workspace -> '%s' to workspace -> '%s'). Operations -> %s.",
                from_sub_workspace if from_sub_workspace else from_workspace,
                to_sub_workspace if to_sub_workspace else to_workspace,
                str(operations),
            )

            # See if bulkWorkspace definition has a specific thread number
            # otherwise it is read from a global environment variable
            bulk_thread_number = int(
                bulk_workspace_relationship.get("thread_number", BULK_THREAD_NUMBER),
            )

            partitions = data.partitionate(bulk_thread_number)

            # Create a list to hold the threads
            threads = []
            results = []

            # Create and start a thread for each partition
            for index, partition in enumerate(partitions, start=1):
                # start a thread executing the process_bulk_workspace_relationships_worker() method below:
                thread = threading.Thread(
                    name=f"{section_name}_{index:02}",
                    target=self.thread_wrapper,
                    args=(
                        self.process_bulk_workspace_relationships_worker,
                        bulk_workspace_relationship,
                        partition,
                        operations,
                        results,
                    ),
                )
                self.logger.info("Starting thread -> '%s'...", str(thread.name))
                thread.start()
                threads.append(thread)
                # Avoid that all threads start at the exact same time with
                # potentially expired cookies that cases race conditions:
                time.sleep(1)
            # end for index, partition in enumerate(partitions, start=1)

            # Wait for all threads to complete
            for thread in threads:
                self.logger.info(
                    "Waiting for thread -> '%s' to complete...",
                    str(thread.name),
                )
                thread.join()
                self.logger.info("Thread -> '%s' has completed.", str(thread.name))

            if "relationships" not in bulk_workspace_relationship:
                bulk_workspace_relationship["relationships"] = {}

            # Print statistics for each thread. In addition,
            # check if all threads have completed without error / failure.
            # If there's a single failure in on of the thread results we
            # set 'success' variable to False.
            results.sort(key=lambda x: x["thread_name"])
            for result in results:
                if not result["success"]:
                    self.logger.info(
                        "Thread -> '%s' completed with %s failed, %s skipped, and %s created workspace relationships.",
                        str(result["thread_name"]),
                        str(result["failure_counter"]),
                        str(result["skipped_counter"]),
                        str(result["success_counter"]),
                    )
                    success = False
                else:
                    self.logger.info(
                        "Thread -> '%s' completed successful with %s skipped, and %s created workspace relationships.",
                        str(result["thread_name"]),
                        str(result["skipped_counter"]),
                        str(result["success_counter"]),
                    )
                # Record all generated workspaces relationships. This should
                # allow us to restart in case of failures and avoid trying to
                # create workspaces that have been created before.
                bulk_workspace_relationship["relationships"].update(
                    result["relationships"],
                )
            self._log_header_callback(
                text="Completed processing of bulk workspace relationships from -> '{}' to -> '{}'".format(
                    from_sub_workspace if from_sub_workspace else from_workspace,
                    to_sub_workspace if to_sub_workspace else to_workspace,
                ),
                char="-",
            )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._bulk_workspace_relationships,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_bulk_workspace_relationship_endpoint")
    def get_bulk_workspace_relationship_endpoint(
        self,
        bulk_workspace_relationship: dict,
        row: pd.Series,
        index: int,
        endpoint: str,
        replacements: dict | None = None,
        nickname_additional_regex_list: list | None = None,
        show_error: bool = True,
    ) -> tuple[int | None, str | None]:
        """Determine the node ID of the workspace that is one of the endpoints of the workspace relationship (either 'from' or 'to').

        Args:
            bulk_workspace_relationship (dict):
                The payload element of the bulk workspace relationship
            row (pd.Series):
                The data frame row.
            index (int):
                The index of the data frame row.
            endpoint (str):
                The name of the endpoint - either "from" or "to".
            replacements (dict | None, optional):
                Replacements for placeholders. Defaults to None.
            nickname_additional_regex_list (list | None, optional):
                Additional regex replacements for nicknames. Defaults to None.
            show_error (bool, optional):
                Whether or not to log an error. If False just a warning is logged.

        Returns:
            tuple[int | None, str | None]:
                The workspace ID and the looked up workspace name.

        """

        if endpoint not in ["from", "to"]:
            self.logger.error("The endpoint must be either 'from' or 'to'!")
            return (None, None)

        # Determine the workspace nickname field:
        workspace_nickname_field = bulk_workspace_relationship.get(
            "{}_workspace".format(endpoint),
            None,
        )
        workspace_nickname = self.replace_bulk_placeholders(
            input_string=workspace_nickname_field,
            row=row,
            replacements=replacements,
            additional_regex_list=nickname_additional_regex_list,
        )
        if not workspace_nickname:
            self.logger.warning(
                "Row -> %s does not have the required data to resolve -> '%s' for the workspace nickname (endpoint -> '%s')!",
                str(index),
                workspace_nickname_field,
                endpoint,
            )
            return (None, None)

        # Get the workspace type if specified:
        workspace_type = bulk_workspace_relationship.get(
            "{}_workspace_type".format(endpoint),
            None,
        )

        # Get the workspace name if specified:
        workspace_name_field = bulk_workspace_relationship.get(
            "{}_workspace_name".format(endpoint),
            None,
        )
        if workspace_name_field:
            workspace_name = self.replace_bulk_placeholders(
                input_string=workspace_name_field,
                row=row,
                replacements=replacements,
            )
            if not workspace_name:
                self.logger.warning(
                    "Row -> %s does not have the required data to resolve -> '%s' for the workspace name (endpoint -> '%s')!",
                    str(index),
                    workspace_name_field,
                    endpoint,
                )
                return (None, None)
        else:
            workspace_name = None

        # Get the workspace data source if specified:
        workspace_data_source = bulk_workspace_relationship.get(
            "{}_workspace_data_source".format(endpoint),
            None,
        )

        # Based on the given information, we now try to determine
        # the name and the ID of the workspace that is the endpoint
        # for the workspace relationship:
        (workspace_id, workspace_name) = self.process_bulk_workspaces_lookup(
            workspace_nickname=workspace_nickname,
            workspace_name=workspace_name,
            workspace_type=workspace_type,
            data_source_name=workspace_data_source,
            show_error=show_error,
        )

        if not workspace_id:
            self.logger.warning(
                "Cannot find workspace to establish relationship (endpoint -> '%s')%s%s%s%s",
                endpoint,
                (", nickname -> '{}'".format(workspace_nickname) if workspace_nickname else ""),
                (", workspace name -> '{}'".format(workspace_name) if workspace_name else ""),
                (", workspace type -> '{}'".format(workspace_type) if workspace_type else ""),
                (", data source -> '{}'".format(workspace_data_source) if workspace_data_source else ""),
            )
            return (None, None)

        # See if a sub-workspace is configured:
        sub_workspace_name_field = bulk_workspace_relationship.get(
            "{}_sub_workspace_name".format(endpoint),
            None,
        )
        # If no sub-workspace is configured we can already
        # return the resulting workspace ID and name here:
        if not sub_workspace_name_field:
            return (workspace_id, workspace_name)

        # Otherwise we are no processing the sub-workspaces to return
        # its ID instead:
        sub_workspace_name = self.replace_bulk_placeholders(
            input_string=sub_workspace_name_field,
            row=row,
            replacements=replacements,
        )
        if not sub_workspace_name:
            self.logger.warning(
                "Row -> %s does not have the required data to resolve -> %s for the sub-workspace name (endpoint -> '%s')!",
                str(index),
                sub_workspace_name_field,
                endpoint,
            )
            return (None, None)

        # See if a sub-workspace is in a sub-path of the main workspace:
        sub_workspace_path = bulk_workspace_relationship.get(
            "{}_sub_workspace_path".format(endpoint),
            None,
        )
        if sub_workspace_path:
            # sub_workspace_path is a mutable that is changed in place!
            result = self.replace_bulk_placeholders_list(
                input_list=sub_workspace_path,
                row=row,
                replacements=replacements,
            )
            if not result:
                self.logger.warning(
                    "Row -> %s does not have the required data to resolve -> %s for the sub-workspace path (endpoint -> '%s')!",
                    str(index),
                    sub_workspace_path,
                    endpoint,
                )
                return None

            self.logger.info(
                "Endpoint has a sub-workspace -> '%s' configured. Try to find the sub-workspace in workspace path -> %s...",
                sub_workspace_name,
                sub_workspace_path,
            )

            # We now want to retrieve the folder in the main workspace that
            # includes the sub-workspace:
            response = self._otcs_frontend.get_node_by_workspace_and_path(
                workspace_id=workspace_id,
                path=sub_workspace_path,
                create_path=False,  # we don't want the path to be created if it doesn't exist
                show_error=True,
            )
            parent_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
            if not parent_id:
                self.logger.error(
                    "Failed to find path -> %s in workspace -> '%s' (%s)!",
                    str(sub_workspace_path),
                    workspace_name,
                    workspace_id,
                )
                return (None, None)
        # end if sub_workspace_path_field
        else:
            # the sub-workspace is immediately under the main workspace:
            parent_id = workspace_id

        response = self._otcs_frontend.get_node_by_parent_and_name(
            parent_id=parent_id,
            name=sub_workspace_name,
            show_error=True,
        )
        sub_workspace_id = self._otcs_frontend.get_result_value(
            response=response,
            key="id",
        )

        return (sub_workspace_id, sub_workspace_name)

    # end method definition

    @tracer.start_as_current_span(
        attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspace_relationships_worker"
    )
    def process_bulk_workspace_relationships_worker(
        self,
        bulk_workspace_relationship: dict,
        partition: pd.DataFrame,
        operations: list | None = None,
        results: list | None = None,
    ) -> None:
        """Thread worker to create workspaces relationships in bulk.

        Each worker thread gets a partition of the rows that include
        the data required for the workspace relationship creation.

        Args:
            bulk_workspace_relationship (dict):
                The payload of the bulkWorkspaceRelationship.
            partition (pd.DataFrame):
                The data partition with rows to process.
            from_workspace (str):
                The string pattern for nickname of workspace (from).
            to_workspace (str):
                The string pattern for nickname of workspace (to).
            operations (list, optional):
                Defines which operations should be applyed on workspace relationships.
                Possible values are "create", "delete", "recreate".
            results (list, optional):
                A mutable list of thread results.

        """

        thread_id = threading.get_ident()
        thread_name = threading.current_thread().name

        self.logger.info(
            "Start working on data frame partition of size -> %s...",
            str(len(partition)),
        )

        # Avoid linter warnings - so make parameter default None while we
        # actually want ["create"] to be the default:
        if operations is None:
            operations = ["create"]

        result = {}
        result["thread_id"] = thread_id
        result["thread_name"] = thread_name
        result["success_counter"] = 0
        result["failure_counter"] = 0
        result["skipped_counter"] = 0
        result["delete_counter"] = 0
        result["relationships"] = {}
        result["success"] = True

        # Check if workspace relationships have been processed before, e.i. testing
        # if a "relationships" key exists and if it is pointing to a non-empty list:
        if bulk_workspace_relationship.get("relationships") and "delete" not in operations:
            existing_workspace_relationships = bulk_workspace_relationship["relationships"]
            self.logger.info(
                "Found %s already processed workspace relationships. Try to complete the job...",
                str(len(existing_workspace_relationships)),
            )
        else:
            existing_workspace_relationships = {}

        # Get dictionary of replacements for bulk workspace relationships
        # this we will be used of all places data is read from the
        # current data frame row. Each dictionary item has the field name as the
        # dictionary key and a list of regular expressions as dictionary value
        replacements = bulk_workspace_relationship.get("replacements")

        # Check if all data conditions to create the workspace relationship are met
        conditions = bulk_workspace_relationship.get("conditions")

        # Type of the relationship - can either be child or parent.
        relationship_type = bulk_workspace_relationship.get("type", "child")

        # Whether or not we want to show an error if the lookup of related
        # workspaces fails.
        show_lookup_error_from = bulk_workspace_relationship.get(
            "from_workspace_lookup_error",
            True,
        )
        show_lookup_error_to = bulk_workspace_relationship.get(
            "to_workspace_lookup_error",
            True,
        )

        # Nicknames are very limited in terms of allowed characters.
        # For nicknames we need additional regexp as we need to
        # replace all non-alphanumeric, non-space characters with ""
        # We also preserve hyphens in the first step to replace
        # them below with underscores. This is important to avoid
        # that different spellings of names produce different nicknames.
        # We want spellings with spaces match spellings with hyphens.
        # For this the workspace names have a regexp "-| " in the payload.
        nickname_additional_regex_list = [r"[^\w\s-]"]

        total_rows = len(partition)

        # Process all datasets in the partion that was given to the thread:
        for index, row in partition.iterrows():
            # Calculate percentage of completion
            percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

            self.logger.info(
                "Processing data row -> %s for bulk workspace relationship creation (%.2f %% complete)...",
                str(index),
                percent_complete,
            )

            # Create a copy of the mutable operations list as we may
            # want to modify it per row:
            row_operations = list(operations)

            # check if we have any exlusions that apply here:
            if conditions:
                evaluated_condition = self.evaluate_conditions(
                    conditions=conditions,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_condition:
                    self.logger.info(
                        "Condition for row -> %s not met. Skipping row for workspace relationship...",
                        str(index),
                    )
                    result["skipped_counter"] += 1
                    continue

            (from_workspace_id, from_workspace_name) = self.get_bulk_workspace_relationship_endpoint(
                bulk_workspace_relationship=bulk_workspace_relationship,
                row=row,
                index=index,
                endpoint="from",
                replacements=replacements,
                nickname_additional_regex_list=nickname_additional_regex_list,
                show_error=show_lookup_error_from,
            )

            # Check we have "from" endpoint:
            if not from_workspace_id:
                self.logger.warning(
                    "%s%s",
                    (
                        "Failed to retrieve 'from' endpoint for bulk workspace relationship! "
                        if not from_workspace_id and not from_workspace_name
                        else ""
                    ),
                    (
                        "Failed to retrieve 'from' endpoint (workspace name -> {}) for bulk workspace relationship! ".format(
                            from_workspace_name,
                        )
                        if not from_workspace_id and from_workspace_name
                        else ""
                    ),
                )
                result["skipped_counter"] += 1
                continue

            # If relationships should be deleted we do it only if the
            # relationships for this workspace ID have not been deleted before.
            # This is checked by a dictionary result["relationships"].
            # If a "delete" operation is requested there cannot be a "create"
            # operation in the same bulk workspace relationships payload!
            # (this would create too many interferences between threads)
            # Because of this we can continue with the next row then:
            if "delete" in row_operations and from_workspace_id not in result["relationships"]:
                # Get the target (to) workspace type if specified:
                to_workspace_type = bulk_workspace_relationship.get("to_workspace_type")
                if not to_workspace_type:
                    self.logger.error(
                        "Cannot delete workspace relationships! Need the target (to) workspace type!",
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue
                result = self._otcs_frontend.delete_workspace_relationships(
                    workspace_id=from_workspace_id,
                    related_workspace_type_id=to_workspace_type,
                )
                if not result:
                    result["success"] = False
                    result["failure_counter"] += 1
                else:
                    result["delete_counter"] += 1
                    # Mark that we have deleted the relationships originating from
                    # the current workspace ID (from) pointing to workspace of
                    # the given type.
                    result["relationships"][from_workspace_id] = [to_workspace_type]
                # We are continuing here as we cannot do delete and create
                # operations in one thread. This needs separate bulkRelationships
                # payloads!
                continue

            (to_workspace_id, to_workspace_name) = self.get_bulk_workspace_relationship_endpoint(
                bulk_workspace_relationship=bulk_workspace_relationship,
                row=row,
                index=index,
                endpoint="to",
                replacements=replacements,
                nickname_additional_regex_list=nickname_additional_regex_list,
                show_error=show_lookup_error_to,
            )

            # Check we have "to" endpoint:
            if not from_workspace_id or not to_workspace_id:
                self.logger.warning(
                    "%s%s",
                    (
                        "Failed to retrieve 'to' endpoint for bulk workspace relationship!"
                        if not to_workspace_id and not to_workspace_name
                        else ""
                    ),
                    (
                        "Failed to retrieve 'to' endpoint (workspace name -> {}) for bulk workspace relationship!".format(
                            to_workspace_name,
                        )
                        if not to_workspace_id and to_workspace_name
                        else ""
                    ),
                )
                result["skipped_counter"] += 1
                continue

            # Check if workspace relationship has been created before (either in this run
            # or in a former run of the customizer):
            if (  # processed in former run?
                from_workspace_id in existing_workspace_relationships
                and to_workspace_id in existing_workspace_relationships[from_workspace_id]
            ) or (  # processed in current run?
                from_workspace_id in result["relationships"]
                and to_workspace_id in result["relationships"][from_workspace_id]
            ):
                self.logger.info(
                    "Workspace relationship between workspace -> '%s' (%s) and related workspace -> '%s' (%s) has successfully been processed before. Skipping...",
                    from_workspace_name,
                    str(from_workspace_id),
                    to_workspace_name,
                    str(to_workspace_id),
                )
                result["skipped_counter"] += 1
                continue

            # Check if workspace relationship does already exist in Extended ECM
            # (this is an additional safety measure to avoid errors):
            response = self._otcs_frontend.get_workspace_relationships(
                workspace_id=from_workspace_id,
                relationship_type=relationship_type,
                related_workspace_name=to_workspace_name,
            )
            current_workspace_relationships = self._otcs_frontend.exist_result_item(
                response=response,
                key="id",
                value=to_workspace_id,
            )
            if current_workspace_relationships:
                self.logger.info(
                    "Workspace relationship between workspace -> '%s' (%s) and related workspace -> '%s' (%s) does already exist. Skipping...",
                    from_workspace_name,
                    str(from_workspace_id),
                    to_workspace_name,
                    str(to_workspace_id),
                )
                result["skipped_counter"] += 1
                continue

            self.logger.info(
                "Bulk create workspace relationship '%s' (%s) -> '%s' (%s)...",
                from_workspace_name,
                str(from_workspace_id),
                to_workspace_name,
                str(to_workspace_id),
            )

            response = self._otcs_frontend.create_workspace_relationship(
                workspace_id=from_workspace_id,
                related_workspace_id=to_workspace_id,
                relationship_type=relationship_type,
                show_error=False,
            )

            if response is None:
                # Potential race condition: see if the workspace-2-workspace relationship has been created by a concurrent thread.
                # So we better check if the relationship is there even if the create_workspace_relationship() call delivered
                # a 'None' response:
                response = self._otcs_frontend.get_workspace_relationships(
                    workspace_id=from_workspace_id,
                    relationship_type=relationship_type,
                    related_workspace_name=to_workspace_name,
                )
                current_workspace_relationships = self._otcs_frontend.exist_result_item(
                    response=response,
                    key="id",
                    value=to_workspace_id,
                )
                if current_workspace_relationships:
                    self.logger.info(
                        "Workspace relationship between workspace -> '%s' (%s) and related workspace -> '%s' (%s) has been created concurrently. Skipping...",
                        from_workspace_name,
                        str(from_workspace_id),
                        to_workspace_name,
                        str(to_workspace_id),
                    )
                    result["skipped_counter"] += 1
                    continue
                self.logger.error(
                    "Failed to bulk create workspace relationship (%s) from -> '%s' (%s) to -> '%s' (%s)!",
                    relationship_type,
                    from_workspace_name,
                    str(from_workspace_id),
                    to_workspace_name,
                    str(to_workspace_id),
                )
                result["success"] = False
                result["failure_counter"] += 1
            else:
                self.logger.info(
                    "Successfully created bulk workspace relationship (%s) from -> '%s' (%s) to -> '%s' (%s).",
                    relationship_type,
                    from_workspace_name,
                    str(from_workspace_id),
                    to_workspace_name,
                    str(to_workspace_id),
                )
                result["success_counter"] += 1
                # Record the workspace name and ID to allow to read it from failure file
                # and speedup the process.
                if from_workspace_id not in result["relationships"]:
                    # Initialize the "to" list:
                    result["relationships"][from_workspace_id] = [to_workspace_id]
                else:
                    result["relationships"][from_workspace_id].append(to_workspace_id)

        self.logger.info("End working...")

        results.append(result)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="prepare_category_data")
    def prepare_category_data(
        self,
        categories_payload: dict,
        source_node_id: int,
        source_is_document: bool = False,
    ) -> dict | None:
        """Prepare the metadata structure for a new workspace or document.

        This translates the category information from the PAYLOAD into the structure
        required by the OTCS REST API for category updates.

        The difficulty is to get the category schema for the OTCS category. The method tries different
        approaches to get the schema:
        1. Check if the category schema is on a source node (e.g. inherited from a parent node)
        2. Find the category definition by nickname and then assign it to the source node (then retrying approach 1)
        3. Find the category definition by a unique name and then assign it to the source node (then retrying approach 1)

        Args:
            categories_payload (dict):
                The payload information for the workspace or document categories.
            source_node_id (int):
                The item to derive or inherit the category data from. We expect this to
                be a folder, workspace or document that has the category assigned.
            source_is_document (bool, optional):
                If the source node type is a document we need to handle category assignment
                a bit different. Default is False.

        Returns:
            dict | None:
                Category data structure required for subsequent document upload OTCS REST call.

        """

        # Determine the category names in the payload. For this we use a
        # comprehension to create a set (with the curly braces) of unique category
        # names used in the payload that then is converted to a list:
        category_names = list({category["name"] for category in categories_payload})

        # Determine the names of all categories inherited from the source_node_id:
        response = self._otcs_frontend.get_node_categories(node_id=source_node_id)
        if response and response["results"]:
            inherited_category_names = [
                next(iter(item["metadata"]["categories"].values()))["name"]
                for item in response["results"]
                if item.get("metadata")
            ]
        else:
            inherited_category_names = []

        # Calculate the category names we have in payload that are NOT inherited (set difference):
        missing_categories: set[str] = set(category_names) - set(inherited_category_names)

        # Are any of the payload categories NOT inherited from the source_node_id ?
        if missing_categories:
            self.logger.info(
                "Categories -> %s are in payload but not found on source node with ID -> %s. Try to find these categories by nicknames or unique names...",
                str(missing_categories),
                str(source_node_id),
            )
            for category_name in missing_categories:  # category_names:
                category_node_id = None
                # First try via nickname. We expect prefix "cat_" and snake case nicknames:
                category_nickname = (
                    "cat_"
                    + category_name.replace(" - ", "_")
                    .replace("-", "_")
                    .replace(" ", "_")
                    .replace("___", "_")
                    .replace("(", "")
                    .replace(")", "")
                    .lower()
                )
                category_node = self._otcs_frontend.get_node_from_nickname(
                    nickname=category_nickname,
                )
                category_node_id = self._otcs_frontend.get_result_value(
                    response=category_node,
                    key="id",
                )
                if category_node_id:
                    self.logger.info(
                        "Found category -> '%s' with ID -> %s via nickname -> '%s'.",
                        category_name,
                        category_node_id,
                        category_nickname,
                    )
                # Next try via unique names:
                else:
                    response = self._otcs_frontend.get_unique_names(
                        names=[category_name],
                        subtype=self._otcs_frontend.ITEM_TYPE_CATEGORY,
                    )
                    if response and "results" in response and response["results"]:
                        category_node_id = next(
                            (result["NodeId"] for result in response["results"] if result["NodeName"] == category_name),
                            None,
                        )
                        if category_node_id:
                            self.logger.info(
                                "Found category -> '%s' with ID -> %s via unique name lookup!",
                                category_name,
                                category_node_id,
                            )

                # if we found the payload category via nickname or unique name
                # we assign it to thew source node (typically the parent node):
                if category_node_id:
                    self._otcs_frontend.assign_category(
                        node_id=source_node_id,
                        category_id=category_node_id,
                        inheritance=None if source_is_document else True,
                        apply_to_sub_items=not source_is_document,
                    )
            # end for category_name in missing_categories

            # Now with the category assigned to the parent (source node id)
            # We retry getting the inherited category:
            response = self._otcs_frontend.get_node_categories(node_id=source_node_id)
        # end if missing_categories:

        # Initialize the result dict we will return at the end of the method
        # and the list of inherited categories:
        category_data = {}
        inherited_categories = []

        # Redo the test...
        if not response or not response["results"]:
            self.logger.warning(
                "Source node with ID -> %s does not inherit categories but we have category payload! Processing without assiging category...",
                source_node_id,
            )
        else:
            inherited_categories = response["results"]

        # we iterate over all parent categories that are inherited
        # to the new document and try to find matching payload values...
        for inherited_category in inherited_categories:
            # We use the "metadata_order" which is a list of typically one
            # element that includes the category ID:
            metadata_order = inherited_category["metadata_order"]

            # If it is not a list or empty we continue with the
            # next inherited category:
            if not metadata_order["categories"] or not isinstance(
                metadata_order["categories"],
                list,
            ):
                continue
            inherited_category_id = metadata_order["categories"][0]

            # We use the "metadata" dict to determine the category name
            # the keys of the dict are the category ID and attribute IDs
            # the first element in the dict is always the category itself.
            metadata = inherited_category["metadata"]["categories"]
            category_name = metadata[str(inherited_category_id)]["name"]

            self.logger.debug(
                "Source node ID -> %s has category -> '%s' (%s)",
                source_node_id,
                category_name,
                inherited_category_id,
            )

            # The following method returns two values: the category ID and
            # a dict of the attributes. If the category is not found
            # on the parent node it returns -1 for the category ID
            # and an empty dict for the attribute definitions:
            (
                category_id,
                attribute_definitions,
            ) = self._otcs_frontend.get_node_category_definition(
                node_id=source_node_id,
                category_name=category_name,
            )
            if category_id == -1:
                self.logger.error(
                    "The item with ID -> %s does not have the specified category -> '%s' assigned. Skipping...",
                    source_node_id,
                    category_name,
                )
                continue

            # We now initialize the substructure for this particular category:
            category_data[str(category_id)] = {}

            self.logger.debug(
                "Processing the attributes in payload to find values for the inherited category -> '%s' (%s)...",
                category_name,
                category_id,
            )
            # now we fill the prepared (but empty) category_data
            # with the actual attribute values from the payload.
            # For this we traverse all category dicts in the payload
            # and check if they include data for the currently processed
            # category:
            for attribute in categories_payload:
                attribute_name = attribute["attribute"]
                set_name = attribute.get("set", "")
                row = attribute.get("row", "")
                if attribute["name"] != category_name:
                    self.logger.debug(
                        "Attribute -> '%s' does not belong to category -> '%s'. Skipping...",
                        attribute_name,
                        category_name,
                    )
                    continue
                attribute_value = attribute["value"]

                # Set attributes are constructed with <set>:<attribute>
                # by method get_node_category_definition(). This is not
                # an OTCS REST syntax but specific for payload.py
                if set_name:
                    attribute_name = set_name + ":" + attribute_name

                if attribute_name not in attribute_definitions:
                    self.logger.error(
                        "Illegal attribute name -> '%s' in payload for category -> '%s'",
                        attribute_name,
                        category_name,
                    )
                    continue
                attribute_type = attribute_definitions[attribute_name]["type"]
                attribute_id = attribute_definitions[attribute_name]["id"]
                # For multi-line sets the "x" is the placeholder for the
                # row number. We need to replace it with the actual row number
                # given in the payload:
                if "_x_" in attribute_id:
                    if not row:
                        self.logger.error(
                            "Row number is not specified in payload for attribute -> '%s' (%s)",
                            attribute_name,
                            attribute_id,
                        )
                        continue
                    attribute_id = attribute_id.replace("_x_", "_" + str(row) + "_")

                # Special treatment for type user: determine the ID for the login name.
                # the ID is the actual value we have to put in the attribute:
                if attribute_type == "user":
                    user = self._otcs_frontend.get_user(
                        name=attribute_value,
                        show_error=True,
                    )
                    user_id = self._otcs_frontend.get_result_value(
                        response=user,
                        key="id",
                    )
                    if not user_id:
                        self.logger.error(
                            "Cannot find user with login name -> '%s'. Skipping...",
                            attribute_value,
                        )
                        continue
                    attribute_value = user_id
                category_data[str(category_id)][attribute_id] = attribute_value
            # end for attribute in categories_payload:

            # If for none of the attributes of the current category ID a value was found
            # in the payload we remove the dictionary entry to not cause problems
            # for later category updates:
            if not category_data[str(category_id)]:
                del category_data[str(category_id)]
        # end for inherited_category in inherited_categories:

        self.logger.debug("Resulting category data -> %s", str(category_data))

        return category_data

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_documents")
    def process_bulk_documents(self, section_name: str = "bulkDocuments") -> bool:
        """Process bulkDocuments in payload and bulk create them in Content Server (multi-threaded).

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not self._bulk_documents:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        # For efficient idem-potent operation we may want to see which workspaces
        # have already been processed successfully before:
        if self.check_status_file(
            payload_section_name=section_name,
            payload_specific=True,
            prefix="failure_",
        ):
            self.logger.info(
                "Found failure file. Trying to reprocess just the failed ones...",
            )
            # Read payload from where we left it last time
            self._bulk_documents = self.get_status_file(
                payload_section_name=section_name,
                prefix="failure_",
            )
            if not self._bulk_documents:
                self.logger.error(
                    "Cannot load existing bulkDocuments failure file. Bailing out!",
                )
                return False

        success: bool = True

        for bulk_document in self._bulk_documents:
            # Check if element has been disabled in payload (enabled = false).
            # In this case we skip the element:
            enabled = bulk_document.get("enabled", True)
            if not enabled:
                self.logger.info("Payload for Bulk Document is disabled. Skipping...")
                continue

            # The payload element must have a "data_source" key:
            data_source_name = bulk_document.get("data_source", None)
            if not data_source_name:
                self.logger.error("Missing data source name in Bulk Document!")
                success = False
                continue

            copy_data_source = bulk_document.get("copy_data_source", False)
            force_reload = bulk_document.get("force_reload", True)

            # Load and prepare the data source for the bulk processing:
            if copy_data_source:
                self.logger.info(
                    "Take a copy of data source -> %s to avoid side-effects for repeative usage of the data source...",
                    data_source_name,
                )
                data = Data(
                    self.process_bulk_datasource(
                        data_source_name=data_source_name,
                        force_reload=force_reload,
                    ),
                    logger=self.logger,
                )
            else:
                self.logger.info(
                    "Use original data source -> '%s' and not do a copy.",
                    bulk_document["data_source"],
                )
                data = self.process_bulk_datasource(
                    data_source_name=data_source_name,
                    force_reload=force_reload,
                )
            if (
                data is None
                or data.get_data_frame() is None  # the 2nd check is important for the "copy_data_source" case!
            ):  # important to use "is None" here!
                self.logger.error(
                    "Failed to load data source -> '%s' for bulk documents!",
                    data_source_name,
                )
                success = False
                continue

            # Check if fields with list substructures should be exploded.
            # We may want to do this outside the bulkDatasource to only
            # explode for bulkDocuments and not for bulkWorkspaces or
            # bulkWorkspaceRelationships:
            explosions = bulk_document.get("explosions", [])
            for explosion in explosions:
                # explode field can be a string or a list
                # exploding multiple fields at once avoids
                # combinatorial explosions - this is VERY
                # different from exploding columns one after the other!
                if "explode_field" not in explosion and "explode_fields" not in explosion:
                    self.logger.error("Missing explosion field(s)!")
                    continue
                # we want to be backwards compatible...
                explode_fields = (
                    explosion["explode_field"] if "explode_field" in explosion else explosion["explode_fields"]
                )
                flatten_fields = explosion.get("flatten_fields", [])
                split_string_to_list = explosion.get("split_string_to_list", False)
                list_splitter = explosion.get("list_splitter", None)
                self.logger.info(
                    "Starting explosion of bulk documents by field(s) -> %s (type -> %s). Size of data frame before explosion -> %s.",
                    explode_fields,
                    str(type(explode_fields)),
                    str(len(data)),
                )
                data.explode_and_flatten(
                    explode_fields=explode_fields,
                    flatten_fields=flatten_fields,
                    make_unique=False,
                    split_string_to_list=split_string_to_list,
                    separator=list_splitter,
                    reset_index=True,
                )
                self.logger.info(
                    "Size of data frame after explosion -> %s.",
                    str(len(data)),
                )
            # end for explosion in explosions

            # Keep only selected rows if filters are specified in bulkDocuments.
            # We have this _after_ "explosions" to allow access to subfields as well.
            # We have this _before_ "sorting" and "deduplication" as we may keep the wrong
            # rows otherwise (unique / deduplication always keeps the first matching row).
            # We may want to do this outside the bulkDatasource to only
            # filter for bulkDocuments and not for bulkWorkspaces or
            # bulkWorkspaceRelationships:
            filters = bulk_document.get("filters", [])
            if filters:
                data.filter(conditions=filters)

            # Sort the data frame if "sort" specified in payload. We may want to do this to have a
            # higher chance that rows with workspace names that may collapse into
            # one name are put into the same partition. This can avoid race conditions
            # between different Python threads.
            # We do this before deduplication (unique) below as sorting has an influence
            # on which duplicates are kept.
            sort_fields = bulk_document.get("sort", [])
            if sort_fields:
                self.logger.info(
                    "Start sorting of bulk document data frame based on fields (columns) -> %s...",
                    str(sort_fields),
                )
                data.sort(sort_fields=sort_fields, inplace=True)
                self.logger.info(
                    "Sorting of bulk document data frame based on fields (columns) -> %s completed!",
                    str(sort_fields),
                )

            # Check if duplicate rows for given fields should be removed. It is
            # important to do this after sorting as Pandas always keep the first occurance,
            # so ordering plays an important role in deduplication!
            unique_fields = bulk_document.get("unique", [])
            if unique_fields:
                self.logger.info(
                    "Starting deduplication of data frame for bulk documents with unique fields -> %s. Size of data frame before deduplication -> %s.",
                    str(unique_fields),
                    str(len(data)),
                )
                data.deduplicate(unique_fields=unique_fields, inplace=True)
                self.logger.info(
                    "Size of data frame after deduplication -> %s.",
                    str(len(data)),
                )

            # Read name field from payload:
            name_field = bulk_document.get("name", None)
            if not name_field:
                self.logger.error(
                    "Bulk document needs a name field! Skipping to next bulk document...",
                )
                success = False
                continue

            self._log_header_callback(
                text="Process bulk documents -> '{}' from data source -> '{}'".format(
                    name_field,
                    data_source_name,
                ),
                char="-",
            )

            # Read optional description field from payload:
            description_field = bulk_document.get("description", None)

            # Read the optional categories payload:
            categories = bulk_document.get("categories", None)
            if not categories:
                self.logger.info(
                    "Bulk document payload has no category data! Will leave category attributes empty...",
                )

            # Should existing documents be updated? False (= no) is the default.
            operations = bulk_document.get("operations", ["create"])

            self.logger.info(
                "Bulk create documents (name field -> '%s', operations -> %s)...",
                name_field,
                str(operations),
            )

            # See if bulkWorkspace definition has a specific thread number
            # otherwise it is read from a global environment variable
            bulk_thread_number = int(
                bulk_document.get("thread_number", BULK_THREAD_NUMBER),
            )

            partitions = data.partitionate(bulk_thread_number)

            # Create a list to hold the threads
            threads = []
            results = []

            # Define source OTCS object and authenticate once and pass it to all workers if needed
            if bulk_document.get("source_type", "URL").lower() == "contentserver":
                if bulk_document.get("cs_hostname") is None:
                    source_otcs = None
                    self.logger.error(
                        "Required information for source type ContentServer is not configured -> cs_hostname",
                    )
                    success = False
                    continue

                if bulk_document.get("cs_username") is None:
                    source_otcs = None
                    self.logger.error(
                        "Required information for source type ContentServer is not configured -> cs_username",
                    )
                    success = False
                    continue

                if bulk_document.get("cs_password") is None:
                    source_otcs = None
                    self.logger.error(
                        "Required information for source type ContentServer is not configured -> cs_password",
                    )
                    success = False
                    continue

                self.logger.info(
                    "Generating reusable OTCS instance for bulk processing...",
                )
                source_otcs = OTCS(
                    protocol=bulk_document.get("cs_protocol", "https"),
                    hostname=bulk_document.get("cs_hostname"),
                    port=bulk_document.get("cs_port", "443"),
                    base_path=bulk_document.get("cs_basepath", "/cs/cs"),
                    username=bulk_document.get("cs_username"),
                    password=bulk_document.get("cs_password"),
                    logger=self.logger,
                )
                source_otcs.authenticate()
            # end if bulk_document.get("source_type", "URL").lower() == "contentserver"
            else:
                source_otcs = None

            # Create and start a thread for each partition
            for index, partition in enumerate(partitions, start=1):
                # start a thread executing the process_bulk_documents_worker() method below:
                thread = threading.Thread(
                    name=f"{section_name}_{index:02}",
                    target=self.thread_wrapper,
                    args=(
                        self.process_bulk_documents_worker,
                        bulk_document,
                        partition,
                        name_field,
                        description_field,
                        categories,
                        operations,
                        results,
                        source_otcs,
                    ),
                )
                self.logger.info("Starting thread -> '%s'...", str(thread.name))
                thread.start()
                threads.append(thread)
                # Avoid that all threads start at the exact same time with
                # potentially expired cookies that cases race conditions:
                time.sleep(1)
            # end for index, partition in enumerate(partitions, start=1)

            # Wait for all threads to complete
            for thread in threads:
                self.logger.info(
                    "Waiting for thread -> '%s' to complete...",
                    str(thread.name),
                )
                thread.join()
                self.logger.info("Thread -> '%s' has completed.", str(thread.name))

            if "documents" not in bulk_document:
                bulk_document["documents"] = {}

            # Print statistics for each thread. In addition,
            # check if all threads have completed without error / failure.
            # If there's a single failure in on of the thread results we
            # set 'success' variable to False.
            results.sort(key=lambda x: x["thread_name"])
            for result in results:
                self.logger.info(
                    "Thread -> '%s' completed with overall status '%s'.",
                    str(result["thread_name"]),
                    "successful" if result["success"] else "failed",
                )
                self.logger.info(
                    "Thread -> '%s' processed %s data rows with %s successful, %s failed, and %s skipped.",
                    str(result["thread_name"]),
                    str(
                        result["success_counter"] + result["failure_counter"] + result["skipped_counter"],
                    ),
                    str(result["success_counter"]),
                    str(result["failure_counter"]),
                    str(result["skipped_counter"]),
                )
                self.logger.info(
                    "Thread -> '%s' created %s documents, updated %s documents, and deleted %s documents.",
                    str(result["thread_name"]),
                    str(result["create_counter"]),
                    str(result["update_counter"]),
                    str(result["delete_counter"]),
                )

                if not result["success"]:
                    success = False
                # Record all generated documents. This should allow us
                # to restart in case of failures and avoid trying to
                # uploading that have been successfully uploaded before.
                bulk_document["documents"].update(result["documents"])
            # end for result in results
            self._log_header_callback(
                text="Completed processing of bulk documents -> '{}' using data source -> '{}'".format(
                    name_field,
                    data_source_name,
                ),
                char="-",
            )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._bulk_documents,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="construct_file_name")
    def construct_file_name(
        self,
        path: str,
        download_name: str,
        download_name_wildcards: bool = False,
        file_extension: str = "",
    ) -> str | None:
        """Construct the file name of the document.

        This considers the path given in the download_dir, potential wildcards in the
        download_name variable and the file extensions provided (if any).

        Args:
            path (str):
                The base base in the file system. Placeholders need to be resolved before
                calling this method.
            download_name (str):
                The filenname of document in the file system. This may include wildcards
                like '*.pdf' or 'en/*.txt' or '**/*.pfd'. Then the actual download
                file name is determined by a directory traversal using the 'path' parameter.
            download_name_wildcards (bool, optional):
                Defines whether or not wildcards should be replaced in the download name.
                If True this walks through the given path and try to find the file in the
                filesystem that matches the wildcard pattern.
            file_extension (str, optional):
                The file extension - typically 3 letters like 'pdf'. Defaults to "".

        Returns:
            str:
                The file name that is used to find the document in the filesystem (or if it does not yet exist)
                the name that should be used for download. This can be None

        """

        if not download_name:
            self.logger.error("Download name is missing. Cannot construct file name!")
            return None

        if path:
            # Normalize on Linux style path separators. Python is able to handle this also under Windows
            path = path.replace("\\", "/")
            if os.path.exists(path):
                # if we have a path specified it should be a directory and not a file!
                # The isfile() test is only working if the file already exist. It cannot
                # tell the difference between a folder and a file otherwise!
                if os.path.isfile(path):
                    self.logger.warning(
                        "Download directory -> '%s' is pointing to an existing file and not a directory - please check the 'download_dir' variable in payload! Stripping path...",
                        path,
                    )
                    path = os.path.dirname(path)
                    self.logger.info("Stripped path -> '%s'...", path)
            else:
                # if we have a path specified but it does not exist, then create it.
                try:
                    os.makedirs(path)
                except (OSError, PermissionError):
                    self.logger.error(
                        "Cannot create folder -> '%s' in file system!",
                        path,
                    )
                    return None
            if not path.endswith("/"):
                path += "/"  # slashes are save in Linux and Windows. Python handles this correctly

        # If we have a file extension and the given download does
        # not yet ends with it, we add it to the download name:
        if file_extension:
            if not file_extension.startswith("."):
                file_extension = "." + file_extension
            if not download_name.endswith(file_extension):
                download_name += file_extension

        # Also find files with wildcards in 'download_name' if requested
        # by download_name_wildcards == True.
        # IMPORTANT: this only matches ONE file as this method
        # also processes only ONE file at a time:
        if download_name_wildcards and any(char in download_name for char in "*?[]"):
            if not path:
                self.logger.error(
                    "Download name includes wildcards but no (base) path is specified in payload (download_dir missing)! Cannot construct file name!",
                )
                return None
            file_name = None
            # Traverse the directory tree starting at 'path', looking for all files and subdirectories
            for _, _, tmpfiles in os.walk(path):
                # For each file found in the current directory:
                for file_data in tmpfiles:
                    # Check if the current file name matches the 'download_name' pattern using wildcards (fnmatch)
                    if fnmatch.fnmatch(file_data, download_name):
                        self.logger.debug(
                            "Found file name -> '%s' that is matching download name pattern -> '%s'",
                            file_data,
                            download_name,
                        )
                        # Construct the full file path by concatenating the base 'path' and the file name
                        file_name = path + file_data
                        # If we found a match we stop the (inner loop)
                        break
        # end if download_name_wildcards and any(char in download_name for char in "*?[]")
        else:
            # We have a normal filename without wildcards:
            file_name = path + download_name

        return file_name

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="download_bulk_document")
    def download_bulk_document(
        self,
        bulk_document: dict,
        download_name: str,
        row: pd.Series,
        result: dict,
        source_otcs: OTCS | None = None,
    ) -> tuple[str, str]:
        """Download the bulk document and determine the file name (with path) and the mime type.

        Args:
            bulk_document (dict):
                The payload item for the bulk document.
            download_name (str):
                The download name of the document.
            row (pd.Series):
                The current data frame series (row).
            result (dict):
                The result dictionary (mutable).
            source_otcs (OTCS | None, optional):
                The Content Server OTCS object if documents should be loaded
                from Content Server. Defaults to None.

        Returns:
            tuple[str, str]:
                The file name (first) and the mime type (second).

        """

        #
        # 1. Construct the local file name to upload:
        #

        path = bulk_document.get("download_dir", BULK_DOCUMENT_PATH)
        path = self.replace_bulk_placeholders(input_string=path, row=row)
        self.logger.debug("Download path for bulk document -> '%s'", path)

        # Determine file extensions:
        file_extension = bulk_document.get("file_extension", "")
        if file_extension:
            file_extension = self.replace_bulk_placeholders(
                input_string=file_extension,
                row=row,
            )
        file_extension_alt = bulk_document.get("file_extension_alt", "")
        if file_extension_alt:
            file_extension_alt = self.replace_bulk_placeholders(
                input_string=file_extension_alt,
                row=row,
            )

        # Determine file names:
        file_name = self.construct_file_name(
            path=path,
            download_name=download_name,
            download_name_wildcards=bulk_document.get("download_name_wildcards", False),
            file_extension=file_extension,
        )
        self.logger.debug("Constructed file name -> '%s'", file_name)
        if file_extension_alt and file_extension_alt != file_extension:
            file_name_alt = self.construct_file_name(
                path=path,
                download_name=download_name,
                download_name_wildcards=bulk_document.get(
                    "download_name_wildcards",
                    False,
                ),
                file_extension=file_extension_alt,
            )
        else:
            file_name_alt = file_name

        # If we couldn't construct the file name nor an alternative file name, we bail out:
        if not file_name and not file_name_alt:
            self.logger.error(
                "Cannot determine file name for document download (path -> '%s', download name -> '%s')!",
                path,
                download_name,
            )
            return None, None

        #
        # 2. Construct the mime type of the file to upload:
        #

        mime_type = bulk_document.get("mime_type", "application/pdf")
        mime_type = self.replace_bulk_placeholders(input_string=mime_type, row=row)
        mime_type_alt = bulk_document.get("mime_type_alt", "text/html")
        if mime_type_alt != "text/html":
            # if it is not the default it may have placeholders:
            mime_type_alt = self.replace_bulk_placeholders(
                input_string=mime_type_alt,
                row=row,
            )

        #
        # 3. Check if the file name(s) do(es) already exist and
        #    if we want to delete existing files (to download fresh ones)
        #

        file_exists = os.path.exists(file_name) if file_name else False

        # make sure there's no name conflict with stale documents:
        delete_download = bulk_document.get("delete_download", True)
        if file_exists and delete_download:
            os.remove(file_name)
            file_exists = False

        # Careful: If the construction of file name and the alternative
        # file name leads to identical result then there's actually
        # no alternative file name!
        if file_name != file_name_alt:
            file_exists_alt = os.path.exists(file_name_alt) if file_name_alt else False
            # make sure there's no name conflict with stale documents:
            if file_exists_alt and delete_download:
                os.remove(file_name_alt)
                file_exists_alt = False
        else:
            file_exists_alt = False

        #
        # 4. if the file does not exist in the local file system
        #    we now go and download it...
        #

        if not file_exists and not file_exists_alt:
            self.logger.debug("File -> '%s' does not exist in filesystem. We need to download it.", file_name)
            # Read "download retry number" and "wait before retry" duration from payload
            # (if specified) otherwise set default values
            wait_time = bulk_document.get("download_wait_time", 30)
            retries = bulk_document.get("download_retries", 2)

            source_type = bulk_document.get("source_type", "URL").lower()
            match source_type:
                case "contentserver":
                    # Functionality to download the document from Content Server
                    cs_source_id = bulk_document.get("cs_source_id", "")
                    cs_source_id = self.replace_bulk_placeholders(
                        input_string=cs_source_id,
                        row=row,
                    )
                    if source_otcs is not None and source_otcs.otcs_ticket() is not None:
                        self.logger.info(
                            "Downloading document from source Content Server with ID -> %s...",
                            cs_source_id,
                        )

                        if source_otcs.download_document(
                            node_id=cs_source_id,
                            file_path=file_name,
                        ):
                            self.logger.debug(
                                "Successfully downloaded from Content Server using URL -> %s with ID -> %s to local file -> '%s'",
                                source_otcs.cs_public_url,
                                cs_source_id,
                                file_name,
                            )
                        else:
                            self.logger.error(
                                "Cannot download file from Content Server using URL -> %s with ID -> %s to local file -> '%s'. Skipping...",
                                source_otcs.cs_public_url(),
                                cs_source_id,
                                file_name,
                            )
                            result["skipped_counter"] += 1
                            return None, None
                    else:
                        self.logger.error(
                            "Cannot download file with ID -> %s from Content Server. OTCS object not configured. Skipping...",
                            cs_source_id,
                        )
                        result["skipped_counter"] += 1
                        return None, None

                case "url":
                    # Default case, download from accessible URL
                    download_url = bulk_document.get("download_url")
                    if download_url:
                        download_url = self.replace_bulk_placeholders(
                            input_string=download_url,
                            row=row,
                        )
                    if not download_url:
                        self.logger.error(
                            "Download URL missing and no existing file -> '%s' in file system!",
                            file_name,
                        )
                        result["skipped_counter"] += 1
                        return None, None
                    # Use the HHTP class to download the file:
                    if not self._http_object.download_file(
                        url=download_url,
                        filename=file_name,
                        retries=retries,
                        wait_time=wait_time,
                        wait_on_status=[403],
                        show_error=False,
                    ):
                        # Fetch alternative download URL (if avialable)
                        download_url_alt = bulk_document.get("download_url_alt")
                        if download_url_alt:
                            download_url_alt = self.replace_bulk_placeholders(
                                input_string=download_url_alt,
                                row=row,
                            )
                        # Check if we have an alternative download URL we try this now:
                        if download_url_alt:
                            self.logger.warning(
                                "Cannot download file from -> %s to local file -> '%s'. Trying alternative download -> %s to file -> '%s'...",
                                download_url,
                                file_name,
                                download_url_alt,
                                file_name_alt,
                            )
                            if self._http_object.download_file(
                                url=download_url_alt,
                                filename=file_name_alt,
                                retries=retries,
                                wait_time=wait_time,
                                wait_on_status=[403],
                                show_error=False,
                            ):
                                self.logger.debug(
                                    "Successfully downloaded file from alternative URL -> %s to local file -> '%s'. Using this file...",
                                    download_url_alt,
                                    file_name_alt,
                                )
                                mime_type = mime_type_alt
                                file_name = file_name_alt
                            else:
                                # as we cannot fully rely one data source we don't treat this
                                # as an error but a warning:
                                self.logger.warning(
                                    "Cannot download file from alternative URL -> %s to local file -> '%s'. Skipping...",
                                    download_url_alt,
                                    file_name_alt,
                                )
                                result["skipped_counter"] += 1
                                return None, None
                        # end if download_url_alt
                        else:
                            # as we cannot fully rely one data source we don't treat this
                            # as an error but a warning:
                            self.logger.warning(
                                "Cannot download file from URL -> %s to local file -> '%s'. Skipping...",
                                download_url,
                                file_name,
                            )
                            result["skipped_counter"] += 1
                            return None, None
                    else:
                        self.logger.debug(
                            "Successfully downloaded file from -> %s to local file -> '%s'",
                            download_url,
                            file_name,
                        )
        # end if not file_exists and not file_exists_alt
        else:  # we already have a local file to reuse
            # If we found the alternative file name in file system
            # but not the regular one, then we switch to alternative
            # file name:
            if not file_exists and file_exists_alt:
                file_name = file_name_alt
                mime_type = mime_type_alt
            self.logger.info(
                "Reusing existing file -> '%s' in local storage.",
                file_name,
            )

        return file_name, mime_type

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_bulk_document_location")
    def get_bulk_document_location(
        self,
        workspace: dict,
        row: pd.Series,
        index: int,
        replacements: dict,
    ) -> tuple[int | None, bool]:
        """Determine the workspace location to store a document in bulk processing.

        This method is used by bulk document and bulk item workers.

        Args:
            workspace (dict):
                The payload for the workspace inside bulk document.
            row (pd.Series):
                The current row in the data frame.
            index (int):
                The index of current row in the data frame.
            replacements (dict):
                The replacement configuration for placeholders.

        Returns:
            int | None:
                Parent ID of folder or workspace to upload the document to or None in case of
                an error or insufficient payload information causing the workspace to be skipped.
            bool:
                True, if success. False if a failure has happened.

        """

        success = True

        if "workspace_name" not in workspace:
            self.logger.error(
                "No workspace name field specified for document upload! Cannot upload document to this workspace...",
            )
            success = False
            return None, success
        workspace_name_field = workspace["workspace_name"]
        workspace_name_field_alt = workspace.get("workspace_name_alt")

        workspace_name = self.replace_bulk_placeholders(
            input_string=workspace_name_field,
            row=row,
            replacements=replacements,
        )
        # If we cannot get the workspace name from the
        # workspace_name_field we try the alternative name field
        # (if specified in payload via "workspace_name_alt"):
        if not workspace_name and workspace_name_field_alt:
            self.logger.debug(
                "Row -> %s does not have the data to resolve the placeholders in workspace name field -> %s! Trying alternative name field -> %s...",
                str(index),
                workspace_name_field,
                workspace_name_field_alt,
            )
            workspace_name = self.replace_bulk_placeholders(
                input_string=workspace_name_field_alt,
                row=row,
                replacements=replacements,
            )

        # it could be that the current data row does not have the
        # required fields to resolve the workspace name placeholders
        # then we skip uploading the document to this workspace
        # but still keep status as successful (don't set success = False)
        if not workspace_name:
            self.logger.warning(
                "Row -> %s does not have the required data to resolve workspace name field -> '%s' specified for document upload! Skipping document upload to this workspace...",
                str(index),
                workspace_name_field,
            )
            # We keep success = True as this is a data problem and not a config problem!
            return None, success

        # Cleanse the workspace name (allowed characters, maximum length):
        workspace_name = OTCS.cleanse_item_name(workspace_name)

        # Check if all data conditions to create the workspace are met
        conditions = workspace.get("conditions")
        if conditions:
            evaluated_condition = self.evaluate_conditions(
                conditions=conditions,
                row=row,
            )
            if not evaluated_condition:
                self.logger.info(
                    "Workspace condition for row -> %s not met. Skipping row for document upload to workspace -> '%s'...",
                    str(index),
                    workspace_name,
                )
                # We keep success = True as this is a data problem and not a config problem!
                return None, success

        if "workspace_type" not in workspace:
            self.logger.error(
                "No workspace type specified for document upload! Skipping workspace -> '%s'...",
                workspace_name,
            )
            success = False
            return None, success
        workspace_type = workspace["workspace_type"]
        workspace_type = self.replace_bulk_placeholders(
            input_string=workspace_type,
            row=row,
            replacements=replacements,
        )
        workspace_data_source_name = workspace.get("data_source")
        # Try to find the workspace by name/synonym and type:
        (workspace_id, workspace_name) = self.process_bulk_workspaces_lookup(
            workspace_name=workspace_name,
            workspace_type=workspace_type,
            data_source_name=workspace_data_source_name,
        )
        if not workspace_id:
            self.logger.warning(
                "Cannot find workspace with name/synonym -> '%s' and type -> '%s'.",
                workspace_name,
                workspace_type,
            )
            success = False
            return None, success

        # If the workspace payload element has a "data_source" key,
        # then add all columns from the given data source to the bulk
        # document row to also support the lookup of values from the workspace
        # data source. These fields get a "lookup_" prefix to avoid name clashes.
        # the values must be specified with this "lookup_" prefix in the payload.
        # We CANNOT do this at the very beginning of the workspace loop as we
        # need the workspace_name to be properly resolved (incl. synonyms):
        if workspace_data_source_name:
            self.logger.info(
                "Workspace for bulk documents has a data source -> '%s' with lookup values. Adding them as row columns...",
                workspace_data_source_name,
            )
            workspace_data_source = next(
                (item for item in self._bulk_datasources if item["name"] == workspace_data_source_name),
                None,
            )
            # Read the synonym column and the name column from the data source payload item:
            workspace_data_source_name_column = workspace_data_source.get(
                "name_column",
                None,  # e.g. "Name"
            )

            if workspace_data_source_name_column:
                # Get additional data from workspace data source
                # for lookups. Synonyms are already resolved at
                # this point in time (workspace_name has been updated above
                # in case the initial workspace name wasn't found)
                lookup_row = self.lookup_data_source_value(
                    data_source=workspace_data_source,
                    lookup_column=workspace_data_source_name_column,
                    lookup_value=workspace_name,
                )

                # Adding all values of the lookup row with the prefix lookup_ to the bulk documents row
                # for replacement of placeholders:
                if lookup_row is not None:
                    for k, value in lookup_row.items():
                        row["lookup_" + k] = value

        # "workspace_folder" can be used if the payload contains
        # the path as a comma-separated string (top down)
        # otherwise "workspace_path" has precedence over "workspace_folder"
        workspace_folder = workspace.get("workspace_folder", "")
        workspace_path = workspace.get("workspace_path")

        if workspace_folder and not workspace_path:
            workspace_folder = self.replace_bulk_placeholders(
                input_string=workspace_folder,
                row=row,
                replacements=replacements,
            )
            workspace_path = (
                [item.strip() for item in workspace_folder.split(",")]
                if "," in workspace_folder
                else [workspace_folder]
            )

        if workspace_path:
            if isinstance(workspace_path, str):
                # if the path is actually a list in a string
                # we need to convert it to a python list in a safe way:
                try:
                    workspace_path = self.replace_bulk_placeholders(
                        input_string=workspace_path,
                        index=None,  # None is VERY important as otherwise index=0 is the default and we only get the first element
                        row=row,
                        replacements=replacements,
                    )
                    workspace_path = literal_eval(workspace_path) if workspace_path else None
                except (SyntaxError, ValueError):
                    self.logger.error(
                        "Cannot parse list-type folder path wrapped in string -> '%s'!",
                        workspace_path,
                    )
                    workspace_path = None
            elif isinstance(workspace_path, list):
                # We create a copy list to not modify original payload
                workspace_path = list(workspace_path)
                # Replace placeholders in payload for the path elements:
                # Note: workspace_path is a mutable data type that is changed in place!
                result_placeholders = self.replace_bulk_placeholders_list(
                    input_list=workspace_path,
                    row=row,
                    replacements=replacements,
                )
                if not result_placeholders:
                    workspace_path = None
            else:
                self.logger.warning("Unsupported data type for workspace path!")
                workspace_path = None

            if not workspace_path:
                # we put the document into the root of the workspace
                # if we couldn't determine a path inside the workspace:
                self.logger.info(
                    "Workspace folder path for workspace -> '%s' of workspace type -> '%s' has no value. Using workspace root for document upload.",
                    workspace_name,
                    workspace_type,
                )
                parent_id = workspace_id
            else:
                # Check if the folder path does already exist and get the target folder at the end of the path:
                self.logger.info(
                    "Check if path -> %s does already exist in workspace -> '%s' (%s) or otherwise create it...",
                    str(workspace_path),
                    workspace_name,
                    workspace_id,
                )
                response = self._otcs_frontend.get_node_by_workspace_and_path(
                    workspace_id=workspace_id,
                    path=workspace_path,
                    create_path=True,  # we want the path to be created if it doesn't exist
                    show_error=False,
                )
                parent_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if not parent_id:
                    self.logger.error(
                        "Failed to create path -> %s in workspace -> '%s' (%s)!",
                        str(workspace_path),
                        workspace_name,
                        workspace_id,
                    )
                    success = False
                    return None, success
                else:
                    self.logger.info(
                        "Using path -> %s inside workspace -> '%s' (%s). Node ID for target folder -> %s.",
                        str(workspace_path),
                        workspace_name,
                        workspace_id,
                        str(parent_id),
                    )
        # end if workspace_path
        else:
            self.logger.info(
                "Workspace folder path for workspace -> '%s' of workspace type -> '%s' is not specified. Using workspace root for document upload.",
                workspace_name,
                workspace_type,
            )
            # we put the document into the root of the workspace:
            parent_id = workspace_id

        # Check if we have sub-workspaces configured.
        # If not, then we are done and can return the found
        # parent ID for the document:
        if "sub_workspace_type" not in workspace or "sub_workspace_name" not in workspace:
            self.logger.debug(
                "No sub workspace configured in payload. Return parent ID -> %s",
                parent_id,
            )
            return parent_id, success

        #
        # Determine (or create) the Sub workspace:
        #

        sub_workspace_type = workspace["sub_workspace_type"]
        sub_workspace_type = self.replace_bulk_placeholders(
            input_string=sub_workspace_type,
            row=row,
            replacements=replacements,
        )
        sub_workspace_name = workspace["sub_workspace_name"]
        sub_workspace_name = self.replace_bulk_placeholders(
            input_string=sub_workspace_name,
            row=row,
            replacements=replacements,
        )
        response = self._otcs_frontend.get_node_by_parent_and_name(
            name=sub_workspace_name,
            parent_id=parent_id,
        )
        # Check if the sub-workspaces does already exist:
        sub_workspace_id = self._otcs_frontend.get_result_value(
            response=response,
            key="id",
        )
        # Sub workspaces are dynamically created
        # during the processing of bulk documents...
        if not sub_workspace_id:
            self.logger.info(
                "Creating sub workspace -> '%s' of type -> '%s' and parent -> %s...",
                sub_workspace_name,
                sub_workspace_type,
                parent_id,
            )
            sub_workspace_template = workspace.get("sub_workspace_template", "")

            sub_workspace_template = self.replace_bulk_placeholders(
                input_string=sub_workspace_template,
                row=row,
                replacements=replacements,
            )
            # Now we try to determine the IDs for the sub-workspace type and template:
            (sub_workspace_type_id, sub_workspace_template_id) = self.determine_workspace_type_and_template_id(
                workspace_type_name=sub_workspace_type,
                workspace_template_name=sub_workspace_template,
            )
            # if either of the two couldn't be determined we cannot create the sub-workspace
            if not sub_workspace_type_id or not sub_workspace_template_id:
                self.logger.error(
                    "Couldn't dertermine workspace template ID and workspace type ID of sub-workspace!",
                )
                success = False
                return None, success

            # Check if we have categories for the sub-workspace:
            if "categories" not in workspace:
                self.logger.debug(
                    "Sub-Workspace payload has no category data! Will leave category attributes empty...",
                )
                sub_workspace_category_data = {}
            else:
                self.logger.debug(
                    "Sub-Workspace payload has category data! Will prepare category data for workspace creation...",
                )
                worker_categories = self.process_bulk_categories(
                    row=row,
                    index=index,
                    categories=workspace["categories"],
                    replacements=replacements,
                )
                self.logger.debug(
                    "Prepare category data for sub-workspace with template -> %s and parent -> %s",
                    sub_workspace_template_id,
                    parent_id,
                )
                sub_workspace_category_data = self.prepare_workspace_create_form(
                    categories=worker_categories,
                    template_id=sub_workspace_template_id,
                    parent_workspace_node_id=parent_id,
                )
                if not sub_workspace_category_data:
                    self.logger.error(
                        "Failed to prepare the category data for sub-workspace -> '%s'!",
                        sub_workspace_name,
                    )
                    success = False
                    return None, success
            # Now we create the sub-workspace:
            response = self._otcs_frontend.create_workspace(
                workspace_template_id=sub_workspace_template_id,
                workspace_name=sub_workspace_name,
                workspace_description="",
                workspace_type=sub_workspace_type_id,
                category_data=sub_workspace_category_data,
                parent_id=parent_id,
                show_error=False,
            )
            if response is None:
                # Potential race condition: see if the sub-workspace has been created by a concurrent thread.
                # So we better check if the workspace is there even if the create_workspace() call delivered
                # a 'None' response:
                response = self._otcs_frontend.get_node_by_parent_and_name(
                    parent_id=parent_id,
                    name=sub_workspace_name,
                )
            sub_workspace_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
            if not sub_workspace_id:
                self.logger.error(
                    "Failed to create sub-workspace -> '%s' with type ID -> %s under parent with ID -> %s!",
                    sub_workspace_name,
                    sub_workspace_type_id,
                    parent_id,
                )
                parent_id = None
                success = False
                return None, success
            else:
                self.logger.info(
                    "Successfully created sub-workspace -> '%s' (%s).",
                    sub_workspace_name,
                    sub_workspace_id,
                )

            # Create Business Relationship between workspace and sub-workspace:
            if workspace_id and sub_workspace_id:
                # Check if workspace relationship does already exist in OTCS
                # (this is an additional safety measure to avoid errors):
                response = self._otcs_frontend.get_workspace_relationships(
                    workspace_id=workspace_id,
                    related_workspace_name=sub_workspace_name,
                )
                current_workspace_relationships = self._otcs_frontend.exist_result_item(
                    response=response,
                    key="id",
                    value=sub_workspace_id,
                )
                if current_workspace_relationships:
                    self.logger.info(
                        "Workspace relationship between workspace -> '%s' (%s) and sub-workspace -> '%s' (%s) does already exist. Skipping...",
                        workspace_name,
                        workspace_id,
                        sub_workspace_name,
                        sub_workspace_id,
                    )
                else:
                    self.logger.info(
                        "Create workspace relationship %s -> %s...",
                        workspace_id,
                        sub_workspace_id,
                    )
                    response = self._otcs_frontend.create_workspace_relationship(
                        workspace_id=workspace_id,
                        related_workspace_id=sub_workspace_id,
                        show_error=False,  # we don't want to show an error because of race conditions handled below
                    )
                    if response:
                        self.logger.info(
                            "Successfully created workspace relationship between workspace ID -> %s and sub-workspace ID -> %s.",
                            workspace_id,
                            sub_workspace_id,
                        )
                    else:
                        # Potential race condition: see if the workspace-2-sub-workspace relationship has been created by a concurrent thread.
                        # So we better check if the relationship is there even if the create_workspace_relationship() call delivered
                        # a 'None' response:
                        response = self._otcs_frontend.get_workspace_relationships(
                            workspace_id=workspace_id,
                            related_workspace_name=sub_workspace_name,
                        )
                        current_workspace_relationships = self._otcs_frontend.exist_result_item(
                            response=response,
                            key="id",
                            value=sub_workspace_id,
                        )
                        if not current_workspace_relationships:
                            self.logger.error(
                                "Failed to create workspace relationship between workspace ID -> %s and sub-workspace ID -> %s!",
                                workspace_id,
                                sub_workspace_id,
                            )
                        else:
                            self.logger.info(
                                "Successfully created workspace relationship between workspace ID -> %s and sub-workspace ID -> %s.",
                                workspace_id,
                                sub_workspace_id,
                            )

        # end if sub-workspace does not exist
        else:
            self.logger.info(
                "Using existing sub workspace -> '%s' (%s) of type -> '%s'...",
                sub_workspace_name,
                str(sub_workspace_id),
                sub_workspace_type,
            )

        #
        # Get the target folder in the sub-workspace by the provided sub workspace path
        #

        # "sub_workspace_folder" can be used if the payload contains
        # the path as a comma-separated string (top down)
        sub_workspace_folder = workspace.get("sub_workspace_folder", "")
        sub_workspace_path = workspace.get("sub_workspace_path")

        if not sub_workspace_path and sub_workspace_folder:
            sub_workspace_folder = self.replace_bulk_placeholders(
                input_string=sub_workspace_folder,
                row=row,
                replacements=replacements,
            )
            sub_workspace_path = (
                [item.strip() for item in sub_workspace_folder.split(",")]
                if "," in sub_workspace_folder
                else [sub_workspace_folder]
            )

        if sub_workspace_path:
            if isinstance(sub_workspace_path, str):
                # if the path is actually a list in a string
                # we need to convert it to a python list in a safe way:
                try:
                    sub_workspace_path = self.replace_bulk_placeholders(
                        input_string=sub_workspace_path,
                        index=None,  # None is VERY important as otherwise index=0 is the default and we only get the first element
                        row=row,
                        replacements=replacements,
                    )
                    sub_workspace_path = literal_eval(sub_workspace_path) if sub_workspace_path else None
                except (SyntaxError, ValueError):
                    self.logger.error(
                        "Cannot parse list-type folder path wrapped in string -> '%s'!",
                        sub_workspace_path,
                    )
                    sub_workspace_path = None
            elif isinstance(sub_workspace_path, list):
                # We create a copy list to not modify original payload
                sub_workspace_path = list(sub_workspace_path)
                # Replace placeholders in payload for the path elements:
                # Note: sub_workspace_path is a mutable data type that is changed in place!
                result_placeholders = self.replace_bulk_placeholders_list(
                    input_list=sub_workspace_path,
                    row=row,
                    replacements=replacements,
                )
                if not result_placeholders:
                    sub_workspace_path = None
            else:
                self.logger.warning("Unsupported data type for sub workspace path!")
                sub_workspace_path = None

            if not sub_workspace_path:
                self.logger.warning(
                    "Sub-Workspace folder path for sub workspace -> '%s' of sub workspace type -> '%s' cannot be resolved (placeholder issue). Using sub workspace root for document upload.",
                    sub_workspace_name,
                    sub_workspace_type,
                )
                # we put the document into the root of the workspace:
                parent_id = sub_workspace_id
            else:
                # Check if the folder path does already exist and get the target folder at the end of the path:
                self.logger.info(
                    "Check if path -> %s does already exist in workspace -> '%s' (%s)... (otherwise create it)",
                    str(sub_workspace_path),
                    sub_workspace_name,
                    sub_workspace_id,
                )
                response = self._otcs_frontend.get_node_by_workspace_and_path(
                    workspace_id=sub_workspace_id,
                    path=sub_workspace_path,
                    create_path=True,  # we want the path to be created if it doesn't exist
                    show_error=False,
                )
                parent_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if not parent_id:
                    self.logger.error(
                        "Failed to create path -> %s in sub workspace -> '%s' (%s)!",
                        str(sub_workspace_path),
                        sub_workspace_name,
                        sub_workspace_id,
                    )
                    success = False
                    return None, success
                else:
                    self.logger.info(
                        "Successfully created path -> %s in sub-workspace -> '%s' (%s). Node ID for target folder -> %s.",
                        str(sub_workspace_path),
                        sub_workspace_name,
                        sub_workspace_id,
                        str(parent_id),
                    )
        else:
            self.logger.info(
                "Folder path inside sub-workspace -> '%s' with sub workspace type -> '%s' is not specified. Using root of sub-workspace for document upload.",
                sub_workspace_name,
                sub_workspace_type,
            )
            # we put the document into the root of the workspace:
            parent_id = sub_workspace_id

        return parent_id, success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_documents_worker")
    def process_bulk_documents_worker(
        self,
        bulk_document: dict,
        partition: pd.DataFrame,
        name_field: str,
        description_field: str,
        categories: list | None = None,
        operations: list | None = None,
        results: list | None = None,
        source_otcs: OTCS | None = None,
    ) -> None:
        """Thread worker to download + create documents in bulk.

        Each worker thread gets a partition of the rows that include
        the data required for the document creation.

        Args:
            bulk_document (dict):
                The bulkDocument payload element.
            partition (pd.DataFrame):
                Data partition with rows to process by this thread.
            name_field (str):
                The payload field where the document name is stored.
            description_field (str):
                The payload field where the document description is stored.
            categories (list, optional):
                A list of category dictionaries.
            operations (list, optional):
                A list of operations that should be applyed on workspaces.
                Possible values: "create", "update", "delete", "recreate".
            results (list, optional):
                A mutable list of thread results.
            source_otcs (OTCS | None, optional):
                Provide the OTCS object if a direct download from Content Server is done.

        """

        thread_id = threading.get_ident()
        thread_name = threading.current_thread().name

        self.logger.info(
            "Start working on data frame partition of size -> %s to bulk create documents...",
            str(len(partition)),
        )

        # Avoid linter warnings - so make parameter default None while we
        # actually want ["create"] to be the default:
        if operations is None:
            operations = ["create"]

        # get the specific update operations given in the payload
        # if not specified we do the following update operations.
        # The 'purge' operation needs to be specified explicitly.
        update_operations = bulk_document.get(
            "update_operations",
            ["name", "description", "categories", "nickname", "version"],
        )

        result = {}
        result["thread_id"] = thread_id
        result["thread_name"] = thread_name
        result["success_counter"] = 0
        result["failure_counter"] = 0
        result["skipped_counter"] = 0
        result["create_counter"] = 0
        result["update_counter"] = 0
        result["delete_counter"] = 0
        result["documents"] = {}
        result["success"] = True

        # Check if documents have been processed before, e.i. testing
        # if a "documents" key exists and if it is pointing to a non-empty list:
        # Additionally we check that workspace updates are not enforced:
        if (
            bulk_document.get("documents")
            and "update" not in operations
            and "delete" not in operations
            and "recreate" not in operations
        ):
            existing_documents = bulk_document["documents"]
            self.logger.info(
                "Found %s already processed documents. Try to complete the job...",
                str(len(existing_documents)),
            )
        else:
            existing_documents = {}

        # See if external creation and modification dates are in the payload data:
        external_modify_date_field = bulk_document.get("external_modify_date")
        external_create_date_field = bulk_document.get("external_create_date")

        # See if we have a key field to uniquely identify an existing document:
        key_field = bulk_document.get("key")

        # Get dictionary of replacements for bulk document creations
        # this we will be used of all places data is read from the
        # data frame. Each dictionary item has the field name as the
        # dictionary key and a list of regular expressions as dictionary value
        replacements = bulk_document.get("replacements")

        # In case the name cannot be resolved we allow to
        # specify an alternative name field in the payload.
        name_field_alt = bulk_document.get("name_alt")

        # If download_name field is not in payload we use name_field instead.
        # It can still be that download_name is "" as name_field is only
        # used if the entry for "download_name" is not in payload at all.
        download_name_field = bulk_document.get("download_name", name_field)

        # Determine if we want to automatically extract ZIP files and upload
        # them as folders / documents:
        extract_zip = bulk_document.get("extract_zip", False)

        # Document names are limited in terms of allowed characters.
        # E.g. if you don't want any path elements and "/" dividers
        # in the document name you could use name_regex = r".*/" in the payload.
        document_name_additional_regex_list = [bulk_document.get("name_regex", r"")]

        # Fetch the nickname field from the payload (if it is specified):
        nickname_field = bulk_document.get("nickname")

        # Nicknames are very limited in terms of allowed characters.
        # For nicknames we need additional regexp as we need to
        # replace all non-alphanumeric, non-space characters with ""
        # We also preserve hyphens in the first step to replace
        # them below with underscores. This is important to avoid
        # that different spellings of names produce different nicknames.
        # We want spellings with spaces match spellings with hyphens.
        # For this the workspace names have a regexp "-| " in the payload.
        nickname_additional_regex_list = [r"[^\w\s-]"]

        total_rows = len(partition)

        # Process all rows in the partition that was given to this thread:
        for index, row in partition.iterrows():
            # Calculate percentage of completion:
            percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

            self.logger.info(
                "Processing data row -> %s for bulk document creation (%.2f %% complete)...",
                str(index),
                percent_complete,
            )

            # Clear variables to ensure clean state for each row:
            parent_id = None
            document_id = None

            # Create a copy of the mutable operations list as we may
            # want to modify it per row:
            row_operations = list(operations)

            # Check if all data conditions to create the document are met
            conditions = bulk_document.get("conditions")
            if conditions:
                evaluated_condition = self.evaluate_conditions(
                    conditions=conditions,
                    row=row,
                )
                if not evaluated_condition:
                    self.logger.info(
                        "Condition for bulk document row -> %s not met. Skipping row for document creation...",
                        str(index),
                    )
                    result["skipped_counter"] += 1
                    continue

            # Check if all data conditions to create the document are met:
            if "create" in row_operations or "recreate" in row_operations:
                conditions_create = bulk_document.get("conditions_create")
                if conditions_create:
                    evaluated_conditions_create = self.evaluate_conditions(
                        conditions=conditions_create,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_create:
                        self.logger.info(
                            "Create condition for bulk document row -> %s not met. Excluding create operation for current row...",
                            str(index),
                        )
                        if "create" in row_operations:
                            row_operations.remove("create")
                        if "recreate" in row_operations:
                            row_operations.remove("recreate")
                elif (
                    "recreate" in row_operations
                ):  # we still create and recreate without conditions_create. But give a warning for 'recreate' without condition.
                    self.logger.warning(
                        "No create condition provided but 'recreate' operation requested. This will recreate all existing documents!",
                    )

            # Check if all data conditions to delete the document are met:
            if "delete" in row_operations:
                conditions_delete = bulk_document.get("conditions_delete")
                if conditions_delete:
                    evaluated_conditions_delete = self.evaluate_conditions(
                        conditions=conditions_delete,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_delete:
                        self.logger.info(
                            "Delete condition for bulk document row -> %s not met. Excluding delete operation for current row...",
                            str(index),
                        )
                        row_operations.remove("delete")
                else:  # without delete_conditions we don't delete!!
                    self.logger.warning(
                        "Delete operation requested for bulk documents but conditions for deletion are missing! (specify 'conditions_delete'!)",
                    )
                    row_operations.remove("delete")

            # Check if all data conditions to update the document are met:
            if "update" in row_operations:
                conditions_update = bulk_document.get("conditions_update")
                if conditions_update:
                    evaluated_conditions_update = self.evaluate_conditions(
                        conditions=conditions_update,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_update:
                        self.logger.info(
                            "Update condition for bulk document row -> %s not met. Excluding update operation for current row...",
                            str(index),
                        )
                        row_operations.remove("update")

            # Determine the document name:
            document_name = self.replace_bulk_placeholders(
                input_string=name_field,
                row=row,
                replacements=replacements,
                additional_regex_list=document_name_additional_regex_list,
            )
            # If we cannot get the document_name from the
            # name_field we try the alternative name field
            # (if specified in payload via "name_alt"):
            if not document_name and name_field_alt:
                self.logger.debug(
                    "Row -> %s does not have the data to resolve the placeholders in document name -> %s! Trying alternative name field -> %s...",
                    str(index),
                    name_field,
                    name_field_alt,
                )
                # The additional regex list will make sure
                # that any path or URL element is stripped from
                # the value in the row element:
                document_name = self.replace_bulk_placeholders(
                    input_string=name_field_alt,
                    row=row,
                    replacements=replacements,
                    additional_regex_list=document_name_additional_regex_list,
                )
            if not document_name:
                self.logger.error(
                    "Row -> %s does not have the data to resolve the placeholders in document name -> %s%s!",
                    str(index),
                    name_field,
                    (" nor in alternative name field -> " + name_field_alt if name_field_alt else ""),
                )
                result["skipped_counter"] += 1
                continue

            # Cleanse the document name (allowed characters, maximum length):
            document_name = OTCS.cleanse_item_name(document_name)

            download_name = ""
            if download_name_field:
                # The additional regex list will make sure
                # that any path or URL element is stripped from
                # the value in the row element:
                download_name = self.replace_bulk_placeholders(
                    input_string=download_name_field,
                    row=row,
                    replacements=replacements,
                    additional_regex_list=document_name_additional_regex_list,
                )
            if not download_name:
                self.logger.warning(
                    "Download name is empty or row -> %s does not have the data to resolve the placeholders in document download name -> '%s'. Using -> '%s' instead!",
                    str(index),
                    download_name_field,
                    document_name,
                )
                # in this case we use the document_name also as the download_name:
                download_name = document_name

            # This is an optimization. We check if the document was created
            # in a former run. This helps if the customizer is re-run:
            if document_name and document_name in existing_documents:
                self.logger.info(
                    "Document -> '%s' does already exist and has ID -> %s. Skipping...",
                    document_name,
                    existing_documents[document_name],
                )
                result["skipped_counter"] += 1
                continue

            # Determine the description field:
            description = (
                self.replace_bulk_placeholders(
                    input_string=description_field,
                    row=row,
                )
                if description_field
                else ""
            )

            # Determine the external creation date (if any):
            if external_create_date_field:
                external_create_date = self.replace_bulk_placeholders(
                    input_string=external_create_date_field,
                    row=row,
                )
            else:
                external_create_date = None

            # Determine the external modification date (if any):
            if external_modify_date_field:
                external_modify_date = self.replace_bulk_placeholders(
                    input_string=external_modify_date_field,
                    row=row,
                )
            else:
                external_modify_date = None

            # Determine the key field (if any):
            key = self.replace_bulk_placeholders(input_string=key_field, row=row) if key_field else None

            # check if workspace with this nickname does already exist.
            # we also store the nickname to assign it to the new workspace:
            if nickname_field:
                nickname = self.replace_bulk_placeholders(
                    input_string=nickname_field,
                    row=row,
                    replacements=replacements,
                    additional_regex_list=nickname_additional_regex_list,
                )
            else:
                nickname = None

            # Based on the evaluation of conditions_create, conditions_delete,
            # and conditions_update we could end up with an empty operations list.
            # In such cases we skip the further processing of this row:
            if not row_operations:
                self.logger.info(
                    "Skip document -> '%s' as row operations is empty (no create, update, delete or recreate). This may be because conditions_create, conditions_delete, and conditions_update have been evaluated to false for this row.",
                    document_name,
                )
                result["skipped_counter"] += 1
                continue

            self.logger.info(
                "Bulk process document -> '%s' using effective operations -> %s...",
                document_name,
                str(row_operations),
            )

            # We only need to download the files if we want more than just
            # the "delete" operation:
            if any(operation in ["create", "update", "recreate"] for operation in row_operations):
                file_name, mime_type = self.download_bulk_document(
                    bulk_document=bulk_document,
                    download_name=download_name,
                    row=row,
                    source_otcs=source_otcs,
                    result=result,
                )

                if file_name is None:
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue

                self.logger.debug("Download name -> '%s'", download_name)
                self.logger.debug("Document name -> '%s'", document_name)
                self.logger.debug("File name -> '%s'", file_name)
                self.logger.debug("Mime type -> '%s'", mime_type)
            else:
                # if we only do a delete operation then there's no need for
                # processing local files for upload:
                file_name = None
                mime_type = None

            # Now we traverse a list of (multiple) workspaces
            # the document should be uploaded to:
            success = True
            workspaces = bulk_document.get("workspaces", [])
            for workspace in workspaces:
                # success will only be false if a config problem (failure)
                # and not just a data problem (skipped) has occured:
                parent_id, success = self.get_bulk_document_location(
                    workspace=workspace,
                    row=row,
                    index=index,
                    replacements=replacements,
                )

                if parent_id is None:
                    continue

                #
                # Create the document in the target folder specified by parent_id:
                #

                # Check if the document does already exist.
                # First we try to look up the document by a unique
                # key that does not change over time.
                # For this we expect a "key" value to be defined for the
                # bulkDocument and one of the category / attribute item
                # to be marked with "is_key" = True. If we don't find the
                # document via key we try via parent and name.
                self.logger.info(
                    "Check if document -> '%s' is already in target folder with ID -> %s%s...",
                    document_name,
                    parent_id,
                    " (using key -> {})".format(key) if key is not None else "",
                )
                # Initialize variables - this is important!
                document_old_name = None
                document_id = None
                handle_name_clash = False
                document_modify_date = None

                # We have 4 cases to differentiate:

                # 1. key given + key found = name irrelevant, item exist
                # 2. key given + key not found = if name exist it is a name clash
                # 3. no key given + name found = item exist
                # 4. no key given + name not found = item does not exist

                # We are NOT trying to compensate for a failed key lookup with a name lookup!
                # Names are only relevant if no key is in payload!

                if key:
                    key_attribute = next(
                        (cat_attr for cat_attr in categories if cat_attr.get("is_key", False) is True),
                        None,
                    )
                    if key_attribute:
                        cat_name = key_attribute.get("name", None)
                        att_name = key_attribute.get("attribute", None)
                        set_name = key_attribute.get("set", None)
                        # Try to find the node that has the given key attribute value:
                        response = self._otcs_frontend.lookup_nodes(
                            parent_node_id=parent_id,
                            category=cat_name,
                            attribute=att_name,
                            attribute_set=set_name,
                            value=key,
                        )
                        document_id = self._otcs_frontend.get_result_value(
                            response=response,
                            key="id",
                        )
                        if document_id:
                            # Case 1: key given + key found = name irrelevant, item exist
                            self.logger.info(
                                "Found existing document with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                                key,
                                cat_name,
                                att_name,
                                parent_id,
                            )
                            document_old_name = self._otcs_frontend.get_result_value(
                                response=response,
                                key="name",
                            )
                            if (
                                document_old_name != document_name
                                and document_old_name != document_name + " (" + key + ")"
                            ):
                                self.logger.info(
                                    "Existing document with ID -> %s has the old name -> '%s' which is different from the new name -> '%s.'",
                                    document_id,
                                    document_old_name,
                                    document_name,
                                )
                            else:
                                # if there was a name clash before and got resolved
                                # then we need to stick to the resolved name also for updates:
                                if document_old_name == document_name + " (" + key + ")":
                                    self.logger.info(
                                        "Document name conflict was resolved before. Changing document name to -> '%s' to match former resolution.",
                                        document_old_name,
                                    )
                                    document_name = document_old_name
                                document_old_name = None
                            # We get the modify date of the existing document.
                            document_modify_date = self._otcs_frontend.get_result_value(
                                response=response,
                                key="modify_date",
                            )
                        else:
                            # Case 2: key given + key not found = if name exist it is a name clash
                            self.logger.info(
                                "Couldn't find existing document with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                                key,
                                cat_name,
                                att_name,
                                parent_id,
                            )
                            # Keep track that we need to handle a name clash as based on the key
                            # the document should not exist.
                            handle_name_clash = True
                    else:
                        self.logger.error(
                            "Document has a key -> '%s' defined but none of the category attributes is marked as a key attribute ('is_key' is missing)!",
                            key,
                        )
                        success = False
                        continue
                # end if key
                else:
                    # If we haven't a key we try by parent + name
                    response = self._otcs_frontend.get_node_by_parent_and_name(
                        name=document_name,
                        parent_id=parent_id,
                    )
                    document_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                    if document_id:
                        # Case 3: no key given + name found = item exist
                        self.logger.info(
                            "Found existing document -> '%s' (%s) in parent with ID -> %s.",
                            document_name,
                            document_id,
                            parent_id,
                        )
                        # We get the modify date of the existing document.
                        document_modify_date = self._otcs_frontend.get_result_value(
                            response=response,
                            key="modify_date",
                        )
                    elif not extract_zip or mime_type != "application/zip":
                        # Case 4: no key given + name not found = item does not exist
                        self.logger.info(
                            "Cannot find document -> '%s' in parent with ID -> %s.",
                            document_name,
                            parent_id,
                        )
                    else:
                        # Edge case: no key given + name not found but it is a zip file that got extracted.
                        # So we don't really know if it exists yet.
                        self.logger.info(
                            "File -> '%s' is a zip file that potentially has been extracted into parent with ID -> %s before. Existence of extracted files will be determined later.",
                            document_name,
                            parent_id,
                        )

                # Check if we want to recreate an existing document
                # then we handle the "delete" part of "recreate" first:
                if document_id and "recreate" in row_operations:
                    response = self._otcs_frontend.delete_node(
                        node_id=document_id,
                        purge=True,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to recreate existing document -> '%s' (%s) under parent ID -> %s! Delete failed.",
                            (document_name if document_old_name is None else document_old_name),
                            document_id,
                            parent_id,
                        )
                        success = False
                        continue
                    self.logger.info(
                        "Deleted existing document -> '%s' (%s) as part of the recreate operation.",
                        (document_name if document_old_name is None else document_old_name),
                        document_id,
                    )
                    result["delete_counter"] += 1
                    document_id = None

                # Check if document does not exists - then we create a new document
                # if this is requested ("create" value in operations list in payload)
                if not document_id and ("create" in row_operations or "recreate" in row_operations):
                    # The document does not exist in Content Server - so we
                    # upload it now...

                    # for Case 2 (we looked up the document by key but could have name collisions)
                    # we need to see if we have name collisions:
                    if handle_name_clash:
                        response = self._otcs_frontend.check_node_name(
                            parent_id=parent_id,
                            node_name=document_name,
                        )
                        # result is a list of names that clash - if it is empty we have no clash
                        if response.get("results"):
                            # We need to handle a race condition here: it could be that
                            # the document has been created by another thread. This is because
                            # the key could be in multiple rows of the data frame depending how the data loader works.
                            # We can also not assume that this can be resolved by making the key unique
                            # in the data source as it could be that a data set with the same key should
                            # go to multiple workspaces that then are folded into one by synonym resolution.
                            conflicting_node_id = response["results"][0].get("id")
                            conflicting_key = self._otcs_frontend.get_category_value_by_name(
                                node_id=conflicting_node_id,
                                category_name=cat_name,
                                attribute_name=att_name,
                                set_name=set_name,
                            )
                            if key == conflicting_key:
                                # We have a race condition as the two documents don't really clash but are identical.
                                # Just skip uploading the same document once more.
                                self.logger.warning(
                                    "Detected a race condition in name clash handling! Skipping document upload!",
                                )
                                continue

                            # We add the suffix with the key which should be unique:
                            self.logger.warning(
                                "Document -> '%s' does already exist in workspace folder with ID -> %s and we need to handle the name clash and use name -> '%s'",
                                document_name,
                                parent_id,
                                document_name + " (" + key + ")",
                            )
                            document_name = document_name + " (" + key + ")"

                    # If category data is in payload we substitute
                    # the values with data from the current data row:
                    if categories:
                        # Make sure the threads are not changing data structures that
                        # are shared between threads. categories is a list of dicts.
                        # list and dicts are "mutable" data structures in Python!
                        worker_categories = self.process_bulk_categories(
                            row=row,
                            index=index,
                            categories=categories,
                            replacements=replacements,
                        )
                        document_category_data = self.prepare_category_data(
                            categories_payload=worker_categories,
                            source_node_id=parent_id,
                        )
                    # end if categories
                    else:
                        document_category_data = {}

                    response = self._otcs_frontend.upload_file_to_parent(
                        file_url=file_name,
                        file_name=document_name,
                        mime_type=mime_type,
                        parent_id=int(parent_id),
                        category_data=document_category_data,
                        description=description,
                        external_create_date=external_create_date,
                        external_modify_date=external_modify_date,
                        extract_zip=extract_zip,
                        replace_existing="update" in row_operations and "version" in update_operations,
                        show_error=False,
                    )
                    document_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                    if not document_id:
                        # We may have a race condition here. Double check the document does not exist
                        # before issuing an error:
                        response = self._otcs_frontend.get_node_by_parent_and_name(
                            parent_id=int(parent_id),
                            name=document_name,
                        )
                        document_id = self._otcs_frontend.get_result_value(
                            response=response,
                            key="id",
                        )
                    if not document_id:
                        self.logger.error(
                            "Cannot create document -> '%s' (download name -> '%s') in folder/workspace with ID -> %s!",
                            document_name,
                            download_name,
                            parent_id,
                        )
                        success = False
                        continue
                    self.logger.info(
                        "Created document -> '%s' (ID -> %s, file -> '%s', mime type -> '%s'%s%s) in parent with ID -> %s.",
                        document_name,
                        document_id,
                        file_name,
                        mime_type,
                        ", description -> {}".format(description) if description else "",
                        ", size -> {}".format(os.path.getsize(file_name))
                        if os.path.exists(file_name)
                        else "",  # this can happen for files in zip files that have been cleaned up already
                        parent_id,
                    )
                    result["create_counter"] += 1

                    # Check if metadata or images embeddings need to be updated for the document:
                    aviator_metadata = bulk_document.get("aviator_metadata", False)
                    aviator_images = bulk_document.get("aviator_images", False)
                    aviator_remove_existing = bulk_document.get("aviator_remove_existing", False)
                    if aviator_metadata or aviator_images or aviator_remove_existing:
                        self.logger.info(
                            "%s Content Aviator %s%s%s embedding via FEME for document or image -> '%s' (%s)...",
                            "Trigger" if not aviator_remove_existing else "Remove",
                            "metadata" if aviator_metadata else "",
                            " and " if aviator_metadata and aviator_images else "",
                            "image" if aviator_images else "",
                            document_name,
                            document_id,
                        )

                        self._otcs_frontend.aviator_embed_metadata(
                            node_id=document_id,
                            workspace_metadata=False,
                            document_metadata=aviator_metadata,
                            images=aviator_images,
                            wait_for_completion=False,
                            remove_existing=aviator_remove_existing,
                        )

                # end if not workspace_id and "create" in row_operations

                # If updates are requested we update the existing document with
                # a new document version and with fresh metadata from the payload.
                # Additionally we check the external modify date to support
                # incremental load for content that has really changed.
                # In addition we check that "delete" is not requested as otherwise it will
                # never go in elif "delete" ... below (and it does not make sense to update a document
                # that is deleted in the next step...)

                elif (
                    document_id
                    and "update" in row_operations
                    and "delete" not in row_operations  # note the NOT !
                    and OTCS.date_is_newer(
                        date_old=document_modify_date,
                        date_new=external_modify_date,
                    )
                ):
                    # If category data is in payload we substitute
                    # the values with data from the current data row.
                    # This is only done if "categories" update is not
                    # excluded from the update_operations:
                    if categories and "categories" in update_operations:
                        # Make sure the threads are not changing data structures that
                        # are shared between threads. categories is a list of dicts.
                        # list and dicts are "mutable" data structures in Python!
                        worker_categories = self.process_bulk_categories(
                            row=row,
                            index=index,
                            categories=categories,
                            replacements=replacements,
                        )
                        # document_category_data = self.prepare_item_create_form(
                        #     parent_id=parent_id,
                        #     categories=worker_categories,
                        #     subtype=self._otcs_frontend.ITEM_TYPE_DOCUMENT,
                        # )
                        # Transform the payload structure into the format
                        # the OTCS REST API requires:
                        document_category_data = self.prepare_category_data(
                            categories_payload=worker_categories,
                            source_node_id=document_id,
                            source_is_document=True,
                        )
                        if not document_category_data:
                            self.logger.error(
                                "Failed to prepare the updated category data for document -> '%s' (%s)!",
                                document_name,
                                str(document_id),
                            )
                            success = False
                            continue  # for index, row in partition.iterrows()
                    # end if categories
                    else:
                        document_category_data = {}

                    self.logger.info(
                        "Update existing document -> '%s' (%s) with operations -> %s...",
                        document_old_name if document_old_name else document_name,
                        document_id,
                        str(update_operations),
                    )
                    # check if adding a version is requested:
                    if "version" in update_operations:
                        response = self._otcs_frontend.add_document_version(
                            node_id=document_id,
                            file_url=file_name,
                            file_name=document_name,
                            mime_type=mime_type,
                            description=description,
                        )
                        if not response:
                            self.logger.error(
                                "Failed to add new version to existing document -> '%s' (%s)!",
                                (document_old_name if document_old_name else document_name),
                                document_id,
                            )
                            success = False
                            continue
                    if "purge" in update_operations:
                        max_versions = bulk_document.get("max_versions", 1)
                        response = self._otcs_frontend.purge_document_versions(
                            node_id=document_id, versions_to_keep=max_versions
                        )
                        if not response:
                            self.logger.error(
                                "Failed to purge versions of document -> '%s' (%s) to %d version%s!",
                                (document_old_name if document_old_name else document_name),
                                document_id,
                                max_versions,
                                "s" if max_versions > 1 else "",
                            )
                            success = False
                            continue
                    response = self._otcs_frontend.update_item(
                        node_id=document_id,
                        parent_id=None,  # None = do not move item
                        item_name=(document_name if "name" in update_operations else None),
                        item_description=(description if "description" in update_operations else None),
                        category_data=(document_category_data if "categories" in update_operations else None),
                        external_create_date=external_create_date,
                        external_modify_date=external_modify_date,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to update metadata of existing document -> '%s' (%s) with metadata -> %s!",
                            (document_old_name if document_old_name else document_name),
                            document_id,
                            str(document_category_data),
                        )
                        success = False
                        continue
                    self.logger.info(
                        "Updated existing document -> '%s' (ID -> %s, file -> '%s', mime type -> '%s', description -> '%s'%s).",
                        document_name,
                        document_id,
                        file_name,
                        mime_type,
                        description,
                        ", size -> {}".format(os.path.getsize(file_name))
                        if os.path.exists(file_name)
                        else "",  # this can happen for files in zip files that have been cleaned up already
                    )
                    result["update_counter"] += 1

                    # Check if metadata or image embeddings need to be updated for the document:
                    aviator_metadata = bulk_document.get("aviator_metadata", False)
                    aviator_images = bulk_document.get("aviator_images", False)
                    aviator_remove_existing = bulk_document.get("aviator_remove_existing", False)
                    if aviator_metadata or aviator_images or aviator_remove_existing:
                        self.logger.info(
                            "%s Content Aviator %s%s%s embedding via FEME for document or image -> '%s' (%s)...",
                            "Update" if not aviator_remove_existing else "Remove",
                            "metadata" if aviator_metadata else "",
                            " and " if aviator_metadata and aviator_images else "",
                            "image" if aviator_images else "",
                            document_name,
                            document_id,
                        )

                        self._otcs_frontend.aviator_embed_metadata(
                            node_id=document_id,
                            workspace_metadata=False,
                            document_metadata=aviator_metadata,
                            images=aviator_images,
                            wait_for_completion=False,
                            remove_existing=aviator_remove_existing,
                        )
                # end if workspace_id and "update" in row_operations
                elif document_id and "delete" in row_operations:
                    # We delete with immediate purging to keep recycle bin clean
                    # and to not run into issues with nicknames used in deleted items:
                    response = self._otcs_frontend.delete_node(
                        node_id=document_id,
                        purge=True,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to bulk delete existing document -> '%s' (%s)!",
                            (document_old_name if document_old_name else document_name),
                            document_id,
                        )
                        success = False
                        continue
                    self.logger.info(
                        "Successfully deleted existing document -> '%s' (%s).",
                        document_old_name if document_old_name else document_name,
                        document_id,
                    )
                    result["delete_counter"] += 1
                    document_id = None
                # end elif document_id and "delete" in row_operations

                # this is the plain old "if it does exist we just skip it" case:
                elif document_id:
                    self.logger.info(
                        "Skipped existing document -> '%s' (%s).",
                        document_old_name if document_old_name else document_name,
                        document_id,
                    )
                # this is the case where we just want to operate on existing documents (update or delete)
                # but they do not exist:
                elif not document_id and ("update" in row_operations or "delete" in row_operations):
                    result["skipped_counter"] += 1
                    self.logger.info(
                        "Skipped update/delete of non-existing document -> '%s'.",
                        document_old_name if document_old_name else document_name,
                    )

                # The following code is executed for all operations
                # (create, recreate, update, delete):

                # Check if we want to set / update the nickname of the document.
                # Precondition is we have determined a nickname, we have the document ID
                # and the update of the nickname is either required (create, recreate)
                # or requested (update_operations include "nickname"):
                if (
                    nickname
                    and document_id
                    and (
                        "create" in row_operations
                        or "recreate" in row_operations
                        or ("update" in row_operations and "nickname" in update_operations)
                    )
                ):
                    response = self._otcs_frontend.set_node_nickname(
                        node_id=document_id,
                        nickname=nickname,
                        show_error=True,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to assign nickname -> '%s' to document -> '%s'!",
                            nickname,
                            document_name,
                        )
                if document_id is not None:
                    # Record the document name and ID to allow to read it from failure file
                    # and speedup the process.
                    result["documents"][document_name] = document_id

            # end for workspaces

            # We want the success, failure and skip counter
            # to consider only complete data frame rows. In
            # case of bulk documents we can potentially create
            # update or delete more than 1 document per row.
            # So we use the "success" variable to accumulate
            # this for all documents per row:
            if not success:
                result["success"] = False
                result["failure_counter"] += 1
            elif document_name not in result["documents"]:
                self.logger.info(
                    "Bulk document -> '%s' was not uploaded to any workspace.",
                    document_name,
                )
                result["skipped_counter"] += 1
            else:
                result["success_counter"] += 1

            # Make sure no temp documents are piling up except
            # we want it (e.g. if using cloud document storage):
            if file_name and os.path.exists(file_name) and bulk_document.get("delete_download", True):
                os.remove(file_name)
        # end for index, row in partition.iterrows()

        self.logger.info("End working...")

        results.append(result)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_items")
    def process_bulk_items(self, section_name: str = "bulkItems") -> bool:
        """Process bulkItems in payload and bulk create them in Content Server (multi-threaded).

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not self._bulk_items:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        # For efficient idem-potent operation we may want to see which workspaces
        # have already been processed before:
        if self.check_status_file(
            payload_section_name=section_name,
            payload_specific=True,
            prefix="failure_",
        ):
            self.logger.info(
                "Found failure file. Trying to reprocess just the failed ones...",
            )
            # Read payload from where we left it last time
            self._bulk_items = self.get_status_file(
                payload_section_name=section_name,
                prefix="failure_",
            )
            if not self._bulk_items:
                self.logger.error(
                    "Cannot load existing bulk items failure file. Bailing out!",
                )
                return False

        success: bool = True

        for bulk_item in self._bulk_items:
            # Check if element has been disabled in payload (enabled = false).
            # In this case we skip the element:
            enabled = bulk_item.get("enabled", True)
            if not enabled:
                self.logger.info("Payload for bulk items is disabled. Skipping...")
                continue

            # The payload element must have a "data_source" key:
            data_source_name = bulk_item.get("data_source", None)
            if not data_source_name:
                self.logger.error("Missing data source name in bulk items!")
                success = False
                continue

            copy_data_source = bulk_item.get("copy_data_source", False)
            force_reload = bulk_item.get("force_reload", True)

            # Load and prepare the data source for the bulk processing:
            if copy_data_source:
                self.logger.info(
                    "Take a copy of data source -> %s to avoid side-effects for repeative usage of the data source...",
                    data_source_name,
                )
                data = Data(
                    self.process_bulk_datasource(
                        data_source_name=data_source_name,
                        force_reload=force_reload,
                    ),
                    logger=self.logger,
                )
            else:
                self.logger.info(
                    "Use original data source -> '%s' and not do a copy.",
                    bulk_item["data_source"],
                )
                data = self.process_bulk_datasource(
                    data_source_name=data_source_name,
                    force_reload=force_reload,
                )
            if (
                data is None
                or data.get_data_frame() is None  # the 2nd check is important for the "copy_data_source" case!
            ):  # important to use "is None" here!
                self.logger.error(
                    "Failed to load data source -> '%s' for bulk items!",
                    data_source_name,
                )
                success = False
                continue

            # Check if fields with list substructures should be exploded.
            # We may want to do this outside the bulkDatasource to only
            # explode for bulkItems and not for bulkWorkspaces or
            # bulkWorkspaceRelationships:
            explosions = bulk_item.get("explosions", [])
            for explosion in explosions:
                # explode field can be a string or a list
                # exploding multiple fields at once avoids
                # combinatorial explosions - this is VERY
                # different from exploding columns one after the other!
                if "explode_field" not in explosion and "explode_fields" not in explosion:
                    self.logger.error("Missing explosion field(s)!")
                    continue
                # we want to be backwards compatible...
                explode_fields = (
                    explosion["explode_field"] if "explode_field" in explosion else explosion["explode_fields"]
                )
                flatten_fields = explosion.get("flatten_fields", [])
                split_string_to_list = explosion.get("split_string_to_list", False)
                list_splitter = explosion.get("list_splitter", None)
                self.logger.info(
                    "Starting explosion of bulk items by field(s) -> %s (type -> %s). Size of data frame before explosion -> %s.",
                    explode_fields,
                    str(type(explode_fields)),
                    str(len(data)),
                )
                data.explode_and_flatten(
                    explode_fields=explode_fields,
                    flatten_fields=flatten_fields,
                    make_unique=False,
                    split_string_to_list=split_string_to_list,
                    separator=list_splitter,
                    reset_index=True,
                )
                self.logger.info(
                    "Size of data frame after explosion -> %s.",
                    str(len(data)),
                )
            # end for explosion in explosions

            # Keep only selected rows if filters are specified in bulkDocuments.
            # We have this _after_ "explosions" to allow access to subfields as well.
            # We have this _before_ "sorting" and "deduplication" as we may keep the wrong
            # rows otherwise (unique / deduplication always keeps the first matching row).
            # We may want to do this outside the bulkDatasource to only
            # filter for bulkDocuments and not for bulkWorkspaces or
            # bulkWorkspaceRelationships:
            filters = bulk_item.get("filters", [])
            if filters:
                data.filter(conditions=filters)

            # Sort the data frame if "sort" specified in payload. We may want to do this to have a
            # higher chance that rows with workspace names that may collapse into
            # one name are put into the same partition. This can avoid race conditions
            # between different Python threads.
            # We do this before deduplication (unique) below as sorting has an influence
            # on which duplicates are kept.
            sort_fields = bulk_item.get("sort", [])
            if sort_fields:
                self.logger.info(
                    "Start sorting of bulk items data frame based on fields (columns) -> %s...",
                    str(sort_fields),
                )
                data.sort(sort_fields=sort_fields, inplace=True)
                self.logger.info(
                    "Sorting of bulk items data frame based on fields (columns) -> %s completed.",
                    str(sort_fields),
                )

            # Check if duplicate rows for given fields should be removed. It is
            # important to do this after sorting as Pandas always keep the first occurance,
            # so ordering plays an important role in deduplication!
            unique_fields = bulk_item.get("unique", [])
            if unique_fields:
                self.logger.info(
                    "Starting deduplication of data frame for bulk items with unique fields -> %s. Size of data frame before deduplication -> %s.",
                    str(unique_fields),
                    str(len(data)),
                )
                data.deduplicate(unique_fields=unique_fields, inplace=True)
                self.logger.info(
                    "Size of data frame after deduplication -> %s.",
                    str(len(data)),
                )

            # Read name field from payload:
            name_field = bulk_item.get("name", None)
            if not name_field:
                self.logger.error(
                    "Bulk item needs a name field! Skipping to next bulk item...",
                )
                success = False
                continue

            self._log_header_callback(
                text="Process bulk items -> '{}' from data source -> '{}'".format(
                    name_field,
                    data_source_name,
                ),
                char="-",
            )

            # Read optional description field from payload:
            description_field = bulk_item.get("description", None)

            # Read the optional categories payload:
            categories = bulk_item.get("categories", None)
            if not categories:
                self.logger.info(
                    "Bulk item payload has no category data! Will leave category attributes empty...",
                )

            # Should existing items be updated? False (= no) is the default.
            operations = bulk_item.get("operations", ["create"])

            self.logger.info(
                "Bulk create items (name field -> %s. Operations -> %s.)",
                name_field,
                str(operations),
            )

            # See if bulkWorkspace definition has a specific thread number
            # otherwise it is read from a global environment variable
            bulk_thread_number = int(
                bulk_item.get("thread_number", BULK_THREAD_NUMBER),
            )

            partitions = data.partitionate(bulk_thread_number)

            # Create a list to hold the threads
            threads = []
            results = []

            # Create and start a thread for each partition
            for index, partition in enumerate(partitions, start=1):
                # start a thread executing the process_bulk_items_worker() method below:
                thread = threading.Thread(
                    name=f"{section_name}_{index:02}",
                    target=self.thread_wrapper,
                    args=(
                        self.process_bulk_items_worker,
                        bulk_item,
                        partition,
                        name_field,
                        description_field,
                        categories,
                        operations,
                        results,
                    ),
                )
                self.logger.info("Starting thread -> '%s'...", str(thread.name))
                thread.start()
                threads.append(thread)
                # Avoid that all threads start at the exact same time with
                # potentially expired cookies that cases race conditions:
                time.sleep(1)
            # end for index, partition in enumerate(partitions, start=1)

            # Wait for all threads to complete
            for thread in threads:
                self.logger.info(
                    "Waiting for thread -> '%s' to complete...",
                    str(thread.name),
                )
                thread.join()
                self.logger.info("Thread -> '%s' has completed.", str(thread.name))

            if "items" not in bulk_item:
                bulk_item["items"] = {}

            # Print statistics for each thread. In addition,
            # check if all threads have completed without error / failure.
            # If there's a single failure in on of the thread results we
            # set 'success' variable to False.
            results.sort(key=lambda x: x["thread_name"])
            for result in results:
                self.logger.info(
                    "Thread -> '%s' completed with overall status '%s'.",
                    str(result["thread_name"]),
                    "successful" if result["success"] else "failed",
                )
                self.logger.info(
                    "Thread -> '%s' processed %s data rows with %s successful, %s failed, and %s skipped.",
                    str(result["thread_name"]),
                    str(
                        result["success_counter"] + result["failure_counter"] + result["skipped_counter"],
                    ),
                    str(result["success_counter"]),
                    str(result["failure_counter"]),
                    str(result["skipped_counter"]),
                )
                self.logger.info(
                    "Thread -> '%s' created %s items, updated %s items, and deleted %s items.",
                    str(result["thread_name"]),
                    str(result["create_counter"]),
                    str(result["update_counter"]),
                    str(result["delete_counter"]),
                )

                if not result["success"]:
                    success = False
                # Record all generated items. This should allow us
                # to restart in case of failures and avoid trying to
                # create items that have been successfully created before.
                bulk_item["items"].update(result["items"])
            # end for result in results
            self._log_header_callback(
                text="Completed processing of bulk items -> '{}' using data source -> '{}'".format(
                    name_field,
                    data_source_name,
                ),
                char="-",
            )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._bulk_items,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_items_worker")
    def process_bulk_items_worker(
        self,
        bulk_item: dict,
        partition: pd.DataFrame,
        name_field: str,
        description_field: str,
        categories: list | None = None,
        operations: list | None = None,
        results: list | None = None,
    ) -> None:
        """Thread worker to create items in bulk.

        Each worker thread gets a partition of the rows that include
        the data required for the item creation.

        Args:
            bulk_item (dict):
                The bulkItem payload element.
            partition (pd.DataFrame):
                Data partition with rows to process by this thread.
            name_field (str):
                The payload field where the item name is stored.
            description_field (str):
                The payload field where the item description is stored.
            categories (list, optional):
                A list of category dictionaries.
            operations (list, optional):
                A list of operations that should be applyed on workspaces.
                Possible values: "create", "update", "delete", "recreate".
            results (list, optional):
                A mutable list of thread results.

        """

        thread_id = threading.get_ident()
        thread_name = threading.current_thread().name

        self.logger.info(
            "Start working on data frame partition of size -> %s to bulk create items...",
            str(len(partition)),
        )

        # Avoid linter warnings - so make parameter default None while we
        # actually want ["create"] to be the default:
        if operations is None:
            operations = ["create"]

        result = {}
        result["thread_id"] = thread_id
        result["thread_name"] = thread_name
        result["success_counter"] = 0
        result["failure_counter"] = 0
        result["skipped_counter"] = 0
        result["create_counter"] = 0
        result["update_counter"] = 0
        result["delete_counter"] = 0
        result["items"] = {}
        result["success"] = True

        # Check if items have been processed before, e.i. testing
        # if a "items" key exists and if it is pointing to a non-empty list:
        # Additionally we check that workspace updates are not enforced:
        if (
            bulk_item.get("items")
            and "update" not in operations
            and "delete" not in operations
            and "recreate" not in operations
        ):
            existing_items = bulk_item["items"]
            self.logger.info(
                "Found %s already processed items. Try to complete the job...",
                str(len(existing_items)),
            )
        else:
            existing_items = {}

        # See if external creation and modification dates are in the payload data:
        external_modify_date_field = bulk_item.get("external_modify_date")
        external_create_date_field = bulk_item.get("external_create_date")

        # See if we have a key field to uniquely identify an existing item:
        key_field = bulk_item.get("key")

        # Get dictionary of replacements for bulk item creations
        # this we will be used of all places data is read from the
        # data frame. Each dictionary item has the field name as the
        # dictionary key and a list of regular expressions as dictionary value
        replacements = bulk_item.get("replacements")

        # In case the name cannot be resolved we allow to
        # specify an alternative name field in the payload.
        name_field_alt = bulk_item.get("name_alt")

        # Determine the type of the item (OTCS subtype ID):
        type_field = bulk_item.get("type", self._otcs_frontend.ITEM_TYPE_URL)

        # In case the type is URL, determine the URL field:
        url_field = bulk_item.get("url")

        # In case the type is Shortcut, determine the origin field:
        original_nickname_field = bulk_item.get("original_nickname")
        original_path = bulk_item.get("original_path")

        # Document names are limited in terms of allowed characters.
        # E.g. if you don't want any path elements and "/" dividers
        # in the item name you could use name_regex = r".*/" in the payload.
        item_name_additional_regex_list = [bulk_item.get("name_regex", r"")]

        # Fetch the nickname field from the payload (if it is specified):
        nickname_field = bulk_item.get("nickname")

        # Nicknames are very limited in terms of allowed characters.
        # For nicknames we need additional regexp as we need to
        # replace all non-alphanumeric, non-space characters with ""
        # We also preserve hyphens in the first step to replace
        # them below with underscores. This is important to avoid
        # that different spellings of names produce different nicknames.
        # We want spellings with spaces match spellings with hyphens.
        # For this the workspace names have a regexp "-| " in the payload.
        nickname_additional_regex_list = [r"[^\w\s-]"]

        total_rows = len(partition)

        # Process all rows in the partition that was given to this thread:
        for index, row in partition.iterrows():
            # Calculate percentage of completion:
            percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

            self.logger.info(
                "Processing data row -> %s for bulk item creation (%.2f %% complete)...",
                str(index),
                percent_complete,
            )

            # Clear variables to ensure clean state for each row:
            parent_id = None
            item_id = None

            # Create a copy of the mutable operations list as we may
            # want to modify it per row:
            row_operations = list(operations)

            # Check if all data conditions to create the item are met
            conditions = bulk_item.get("conditions")
            if conditions:
                evaluated_condition = self.evaluate_conditions(
                    conditions=conditions,
                    row=row,
                )
                if not evaluated_condition:
                    self.logger.info(
                        "Condition for bulk item row -> %s not met. Skipping row for item creation...",
                        str(index),
                    )
                    result["skipped_counter"] += 1
                    continue

            # Check if all data conditions to create the item are met:
            if "create" in row_operations or "recreate" in row_operations:
                conditions_create = bulk_item.get("conditions_create")
                if conditions_create:
                    evaluated_conditions_create = self.evaluate_conditions(
                        conditions=conditions_create,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_create:
                        self.logger.info(
                            "Create condition for bulk item row -> %s not met. Excluding create operation for current row...",
                            str(index),
                        )
                        if "create" in row_operations:
                            row_operations.remove("create")
                        if "recreate" in row_operations:
                            row_operations.remove("recreate")
                elif (
                    "recreate" in row_operations
                ):  # we still create and recreate without conditions_create. But give a warning for 'recreate' without condition.
                    self.logger.warning(
                        "No create condition provided but 'recreate' operation requested. This will recreate all existing items!",
                    )

            # Check if all data conditions to delete the item are met:
            if "delete" in row_operations:
                conditions_delete = bulk_item.get("conditions_delete")
                if conditions_delete:
                    evaluated_conditions_delete = self.evaluate_conditions(
                        conditions=conditions_delete,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_delete:
                        self.logger.info(
                            "Delete condition for bulk item row -> %s not met. Excluding delete operation for current row...",
                            str(index),
                        )
                        row_operations.remove("delete")
                else:  # without delete_conditions we don't delete!!
                    self.logger.warning(
                        "Delete operation requested for bulk items but conditions for deletion are missing! (specify 'conditions_delete'!)",
                    )
                    row_operations.remove("delete")

            # Check if all data conditions to update the item are met:
            if "update" in row_operations:
                conditions_update = bulk_item.get("conditions_update")
                if conditions_update:
                    evaluated_conditions_update = self.evaluate_conditions(
                        conditions=conditions_update,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_update:
                        self.logger.info(
                            "Update condition for bulk item row -> %s not met. Excluding update operation for current row...",
                            str(index),
                        )
                        row_operations.remove("update")

            # Determine the item name:
            item_name = self.replace_bulk_placeholders(
                input_string=name_field,
                row=row,
                replacements=replacements,
                additional_regex_list=item_name_additional_regex_list,
            )
            # If we cannot get the item_name from the
            # name_field we try the alternative name field
            # (if specified in payload via "name_alt"):
            if not item_name and name_field_alt:
                self.logger.debug(
                    "Row -> %s does not have the data to resolve the placeholders in item name -> %s! Trying alternative name field -> %s...",
                    str(index),
                    name_field,
                    name_field_alt,
                )
                # The additional regex list will make sure
                # that any path or URL element is stripped from
                # the value in the row element:
                item_name = self.replace_bulk_placeholders(
                    input_string=name_field_alt,
                    row=row,
                    replacements=replacements,
                    additional_regex_list=item_name_additional_regex_list,
                )
            if not item_name:
                self.logger.error(
                    "Row -> %s does not have the data to resolve the placeholders in item name -> %s%s!",
                    str(index),
                    name_field,
                    (" nor in alternative name field -> " + name_field_alt if name_field_alt else ""),
                )
                result["skipped_counter"] += 1
                continue

            # Cleanse the item name (allowed characters, maximum length):
            item_name = OTCS.cleanse_item_name(item_name)

            # This is an optimization. We check if the item was created
            # in a former run. This helps if the customizer is re-run:
            if item_name and item_name in existing_items:
                self.logger.info(
                    "Item -> '%s' does already exist and has ID -> %s. Skipping...",
                    item_name,
                    existing_items[item_name],
                )
                result["skipped_counter"] += 1
                continue

            # Determine the description field:
            description = (
                self.replace_bulk_placeholders(
                    input_string=description_field,
                    row=row,
                )
                if description_field
                else ""
            )

            # Determine the item type:
            item_type = self.replace_bulk_placeholders(
                input_string=type_field,
                row=row,
            )

            # Determine the item type:
            item_url = self.replace_bulk_placeholders(
                input_string=url_field,
                row=row,
            )

            # Determine the item origin:
            original_nickname = self.replace_bulk_placeholders(
                input_string=original_nickname_field,
                row=row,
            )

            # Determine the item origin:
            self.replace_bulk_placeholders_list(
                input_list=original_path,
                row=row,
            )

            item_original_node = None
            # First try to get the original item for the shortcut via the nickname:
            if original_nickname:
                item_original_node = self._otcs_frontend.get_node_from_nickname(nickname=original_nickname)
            # FIf that didn't resolve the original node we try a path if provided in the payload:
            if not item_original_node and original_path:
                item_original_node = self._otcs_frontend.get_node_by_volume_and_path(
                    volume_type=self._otcs_frontend.VOLUME_TYPE_ENTERPRISE_WORKSPACE,
                    path=original_path,
                )
            item_original_node_id = self._otcs_frontend.get_result_value(response=item_original_node, key="id")

            # Determine the external creation date (if any):
            if external_create_date_field:
                external_create_date = self.replace_bulk_placeholders(
                    input_string=external_create_date_field,
                    row=row,
                )
            else:
                external_create_date = None

            # Determine the external modification date (if any):
            if external_modify_date_field:
                external_modify_date = self.replace_bulk_placeholders(
                    input_string=external_modify_date_field,
                    row=row,
                )
            else:
                external_modify_date = None

            # Determine the key field (if any):
            key = self.replace_bulk_placeholders(input_string=key_field, row=row) if key_field else None

            # check if workspace with this nickname does already exist.
            # we also store the nickname to assign it to the new workspace:
            if nickname_field:
                nickname = self.replace_bulk_placeholders(
                    input_string=nickname_field,
                    row=row,
                    replacements=replacements,
                    additional_regex_list=nickname_additional_regex_list,
                )
            else:
                nickname = None

            # Based on the evaluation of conditions_create, conditions_delete,
            # and conditions_update we could end up with an empty operations list.
            # In such cases we skip the further processing of this row:
            if not row_operations:
                self.logger.info(
                    "Skip item -> '%s' as row operations is empty (no create, update, delete or recreate). This may be because conditions_create, conditions_delete, and conditions_update have been evaluated to false for this row.",
                    item_name,
                )
                result["skipped_counter"] += 1
                continue

            self.logger.info(
                "Bulk process item -> '%s' using effective operations -> %s...",
                item_name,
                str(row_operations),
            )

            # Now we traverse a list of (multiple) workspaces
            # the item should be added to:
            success = True
            workspaces = bulk_item.get("workspaces", [])
            for workspace in workspaces:
                # success will only be false if a config problem (failure)
                # and not just a data problem (skipped) has occured:
                parent_id, success = self.get_bulk_document_location(
                    workspace=workspace,
                    row=row,
                    index=index,
                    replacements=replacements,
                )

                if parent_id is None:
                    continue

                #
                # Create the item in the target folder specified by parent_id:
                #

                # Check if the item does already exist.
                # First we try to look up the item by a unique
                # key that does not change over time.
                # For this we expect a "key" value to be defined for the
                # bulkDocument and one of the category / attribute item
                # to be marked with "is_key" = True. If we don't find the
                # item via key we try via parent and name.
                self.logger.info(
                    "Check if item -> '%s' is already in target folder with ID -> %s%s...",
                    item_name,
                    parent_id,
                    " (using key -> {})".format(key) if key is not None else "",
                )
                # Initialize variables - this is important!
                item_old_name = None
                item_id = None
                handle_name_clash = False
                item_modify_date = None

                # We have 4 cases to differentiate:

                # 1. key given + key found = name irrelevant, item exist
                # 2. key given + key not found = if name exist it is a name clash
                # 3. no key given + name found = item exist
                # 4. no key given + name not found = item does not exist

                # We are NOT trying to compensate for a failed key lookup with a name lookup!
                # Names are only relevant if no key is in payload!

                if key:
                    key_attribute = next(
                        (cat_attr for cat_attr in categories if cat_attr.get("is_key", False) is True),
                        None,
                    )
                    if key_attribute:
                        cat_name = key_attribute.get("name", None)
                        att_name = key_attribute.get("attribute", None)
                        set_name = key_attribute.get("set", None)
                        # Try to find the node that has the given key attribute value:
                        response = self._otcs_frontend.lookup_nodes(
                            parent_node_id=parent_id,
                            category=cat_name,
                            attribute=att_name,
                            attribute_set=set_name,
                            value=key,
                        )
                        item_id = self._otcs_frontend.get_result_value(
                            response=response,
                            key="id",
                        )
                        if item_id:
                            # Case 1: key given + key found = name irrelevant, item exist
                            self.logger.info(
                                "Found existing item with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                                key,
                                cat_name,
                                att_name,
                                parent_id,
                            )
                            item_old_name = self._otcs_frontend.get_result_value(
                                response=response,
                                key="name",
                            )
                            if item_old_name != item_name and item_old_name != item_name + " (" + key + ")":
                                self.logger.info(
                                    "Existing item with ID -> %s has the old name -> '%s' which is different from the new name -> '%s.'",
                                    item_id,
                                    item_old_name,
                                    item_name,
                                )
                            else:
                                # if there was a name clash before and got resolved
                                # then we need to stick to the resolved name also for updates:
                                if item_old_name == item_name + " (" + key + ")":
                                    self.logger.info(
                                        "Document name conflict was resolved before. Changing item name to -> '%s' to match former resolution.",
                                        item_old_name,
                                    )
                                    item_name = item_old_name
                                item_old_name = None
                            # We get the modify date of the existing item.
                            item_modify_date = self._otcs_frontend.get_result_value(
                                response=response,
                                key="modify_date",
                            )
                        else:
                            # Case 2: key given + key not found = if name exist it is a name clash
                            self.logger.info(
                                "Couldn't find existing item with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                                key,
                                cat_name,
                                att_name,
                                parent_id,
                            )
                            handle_name_clash = True
                    else:
                        self.logger.error(
                            "Item has a key -> '%s' defined but none of the category attributes is marked as a key attribute ('is_key' is missing)!",
                            key,
                        )
                        success = False
                        continue
                    # end if key_attribute:
                # end if key:
                else:
                    # If we haven't a key we try by parent + name
                    response = self._otcs_frontend.get_node_by_parent_and_name(
                        name=item_name,
                        parent_id=parent_id,
                    )
                    item_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                    if item_id:
                        # Case 3: no key given + name found = item exist
                        self.logger.info(
                            "Found existing item -> '%s' (%s) in parent with ID -> %s.",
                            item_name,
                            item_id,
                            parent_id,
                        )
                        # We get the modify date of the existing item.
                        item_modify_date = self._otcs_frontend.get_result_value(
                            response=response,
                            key="modify_date",
                        )
                    else:
                        # Case 4: no key given + name not found = item does not exist
                        self.logger.info(
                            "No existing item -> '%s' in parent with ID -> %s.",
                            item_name,
                            parent_id,
                        )

                # Check if we want to recreate an existing item
                # then we handle the "delete" part of "recreate" first:
                if item_id and "recreate" in row_operations:
                    response = self._otcs_frontend.delete_node(
                        node_id=item_id,
                        purge=True,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to recreate existing item -> '%s' (%s) under parent ID -> %s! Delete failed.",
                            (item_name if item_old_name is None else item_old_name),
                            item_id,
                            parent_id,
                        )
                        success = False
                        continue
                    self.logger.info(
                        "Successfully deleted existing item -> '%s' (%s) as part of the recreate operation.",
                        (item_name if item_old_name is None else item_old_name),
                        item_id,
                    )
                    result["delete_counter"] += 1
                    item_id = None

                # Check if item does not exists - then we create a new item
                # if this is requested ("create" value in operations list in payload)
                if not item_id and ("create" in row_operations or "recreate" in row_operations):
                    # The item does not exist in Content Server - so we
                    # add it now...

                    # for Case 2 (we looked up the item by key but could have name collisions)
                    # we need to see if we have name collisions:
                    if handle_name_clash:
                        response = self._otcs_frontend.check_node_name(
                            parent_id=parent_id,
                            node_name=item_name,
                        )
                        # result is a list of names that clash - if it is empty we have no clash
                        if response.get("results"):
                            # We need to handle a race condition here: it could be that
                            # the right item has been created by another thread. This is because
                            # the key could be in multiple rows of the data frame depending how the data loader works.
                            # We can also not assume that this can be resolved by making the key unique
                            # in the data source as it could be that a data set with the same key should
                            # go to multiple workspaces that then are folded into one by synonym resolution.
                            conflicting_node_id = response["results"][0].get("id")
                            conflicting_key = self._otcs_frontend.get_category_value_by_name(
                                node_id=conflicting_node_id,
                                category_name=cat_name,
                                attribute_name=att_name,
                                set_name=set_name,
                            )
                            if key == conflicting_key:
                                # We have a race condition as the two items don't really clash but are identical.
                                # Just skip creating the same item once more.
                                self.logger.warning(
                                    "Detected a race condition in name clash handling! Skipping item creation!",
                                )
                                continue

                            # We add the suffix with the key which should be unique:
                            self.logger.warning(
                                "Item -> '%s' does already exist in workspace folder with ID -> %s and we need to handle the name clash and use name -> '%s'",
                                item_name,
                                parent_id,
                                item_name + " (" + key + ")",
                            )
                            item_name = item_name + " (" + key + ")"

                    # If category data is in payload we substitute
                    # the values with data from the current data row:
                    if categories:
                        # Make sure the threads are not changing data structures that
                        # are shared between threads. categories is a list of dicts.
                        # list and dicts are "mutable" data structures in Python!
                        worker_categories = self.process_bulk_categories(
                            row=row,
                            index=index,
                            categories=categories,
                            replacements=replacements,
                        )
                        item_category_data = self.prepare_category_data(
                            categories_payload=worker_categories,
                            source_node_id=parent_id,
                        )
                    # end if categories
                    else:
                        item_category_data = {}

                    response = self._otcs_frontend.create_item(
                        parent_id=int(parent_id),
                        item_type=int(item_type),
                        item_name=item_name,
                        item_description=description,
                        url=item_url,
                        original_id=int(item_original_node_id),
                        category_data=item_category_data,
                        external_create_date=external_create_date,
                        external_modify_date=external_modify_date,
                        show_error=False,
                    )
                    item_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                    if not item_id:
                        # We may have a race condition here. Double check the item does not yet exist:
                        response = self._otcs_frontend.get_node_by_parent_and_name(
                            parent_id=int(parent_id),
                            name=item_name,
                        )
                        item_id = self._otcs_frontend.get_result_value(
                            response=response,
                            key="id",
                        )
                    if not item_id:
                        self.logger.error(
                            "Cannot create item -> '%s' in folder/workspace with ID -> %s!",
                            item_name,
                            parent_id,
                        )
                        success = False
                        continue
                    self.logger.info(
                        "Created item -> '%s' (%s), description -> '%s' in parent with ID -> %s.",
                        item_name,
                        item_id,
                        description,
                        parent_id,
                    )
                    result["create_counter"] += 1

                # end if not workspace_id and "create" in row_operations

                # If updates are requested we update the existing item with
                # a new item version and with fresh metadata from the payload.
                # Additionally we check the external modify date to support
                # incremental load for content that has really changed.
                # In addition we check that "delete" is not requested as otherwise it will
                # never go in elif "delete" ... below (and it does not make sense to update a item
                # that is deleted in the next step...)
                elif (
                    item_id
                    and "update" in row_operations
                    and "delete" not in row_operations  # note the NOT !
                    and OTCS.date_is_newer(
                        date_old=item_modify_date,
                        date_new=external_modify_date,
                    )
                ):
                    # get the specific update operations given in the payload
                    # if not specified we do all 4 update operations (name, description, categories and version)
                    update_operations = bulk_item.get(
                        "update_operations",
                        ["name", "description", "categories", "nickname", "url"],
                    )

                    # If category data is in payload we substitute
                    # the values with data from the current data row.
                    # This is only done if "categories" update is not
                    # excluded from the update_operations:
                    if categories and "categories" in update_operations:
                        # Make sure the threads are not changing data structures that
                        # are shared between threads. categories is a list of dicts.
                        # list and dicts are "mutable" data structures in Python!
                        worker_categories = self.process_bulk_categories(
                            row=row,
                            index=index,
                            categories=categories,
                            replacements=replacements,
                        )
                        # Transform the payload structure into the format
                        # the OTCS REST API requires:
                        item_category_data = self.prepare_category_data(
                            categories_payload=worker_categories,
                            source_node_id=item_id,
                            source_is_document=True,
                        )
                        if not item_category_data:
                            self.logger.error(
                                "Failed to prepare the updated category data for item -> '%s' (%s)!",
                                item_name,
                                str(item_id),
                            )
                            success = False
                            continue  # for index, row in partition.iterrows()
                    # end if categories
                    else:
                        item_category_data = {}

                    self.logger.info(
                        "Update existing item -> '%s' (%s) with operations -> %s...",
                        item_old_name if item_old_name else item_name,
                        item_id,
                        str(update_operations),
                    )
                    response = self._otcs_frontend.update_item(
                        node_id=item_id,
                        parent_id=None,  # None = do not move item
                        item_name=(item_name if "name" in update_operations else None),
                        item_description=(description if "description" in update_operations else None),
                        category_data=(item_category_data if "categories" in update_operations else None),
                        url=(item_url if "url" in update_operations else None),
                        external_create_date=external_create_date,
                        external_modify_date=external_modify_date,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to update metadata of existing item -> '%s' (%s) with metadata -> %s!",
                            (item_old_name if item_old_name else item_name),
                            item_id,
                            str(item_category_data),
                        )
                        success = False
                        continue
                    self.logger.info(
                        "Updated existing item -> '%s' (%s, description -> '%s')",
                        item_name,
                        item_id,
                        description,
                    )
                    result["update_counter"] += 1
                # end if item_id and "update" in row_operations
                elif item_id and "delete" in row_operations:
                    # We delete with immediate purging to keep recycle bin clean
                    # and to not run into issues with nicknames used in deleted items:
                    response = self._otcs_frontend.delete_node(
                        node_id=item_id,
                        purge=True,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to bulk delete existing item -> '%s' (%s)!",
                            (item_old_name if item_old_name else item_name),
                            item_id,
                        )
                        success = False
                        continue
                    self.logger.info(
                        "Successfully deleted existing item -> '%s' (%s).",
                        item_old_name if item_old_name else item_name,
                        item_id,
                    )
                    result["delete_counter"] += 1
                    item_id = None
                # end elif item_id and "delete" in row_operations

                # this is the plain old "if it does exist we just skip it" case:
                elif item_id:
                    self.logger.info(
                        "Skipped existing item -> '%s' (%s).",
                        item_old_name if item_old_name else item_name,
                        item_id,
                    )
                # this is the case where we just want to operate on existing items (update or delete)
                # but they do not exist:
                elif not item_id and ("update" in row_operations or "delete" in row_operations):
                    result["skipped_counter"] += 1
                    self.logger.info(
                        "Skipped update/delete of non-existing item -> '%s'.",
                        item_old_name if item_old_name else item_name,
                    )

                # The following code is executed for all operations
                # (create, recreate, update, delete):

                # Check if we want to set / update the nickname of the item.
                # Precondition is we have determined a nickname, we have the item ID
                # and the update of the nickname is either required (create, recreate)
                # or requested (update_operations include "nickname"):
                if (
                    nickname
                    and item_id
                    and (
                        "create" in row_operations
                        or "recreate" in row_operations
                        or ("update" in row_operations and "nickname" in update_operations)
                    )
                ):
                    response = self._otcs_frontend.set_node_nickname(
                        node_id=item_id,
                        nickname=nickname,
                        show_error=True,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to assign nickname -> '%s' to item -> '%s'!",
                            nickname,
                            item_name,
                        )
                if item_id is not None:
                    # Record the item name and ID to allow to read it from failure file
                    # and speedup the process.
                    result["items"][item_name] = item_id

            # end for workspaces

            # We want the success, failure and skip counter
            # to consider only complete data frame rows. In
            # case of bulk items we can potentially create
            # update or delete more than 1 item per row.
            # So we use the "success" variable to accumulate
            # this for all items per row:
            if not success:
                result["success"] = False
                result["failure_counter"] += 1
            elif item_name not in result["items"]:
                self.logger.info(
                    "Bulk item -> '%s' was not added to any workspace.",
                    item_name,
                )
                result["skipped_counter"] += 1
            else:
                result["success_counter"] += 1

        # end for index, row in partition.iterrows()

        self.logger.info("End working...")

        results.append(result)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_avts_repositories")
    def process_avts_repositories(self, section_name: str = "avtsRepositories") -> bool:
        """Process Aviator Search repositories.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._avts_repositories:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        token = self._avts.authenticate()
        if not token:
            self.logger.error("Cannot authenticate at Aviator Search!")
            success = False
        else:
            for payload_repo in self._avts_repositories:
                if not payload_repo.get("enabled", True):
                    continue

                repository = self._avts.get_repo_by_name(name=payload_repo["name"])

                if repository is None:
                    self.logger.info(
                        "Repository -> '%s' does not exist, creating it...",
                        payload_repo["name"],
                    )

                    if payload_repo.get("type", "Extended ECM") == "Extended ECM":
                        repository = self._avts.create_extended_ecm_repo(
                            name=payload_repo["name"],
                            username=payload_repo["username"],
                            password=payload_repo["password"],
                            otcs_url=payload_repo["otcs_url"],
                            otcs_api_url=payload_repo["otcs_api_url"],
                            node_id=int(payload_repo["node_id"]),
                        )

                    elif payload_repo["type"] == "Documentum":
                        self.logger.warning("Not yet implemented")
                    elif payload_repo["type"] == "MSTeams":
                        repository = self._avts.create_msteams_repo(
                            name=payload_repo["name"],
                            client_id=payload_repo["client_id"],
                            tenant_id=payload_repo["tenant_id"],
                            certificate_file=payload_repo["certificate_file"],
                            certificate_password=payload_repo["certificate_password"],
                            index_attachments=payload_repo.get("index_attachments", True),
                            index_call_recordings=payload_repo.get(
                                "index_call_recordings",
                                True,
                            ),
                            index_message_replies=payload_repo.get(
                                "index_message_replies",
                                True,
                            ),
                            index_user_chats=payload_repo.get("index_user_chats", True),
                        )
                    elif payload_repo["type"] == "SharePoint":
                        repository = self._avts.create_sharepoint_repo(
                            name=payload_repo["name"],
                            client_id=payload_repo["client_id"],
                            tenant_id=payload_repo["tenant_id"],
                            certificate_file=payload_repo["certificate_file"],
                            certificate_password=payload_repo["certificate_password"],
                            sharepoint_url=payload_repo["sharepoint_url"],
                            sharepoint_url_type=payload_repo["sharepoint_url_type"],
                            sharepoint_mysite_url=payload_repo["sharepoint_mysite_url"],
                            sharepoint_admin_url=payload_repo["sharepoint_admin_url"],
                            index_user_profiles=payload_repo.get(
                                "index_message_replies",
                                False,
                            ),
                        )
                    else:
                        self.logger.error(
                            "Invalid repository type -> '%s' specified! Valid values are: Extended ECM, Documentum, MSTeams, SharePoint.",
                            payload_repo["type"],
                        )
                        success = False
                        break

                    if repository is None:
                        self.logger.error(
                            "Creation of Aviator Search repository -> '%s' failed!",
                            payload_repo["name"],
                        )
                        success = False
                    else:
                        self.logger.info(
                            "Successfully created Aviator Search repository -> '%s'.",
                            payload_repo["name"],
                        )
                        self.logger.debug("%s", repository)

                else:
                    self.logger.info(
                        "Aviator Search repository -> '%s' already exists.",
                        payload_repo["name"],
                    )

                # Start Crawling
                start_crawling = payload_repo.get("start", False)

                if repository is not None and start_crawling:
                    response = self._avts.start_crawling(repo_name=payload_repo["name"])

                    if response is None:
                        self.logger.error(
                            "Aviator Search start crawling on repository failed -> '%s'!",
                            payload_repo["name"],
                        )
                        success = False
                    else:
                        self.logger.info(
                            "Aviator Search crawling started on repository -> '%s'",
                            payload_repo["name"],
                        )
                        self.logger.debug("%s", response)
            # end for payload_repo in self._avts_repositories:
        # end else:

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._avts_repositories,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_avts_questions")
    def process_avts_questions(self, section_name: str = "avtsQuestions") -> bool:
        """Process Aviator Search repositories.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._avts_questions:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        if not self._avts_questions.get("enabled", True):
            self.logger.info(
                "Payload section -> '%s' is not enabled. Skipping...",
                section_name,
            )
            return True
        questions = self._avts_questions.get("questions", [])
        self.logger.info("Sample questions -> %s.", str(questions))

        token = self._avts.authenticate()
        if not token:
            self.logger.error("Cannot authenticate at Aviator Search!")
            success = False
        else:
            response = self._avts.set_questions(questions=questions)

            if response is None:
                self.logger.error("Aviator Search setting questions failed!")
                success = False
            else:
                self.logger.info("Successfully configured Aviator Search questions.")
                self.logger.debug("%s", response)

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._avts_questions,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_embeddings")
    def process_embeddings(self, section_name: str = "embeddings") -> bool:
        """Process additional Aviator embeddings.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise.

        """

        if not self._embeddings:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for embedding in self._embeddings:
            # Check if item has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not embedding.get("enabled", True):
                self.logger.info(
                    "Payload for embedding -> '%s' is disabled for FEME. Skipping...",
                    str(embedding),
                )
                continue

            # Backwards-compatibility for old syntax "documents":
            if "document_metadata" not in embedding:
                document_metadata = bool(embedding.get("documents", False))
            else:
                document_metadata = bool(embedding.get("document_metadata", False))

            # Backwards-compatibility for old syntax "workspaces":
            if "workspace_metadata" not in embedding:
                workspace_metadata = bool(embedding.get("workspaces", False))
            else:
                workspace_metadata = bool(embedding.get("workspace_metadata", False))

            wait_for_completion = bool(embedding.get("wait_for_completion", True))
            crawl = bool(embedding.get("crawl", False))
            images = bool(embedding.get("images", False))

            # We support 3 ways to determine the node ID(s):
            # 1. Node ID is specified directly in the payload using 'id = "..."'
            # 2. Node is is specified via a nickname of the node using 'nickname = "..."'
            # 3. Nodes are specified via the name of a workspace type. In this
            #    case all nodes of workspace instances are considered.

            node_id = None

            if embedding.get("id", None) is None and "nickname" in embedding:
                response = self._otcs.get_node_from_nickname(
                    nickname=embedding.get("nickname"),
                )
                node_id = self._otcs.get_result_value(response=response, key="id")

            else:
                node_id = embedding.get("id", None)
                response = None

            if node_id:
                result = self._otcs.aviator_embed_metadata(
                    node_id=node_id,
                    node=response,
                    wait_for_completion=wait_for_completion,
                    crawl=crawl,
                    document_metadata=document_metadata,
                    workspace_metadata=workspace_metadata,
                    images=images,
                )
                if not result:
                    success = False
            elif "workspace_types" in embedding:
                workspace_types = embedding["workspace_types"]
                # Handle the case of a single workspace type name:
                if isinstance(workspace_types, str):
                    workspace_types = [workspace_types]
                for workspace_type_name in workspace_types:
                    self.logger.info(
                        "Embedding metadata of workspace instances with type -> '%s'...", workspace_type_name
                    )
                    workspace_instances = self._otcs.get_workspace_instances_iterator(type_name=workspace_type_name)
                    for workspace in workspace_instances:
                        properties = workspace.get("data").get("properties")
                        self.logger.info(
                            "Embedding metadata of workspace instance -> '%s' (%s)...",
                            properties["name"],
                            properties["id"],
                        )
                        result = self._otcs.aviator_embed_metadata(
                            node_id=None,
                            node=workspace,
                            wait_for_completion=wait_for_completion,
                            crawl=crawl,
                            document_metadata=document_metadata,
                            workspace_metadata=workspace_metadata,
                            images=images,
                        )
                        if not result:
                            success = False

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._embeddings,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="replace_bulk_placeholders_list")
    def replace_bulk_placeholders_list(
        self,
        input_list: list,
        row: pd.Series,
        index: int = 0,
        replacements: dict | None = None,
        additional_regex_list: list | None = None,
    ) -> bool:
        """Process list of payload strings and replace placeholders (see next method).

        Args:
            input_list (list):
                A list of strings that contain placeholders.
            row (pd.Series):
                The curent data frame row.
            index (int, optional):
                Index for use if we encounter a list value.
                Pass None here if you want alle elements of
                the list to be parsed and placeholders replaced.
            replacements (dict, optional):
                The replacements to apply to given fields (dictionary key = field name)
            additional_regex_list (list, optional):
                These are not coming from the payload but dynamically
                added for special needs like determining the nicknames.

        Returns:
            bool:
                True = all replacements worked, False = some replacements had lookup errors.

        """

        success = True

        if not input_list:
            return False

        for i, value in enumerate(input_list):
            input_list[i] = self.replace_bulk_placeholders(
                input_string=value,
                row=row,
                index=index,
                replacements=replacements,
                additional_regex_list=additional_regex_list,
            )
            if not input_list[i]:
                success = False

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="replace_bulk_placeholders")
    def replace_bulk_placeholders(
        self,
        input_string: str,
        row: pd.Series,
        index: int | None = 0,  # don't use None as the default here!
        replacements: dict | None = None,
        additional_regex_list: list | None = None,
    ) -> str:
        """Replace placeholders like "{variable.subvalue}" in payload of bulk processing.

        Args:
            input_string (str):
                The string to replace placeholders in.
            row (pd.Series):
                Curent row in the data frame.
            index (int | None, optional):
                The index for use if we encounter a list value.
                If index is "None" then we return the complete list as value.
                Otherwise we return the list item with the given index (0 = first element is the default).
                IMPORTANT: None and 0 have different effects!
            replacements (dict, optional):
                The replacements to apply to given fields (dictionary key = field name)
            additional_regex_list (list, optional):
                These are not coming from the payload but dynamically
                added for special needs like determining the nicknames.

        Returns:
            str:
                Updated string with replacements.

        """

        r"""
        XML data sources may include "@" in Pandas column names as well!
        This happens if the XML elements have attributes.
               pattern = r"\{([\w@]+(\.[\w@]+)*)\}"
               pattern = r"\{(\w+(\.\w+)*)\}"
        Adjust Pattern to allow any sequence of characters withint the {...}
           pattern = r"\{([^}]*)\}"
        """

        # non-greedy match of placeholders that are surrounded by curly braces:
        pattern = r"\{(.*?)\}"

        had_lookup_error = False

        # Define a function to replace placeholders. This
        # function is called by re.sub() for each pattern match below.
        def replace(match: re.Match) -> str:
            # we want to change the variable of the main method
            nonlocal had_lookup_error

            # Extract the first captured group from the regex match, which corresponds
            # to the placeholder name. In the regex pattern r"\{(.*?)\}", the parentheses
            # define a capturing group that matches any content inside curly braces `{}`.
            # For example, in the string "Hello {user.name}", the match would capture "user.name".
            # `match.group(1)` retrieves this captured text (the placeholder name),
            # which will later be used to look up corresponding values in the provided data frame
            # row or replacement dictionary.
            field_name = match.group(1)
            # split up the field name into keys at ".", e.g. cm_vehicles.make
            keys = field_name.split(".")  # Splitting the variable and sub-value
            # we initialize value with the data frame row (pd.Series):
            value = row
            # Walk through the list of keys:
            for key in keys:
                # first we access the field in the row and handle the
                # exception that key may not be a valid column (KeyError):
                try:
                    # read the value of the data frame column defined by key
                    value = value[key]
                except KeyError as e:
                    self.logger.warning(
                        "KeyError: Cannot replace field -> '%s'%s as the data frame row does not have a column called -> '%s': %s",
                        field_name,
                        " (sub-key -> '{}')".format(key) if key != field_name else "",
                        field_name,
                        str(e),
                    )
                    had_lookup_error = True
                    return ""
                except TypeError:
                    # If we get an error with (value type -> <class 'float'>)
                    # this is actually indicating we have a NaN value!
                    self.logger.error(
                        "TypeError: Cannot replace field -> '%s'%s (value type -> %s). Expecting a dictionary-like value.",
                        field_name,
                        " (sub-key -> '{}')".format(key) if key != field_name else "",
                        str(type(value)),
                    )
                    had_lookup_error = True
                    return ""

                # if the returned value is a list we use the index parameter
                # to select the item in the list according to the given index
                # We handle the exception that index may be out of range for
                # the list (IndexError).
                # If the given index is None we return the whole list. This
                # is required for multi-value attributes.
                if isinstance(value, list) and index is not None and len(value) > 0:
                    try:
                        value = value[index]
                    except IndexError:
                        self.logger.error(
                            "Index error in replacement of list field -> '%s' using index -> %s.",
                            field_name,
                            str(index),
                        )
                        had_lookup_error = True
                        return ""

                # Check if the column or sub-field value is NaN or None. Then we should not
                # continue to process the keys and return "" (it is important to do this
                # after the test for a list above otherwise we may get an error
                # "The truth value of an array with more than one element is ambiguous."):
                if not isinstance(value, list) and pd.isna(value):
                    self.logger.debug(
                        "Cannot replace field -> '%s' as %s has no value in the current row!",
                        field_name,
                        (
                            "sub-key -> '{}'".format(key) if key != keys[0] else "column -> '{}'".format(key)
                        ),  # the first key is the column
                    )
                    had_lookup_error = True
                    return ""

            # end for key in keys

            if isinstance(value, list):
                if value == []:
                    had_lookup_error = True
                    return ""
            else:
                if pd.isna(value):
                    had_lookup_error = True
                    return ""
                value = str(value)

            if replacements and field_name in replacements:
                # replacements is a dictionary that is defined
                # in the payload. Each item is a dictionary
                # that can be looked up by the field name
                field_replacements = replacements[field_name]
                upper = field_replacements.get("upper_case", False)
                lower = field_replacements.get("lower_case", False)
                regex_list = field_replacements.get("regex_list", [])
            else:
                regex_list = []
                upper = False
                lower = False

            if additional_regex_list:
                regex_list = (
                    regex_list + additional_regex_list
                )  # don't do an append here as it would modify the original list

            if regex_list or upper or lower:
                if not isinstance(value, list):
                    value = self.cleanup_value(
                        cleanup_value=value,
                        regex_list=regex_list,
                        upper=upper,
                        lower=lower,
                    )
                else:  # we have a list, so we need to iterate
                    for v in value:
                        v = self.cleanup_value(
                            cleanup_value=v,
                            regex_list=regex_list,
                            upper=upper,
                            lower=lower,
                        )

            value = str(value)

            return value

        # end sub-method replace()

        if not input_string:
            return ""

        # Use re.sub() to replace placeholders using the defined function
        # replace() - see above.
        result_string = re.sub(pattern, replace, input_string)

        if had_lookup_error:
            return ""

        return result_string

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="cleanup_value")
    def cleanup_value(
        self,
        cleanup_value: str,
        regex_list: list,
        upper: bool = False,
        lower: bool = False,
    ) -> str:
        """Cleanup field values based on regular expressions.

        Args:
            cleanup_value (str):
                The string to clean up.
            regex_list (list):
                A list of regular expressions to apply.
            upper (bool, optional):
                If True, convert name to upper case letters.
            lower (bool, optional):
                If true, convert name to lower case letters.

        Returns:
            str:
                The cleaned string.

        """

        cleaned_string = cleanup_value

        if upper:
            cleaned_string = cleaned_string.upper()
        if lower:
            cleaned_string = cleaned_string.lower()

        if regex_list:
            try:
                for regex in regex_list:
                    # We use the pipe symbol to divide patterns from replacements
                    # this is a short-hand syntax to keep it simple. If there's
                    # no pipe in regex string and than we remove the pattern
                    # from the string
                    parts = regex.split("|")
                    pattern = parts[0]
                    replacement = ""  # Treat replacement as empty if no pipe specified
                    if len(parts) > 1:
                        pattern = r"\b" + pattern + r"\b"  # Match whole words only
                        replacement = parts[1]
                    cleaned_string = re.sub(pattern, replacement, cleaned_string)
            except re.error:
                self.logger.error(
                    "Invalid regular expression pattern -> %s",
                    pattern,
                )
                return cleanup_value

        return cleaned_string

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="evaluate_conditions")
    def evaluate_conditions(
        self,
        conditions: list,
        row: pd.Series,
        replacements: dict | None = None,
    ) -> bool:
        """Evaluate given conditions for a DataFrame series (i.e. a row).

        Args:
            conditions (list):
                A list of dictionaries that have a "field" (mandatory)
                and an "value" (optional) element.
            row (pd.Series):
                The current data row to pull data from.
            replacements (dict):
                The replacements to apply to given fields (dictionary key = field name).

        Returns:
            bool:
                True if all given conditions evaluate to True. False otherwise.

        """

        evaluated_condition = True

        # We traverse a list of conditions. All conditions must evaluate to true
        # otherwise the current workspace or document (i.e. the data row for these objects)
        # will be skipped:
        for condition in conditions:
            field = condition.get("field", None)
            if not field:
                self.logger.error(
                    "Missing field in condition.",
                )
                evaluated_condition = False
                break
            field_value = self.replace_bulk_placeholders(
                input_string=field,
                row=row,
                replacements=replacements,
            )
            self.logger.debug(
                "Evaluated field name -> '%s' to value -> '%s'",
                field,
                field_value,
            )
            # we have 3 options for value:
            # a) it does not exist in payload - then just the existance of the field is tested
            # b) it is a string - then we compare it 1:1 with the field value
            # c) it is a list of string - then the condition is met if one or more
            #    of the list values is equal to the field value
            value = condition.get("value", None)
            #            if not value:
            if value is None:
                # if there's no "value" element in the payload
                # this means that we just check the existance of the field
                if field_value:
                    # field does exist and has any non-"" value ==> condition met!
                    continue
                # field does not exist ==> condition not met!
                evaluated_condition = False
                break
            #
            # we handle string, boolean, and list values:
            #
            should_be_equal = condition.get("equal", True)
            if isinstance(value, str):
                if (should_be_equal and (value != str(field_value))) or (
                    not should_be_equal and (value == str(field_value))
                ):
                    self.logger.debug(
                        "String value -> '%s' is %s to field value -> '%s' but it %s. Condition not met for field -> '%s'!",
                        value,
                        "not equal" if should_be_equal else "equal",
                        field_value,
                        "should" if should_be_equal else "shouldn't",
                        field,
                    )
                    evaluated_condition = False
                    break
            elif isinstance(value, bool):
                # We can't do a bool(field_value) as this would return True
                # for any non-empty string. So we explictly convert to a bool value
                # in a safe way:
                bool_field_value: bool = field_value.lower() == "true" if field_value else False
                if (should_be_equal and (value != bool_field_value)) or (
                    not should_be_equal and (value == bool_field_value)
                ):
                    self.logger.debug(
                        "Boolean value -> '%s' is %s to field value -> '%s' but it %s. Condition not met for field -> '%s'!",
                        value,
                        "not equal" if should_be_equal else "equal",
                        str(bool_field_value),
                        "should" if should_be_equal else "shouldn't",
                        field,
                    )
                    evaluated_condition = False
                    break
            elif isinstance(value, list):
                for value_item in value:
                    # Check if we found a matching element and then break.
                    # Break equals positive result if should_be_equal is True:
                    if should_be_equal and (value_item == field_value):
                        break
                else:  # just executed if the for loop is not interrupted by break
                    # not finding a match is only negative if should_be_equal is TRUE:
                    if should_be_equal:
                        self.logger.debug(
                            "Value list -> %s does not include field value -> '%s'. Condition not met!",
                            str(value),
                            field_value,
                        )
                        evaluated_condition = False

        return evaluated_condition

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_classifications")
    def process_bulk_classifications(self, section_name: str = "bulkClassifications") -> bool:
        """Process bulk classifications in payload and bulk create them in Content Server (multi-threaded).

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True if payload has been processed without errors, False otherwise.

        """

        if not self._bulk_classifications:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        # For efficient idem-potent operation we may want to see which
        # bulk classifications have already been processed before:
        if self.check_status_file(
            payload_section_name=section_name,
            payload_specific=True,
            prefix="failure_",
        ):
            # Read payload from where we left it last time
            self._bulk_classifications = self.get_status_file(
                payload_section_name=section_name,
                prefix="failure_",
            )
            if not self._bulk_classifications:
                self.logger.error(
                    "Cannot load existing bulkClassifications failure file. Bailing out!",
                )
                return False

        success: bool = True

        for bulk_classification in self._bulk_classifications:
            # Check if element has been disabled in payload (enabled = false).
            # In this case we skip the element:
            enabled = bulk_classification.get("enabled", True)
            if not enabled:
                self.logger.info("Payload for bulk classification is disabled. Skipping...")
                continue

            # The payload element must have a "data_source" key:
            data_source_name = bulk_classification.get("data_source", None)
            if not data_source_name:
                self.logger.error("Missing data source name in bulk classification!")
                success = False
                continue

            self._log_header_callback(
                text="Process bulk classifications using data source -> '{}'".format(
                    data_source_name,
                ),
                char="-",
            )

            copy_data_source = bulk_classification.get("copy_data_source", False)
            force_reload = bulk_classification.get("force_reload", True)

            # Load and prepare the data source for the bulk processing:
            if copy_data_source:
                self.logger.info(
                    "Take a copy of data source -> '%s' to avoid side-effects for repeative usage of the data source...",
                    data_source_name,
                )
                data = Data(
                    self.process_bulk_datasource(
                        data_source_name=data_source_name,
                        force_reload=force_reload,
                    ),
                    logger=self.logger,
                )
            else:
                self.logger.info(
                    "Use original data source -> '%s' and not do a copy.",
                    bulk_classification["data_source"],
                )
                data = self.process_bulk_datasource(
                    data_source_name=data_source_name,
                    force_reload=force_reload,
                )
            if data is None:  # important to use "is None" here!
                self.logger.error("Failed to load data source -> '%s' for bulk classification!", data_source_name)
                success = False
                continue
            if data.get_data_frame() is None:  # important to use "is None" here!
                self.logger.error("Data source -> '%s' for bulk classification is empty!", data_source_name)
                continue

            # Check if fields with list substructures should be exploded.
            # We may want to do this outside the bulkDatasource to only
            # explode for bulkClassifications and not other bulk elements
            # like bulkDocuments or bulkWorkspaces:
            explosions = bulk_classification.get("explosions", [])
            for explosion in explosions:
                # explode field can be a string or a list
                # exploding multiple fields at once avoids
                # combinatorial explosions - this is VERY
                # different from exploding columns one after the other!
                if "explode_field" not in explosion and "explode_fields" not in explosion:
                    self.logger.error("Missing explosion field(s)!")
                    continue
                # we want to be backwards compatible...
                explode_fields = (
                    explosion["explode_field"] if "explode_field" in explosion else explosion["explode_fields"]
                )
                flatten_fields = explosion.get("flatten_fields", [])
                split_string_to_list = explosion.get("split_string_to_list", False)
                list_splitter = explosion.get(
                    "list_splitter",
                    ",",
                )  # don't have None as default!
                self.logger.info(
                    "Starting explosion of bulk classifications by field(s) -> %s (type -> %s). Size of data frame before explosion -> %s.",
                    str(explode_fields),
                    type(explode_fields),
                    str(len(data)),
                )
                data.explode_and_flatten(
                    explode_fields=explode_fields,
                    flatten_fields=flatten_fields,
                    make_unique=False,
                    split_string_to_list=split_string_to_list,
                    separator=list_splitter,
                    reset_index=True,
                )
                self.logger.info(
                    "Size of data frame after explosion -> %s.",
                    str(len(data)),
                )

            # Keep only selected rows if filters are specified in bulkClassifications.
            # We have this _after_ "explosions" to allow access to subfields as well.
            # We have this _before_ "sorting" and "deduplication" as we may keep the wrong
            # rows otherwise (unique / deduplication always keeps the first matching row).
            # We may want to do this outside the bulkDatasource to only
            # filter for bulkClassifications and not for bulkWorkspaces or
            # bulkWorkspaceRelationships:
            filters = bulk_classification.get("filters", [])
            if filters:
                data.filter(conditions=filters)

            # Sort the data frame if "sort" specified in payload. We may want to do this to have a
            # higher chance that rows with classifications names that may collapse into
            # one name are put into the same partition. This can avoid race conditions
            # between different Python threads.
            sort_fields = bulk_classification.get("sort", [])
            if sort_fields:
                self.logger.info(
                    "Start sorting of data frame for bulk classifications based on fields (columns) -> %s...",
                    str(sort_fields),
                )
                data.sort(sort_fields=sort_fields, inplace=True)
                self.logger.info(
                    "Sorting of data frame for bulk classifications based on fields (columns) -> %s completed.",
                    str(sort_fields),
                )

            # Check if duplicate rows for given fields should be removed. It is
            # important to do this after sorting as Pandas always keep the first occurance,
            # so ordering plays an important role in deduplication!
            unique_fields = bulk_classification.get("unique", [])
            if unique_fields:
                self.logger.info(
                    "Starting deduplication of data frame for bulk classifications with unique fields -> %s. Size of data frame before deduplication -> %s.",
                    str(unique_fields),
                    str(len(data)),
                )
                data.deduplicate(unique_fields=unique_fields, inplace=True)
                self.logger.info(
                    "Size of data frame after deduplication -> %s.",
                    str(len(data)),
                )

            # Read the optional categories payload:
            categories = bulk_classification.get("categories", None)
            if not categories:
                self.logger.info(
                    "Bulk classification payload has no category data! Will leave category attributes empty...",
                )

            # Get the operations to carry out during bulk processing.
            # Just doing a create is the default. Other options are
            # "update", "delete", "recreate":
            operations = bulk_classification.get("operations", ["create"])

            self.logger.info(
                "Bulk create classifications. Operations -> %s.",
                str(operations),
            )

            # See if bulkClassification definition has a specific thread number
            # otherwise it is read from a global environment variable:
            bulk_thread_number = int(
                bulk_classification.get("thread_number", BULK_THREAD_NUMBER),
            )

            partitions = data.partitionate(bulk_thread_number)

            # Create a list to hold the threads
            threads = []
            results = []

            # Create and start a thread for each partition
            for index, partition in enumerate(partitions, start=1):
                thread = threading.Thread(
                    name=f"{section_name}_{index:02}",
                    target=self.thread_wrapper,
                    args=(
                        self.process_bulk_classifications_worker,
                        bulk_classification,
                        partition,
                        categories,
                        operations,
                        results,
                    ),
                )
                # start a thread executing the process_bulk_classifications_worker() method below:
                self.logger.info("Starting thread -> '%s'...", str(thread.name))
                thread.start()
                threads.append(thread)
                # Avoid that all threads start at the exact same time with
                # potentially expired cookies that cases race conditions:
                time.sleep(1)
            # end for index, partition in enumerate(partitions, start=1)

            # Wait for all threads to complete
            for thread in threads:
                self.logger.info(
                    "Waiting for thread -> '%s' to complete...",
                    str(thread.name),
                )
                thread.join()
                self.logger.info("Thread -> '%s' has completed.", str(thread.name))

            if "classifications" not in bulk_classification:
                bulk_classification["classifications"] = {}

            # Print statistics for each thread. In addition,
            # check if all threads have completed without error / failure.
            # If there's a single failure in on of the thread results we
            # set 'success' variable to False.
            results.sort(key=lambda x: x["thread_name"])
            for result in results:
                self.logger.info(
                    "Thread -> '%s' completed with overall status '%s'.",
                    str(result["thread_name"]),
                    "successful" if result["success"] else "failed",
                )
                self.logger.info(
                    "Thread -> '%s' processed %s data rows with %s successful, %s failed, and %s skipped.",
                    str(result["thread_name"]),
                    str(
                        result["success_counter"] + result["failure_counter"] + result["skipped_counter"],
                    ),
                    str(result["success_counter"]),
                    str(result["failure_counter"]),
                    str(result["skipped_counter"]),
                )
                self.logger.info(
                    "Thread -> '%s' created %s classifications, updated %s classifications, and deleted %s classifications.",
                    str(result["thread_name"]),
                    str(result["create_counter"]),
                    str(result["update_counter"]),
                    str(result["delete_counter"]),
                )

                if not result["success"]:
                    success = False
                # Record all generated classifications. This should allow us
                # to restart in case of failures and avoid trying to
                # create classifications that have been created before.
                bulk_classification["classifications"].update(result["classifications"])
            self._log_header_callback(
                text="Completed processing of bulk classifications using data source -> '{}'".format(data_source_name),
                char="-",
            )

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._bulk_classifications,
        )

        return success

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_classifications_worker")
    def process_bulk_classifications_worker(
        self,
        bulk_classification: dict,
        partition: pd.DataFrame,
        categories: list | None = None,
        operations: list | None = None,
        results: list | None = None,
    ) -> None:
        """Thread worker to create classificatins in bulk.

        Each worker thread gets a partition of the rows that include
        the data required for the classification creation.

        Args:
            bulk_classification (dict):
                The payload of the bulkClassification.
            partition (pd.DataFrame):
                Data partition with rows to process.
            categories (list):
                List of category dictionaries.
            operations (list):
                Defines which operations should be applyed on classifications.
                Possible values are "create", "update", "delete", "recreate".
            results (list):
                A mutable list of thread results.

        """

        thread_id = threading.get_ident()
        thread_name = threading.current_thread().name

        self.logger.info(
            "Start working on data frame partition of size -> %s to bulk create classifications...",
            str(len(partition)),
        )

        # Avoid linter warnings - so make parameter default None while we
        # actually want ["create"] to be the default:
        if operations is None:
            operations = ["create"]

        result = {}
        result["thread_id"] = thread_id
        result["thread_name"] = thread_name
        result["success_counter"] = 0
        result["failure_counter"] = 0
        result["skipped_counter"] = 0
        result["create_counter"] = 0
        result["update_counter"] = 0
        result["delete_counter"] = 0
        result["classifications"] = {}
        result["success"] = True

        # Check if classifications have been processed before, e.i. testing
        # if a "classifications" key exists and if it is pointing to a non-empty list.
        # Additionally we check that classification updates are not enforced:
        if (
            bulk_classification.get("classifications")
            and "update" not in operations
            and "delete" not in operations
            and "recreate" not in operations
        ):
            existing_classifications = bulk_classification["classifications"]
            self.logger.info(
                "Found %s already processed classifications. Try to complete the job...",
                str(len(existing_classifications)),
            )
        else:
            existing_classifications = {}

        # See if external creation and modification dates are in the payload data:
        external_modify_date_field = bulk_classification.get("external_modify_date")
        external_create_date_field = bulk_classification.get("external_create_date")

        # See if we have a key field to uniquely identify an existing document:
        key_field = bulk_classification.get("key")

        # Get dictionary of replacements for bulk classification creations
        # this we will be used of all places data is read from the
        # data frame. Each dictionary item has the field name as the
        # dictionary key and a list of regular expressions as dictionary value
        replacements = bulk_classification.get("replacements")

        # In case the name cannot be resolved we allow to
        # specify an alternative name field in the payload.
        name_field = bulk_classification.get("name")

        # In case the name cannot be resolved we allow to
        # specify an alternative name field in the payload.
        name_field_alt = bulk_classification.get("name_alt")

        # In case the description cannot be resolved we allow to
        # specify an alternative description field in the payload.
        description_field = bulk_classification.get("description")

        # In case the description cannot be resolved we allow to
        # specify an alternative description field in the payload.
        description_field_alt = bulk_classification.get("description_alt")

        # Fetch the nickname field from the payload (if it is specified):
        nickname_field = bulk_classification.get("nickname")

        # In case the nickname cannot be resolved we allow to
        # specify an alternative nickname field in the payload.
        nickname_field_alt = bulk_classification.get("nickname_alt")

        # Nicknames are very limited in terms of allowed characters.
        # For nicknames we need additional regexp as we need to
        # replace all non-alphanumeric, non-space characters with ""
        # We also preserve hyphens in the first step to replace
        # them below with underscores. This is important to avoid
        # that different spellings of names produce different nicknames.
        # We want spellings with spaces match spellings with hyphens.
        # For this the classification names have a regexp "-| " in the payload.
        nickname_additional_regex_list = [r"[^\w\s-]"]

        total_rows = len(partition)

        # Process all rows in the partition that was given to this thread:
        for index, row in partition.iterrows():
            # Calculate percentage of completion:
            percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

            self.logger.info(
                "Processing data row -> %s for bulk classification creation (%.2f %% complete)...",
                str(index),
                percent_complete,
            )

            # Clear variables to esure clean state for each row:
            classification_id = None

            # Determine the classification name:
            classification_name = self.replace_bulk_placeholders(
                input_string=name_field,
                row=row,
                replacements=replacements,
            )
            # If we cannot get the classification name from the
            # name_field we try the alternative name field
            # (if specified in payload via "name_alt"):
            if not classification_name and name_field_alt:
                self.logger.debug(
                    "Row -> %s does not have the data to resolve the placeholders in classification name -> %s! Trying alternative name field -> %s...",
                    str(index),
                    name_field,
                    name_field_alt,
                )
                classification_name = self.replace_bulk_placeholders(
                    input_string=name_field_alt,
                    row=row,
                    replacements=replacements,
                )

            if not classification_name:
                self.logger.warning(
                    "Row -> %s does not have the required data to resolve -> %s for the classification name!%s",
                    str(index),
                    name_field,
                    " There's no alternative field name given in the payload (via 'name_alt')."
                    if not name_field_alt
                    else " The alternative field -> '{}' could not be resolved either!".format(
                        name_field_alt,
                    ),
                )
                result["skipped_counter"] += 1
                continue

            # Cleanse the classification name (allowed characters, maximum length):
            classification_name = OTCS.cleanse_item_name(classification_name)

            # Check if classification has been created before (either in this run
            # or in a former run of the customizer):
            if (
                classification_name in existing_classifications  # processed in former run?
                or classification_name in result["classifications"]  # processed in current run?
            ):
                self.logger.info(
                    "Classification -> '%s' does already exist. Skipping...",
                    classification_name,
                )
                result["skipped_counter"] += 1
                continue

            # Determine the description field:
            if description_field:
                description = self.replace_bulk_placeholders(
                    input_string=description_field,
                    row=row,
                )
                # If we cannot get the description from the
                # description_field we try the alternative description field
                # (if specified in payload via "description_alt"):
                if not description and description_field_alt:
                    self.logger.debug(
                        "Row -> %s does not have the data to resolve the placeholders in classification description -> %s! Trying alternative description field -> %s...",
                        str(index),
                        name_field,
                        description_field_alt,
                    )
                    description = self.replace_bulk_placeholders(
                        input_string=description_field_alt,
                        row=row,
                    )
            else:
                description = ""

            # Create a copy of the mutable operations list as we may
            # want to modify it per row:
            row_operations = list(operations)

            # Check if all data conditions to create the classification are met
            conditions = bulk_classification.get("conditions")
            if conditions:
                evaluated_condition = self.evaluate_conditions(
                    conditions=conditions,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_condition:
                    self.logger.info(
                        "Condition for bulk classification row -> %s not met. Skipping row for classification creation...",
                        str(index),
                    )
                    result["skipped_counter"] += 1
                    continue

            # Check if all data conditions to create or recreate the classification are met:
            if "create" in row_operations or "recreate" in row_operations:
                conditions_create = bulk_classification.get("conditions_create")
                if conditions_create:
                    evaluated_conditions_create = self.evaluate_conditions(
                        conditions=conditions_create,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_create:
                        self.logger.info(
                            "Create condition for bulk classification row -> %s not met. Excluding create operation...",
                            str(index),
                        )
                        if "create" in row_operations:
                            row_operations.remove("create")
                        if "recreate" in row_operations:
                            row_operations.remove("recreate")
                elif "recreate" in row_operations:
                    # we still create and recreate without conditions_create.
                    # But give a warning for 'recreate' without condition.
                    self.logger.warning(
                        "No create condition provided but 'recreate' operation requested. Recreating all existing classifications!",
                    )

            # Check if all data conditions to delete the classification are met:
            if "delete" in row_operations:
                conditions_delete = bulk_classification.get("conditions_delete")
                if conditions_delete:
                    evaluated_conditions_delete = self.evaluate_conditions(
                        conditions=conditions_delete,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_delete:
                        self.logger.info(
                            "Delete condition for bulk classification row -> %s not met. Excluding delete operation...",
                            str(index),
                        )
                        row_operations.remove("delete")
                else:  # without delete_conditions we don't delete!!
                    self.logger.warning(
                        "Delete operation requested for bulk classifications but conditions for deletion are missing! (specify 'conditions_delete')!",
                    )
                    row_operations.remove("delete")

            # Check if all data conditions to delete the classification are met:
            if "update" in row_operations:
                conditions_update = bulk_classification.get("conditions_update")
                if conditions_update:
                    evaluated_conditions_update = self.evaluate_conditions(
                        conditions=conditions_update,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_update:
                        self.logger.info(
                            "Update condition for bulk classification row -> %s not met. Excluding update operation...",
                            str(index),
                        )
                        row_operations.remove("update")

            # Determine the external modification date (if any):
            if external_modify_date_field:
                external_modify_date = self.replace_bulk_placeholders(
                    input_string=external_modify_date_field,
                    row=row,
                )
            else:
                external_modify_date = None

            # Determine the external creation date (if any):
            if external_create_date_field:
                external_create_date = self.replace_bulk_placeholders(
                    input_string=external_create_date_field,
                    row=row,
                )
            else:
                external_create_date = None

            # Determine the key field (if any):
            key = self.replace_bulk_placeholders(input_string=key_field, row=row) if key_field else None

            # check if classification with this nickname does already exist.
            # we also store the nickname to assign it to the new classification:
            if nickname_field:
                nickname = self.replace_bulk_placeholders(
                    input_string=nickname_field,
                    row=row,
                    replacements=replacements,
                    additional_regex_list=nickname_additional_regex_list,
                )
                # If we cannot get the nickname from the
                # nickname_field we try the alternative nickname field
                # (if specified in payload via "nickname_alt"):
                if not nickname and nickname_field_alt:
                    nickname = self.replace_bulk_placeholders(
                        input_string=nickname_field_alt,
                        row=row,
                        replacements=replacements,
                        additional_regex_list=nickname_additional_regex_list,
                    )

                # Nicknames for sure should not have leading or trailing spaces:
                nickname = nickname.strip()
                # Nicknames for sure are not allowed to include spaces:
                nickname = nickname.replace(" ", "_")
                # We also want to replace hyphens with underscores
                # to make sure that classification name spellings with
                # spaces and with hyphens are mapped to the same
                # classification nicknames (aligned with the classification names
                # that have a regexp rule for this in the payload):
                nickname = nickname.replace("-", "_")
                nickname = nickname.replace("___", "_")
                nickname = nickname.lower()
                response = self._otcs_frontend.get_node_from_nickname(nickname=nickname)
                if response:
                    found_classification_name = self._otcs_frontend.get_result_value(
                        response=response,
                        key="name",
                    )
                    found_classification_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                    if found_classification_name != classification_name:
                        self.logger.warning(
                            "Clash of nicknames for classification -> '%s' and classification -> '%s' (%s)!",
                            classification_name,
                            found_classification_name,
                            found_classification_id,
                        )
                    # Only skip, if classification 'update' or 'delete' is NOT requested:
                    elif "update" not in row_operations and "delete" not in row_operations:
                        self.logger.info(
                            "Classification -> '%s' with nickname -> '%s' does already exist (found -> '%s'). No update or delete operations requested or allowed. Skipping...",
                            classification_name,
                            nickname,
                            found_classification_name,
                        )
                        result["skipped_counter"] += 1
                        continue
            # end if nickname_field
            else:
                nickname = None

            # Based on the evaluation of conditions_create, conditions_delete,
            # and conditions_update we could end up with an empty operations list.
            # In such cases we skip the further processing of this row:
            if not row_operations:
                self.logger.info(
                    "Skip classification -> '%s' as row operations is empty (no create, update, delete or recreate). This may be because conditions_create, conditions_delete, and conditions_update have been evaluated to false for this row.",
                    classification_name,
                )
                result["skipped_counter"] += 1
                continue

            self.logger.info(
                "Bulk process classification -> '%s' using effective operations -> %s...",
                classification_name,
                str(row_operations),
            )

            self.logger.info(
                "Check if classification -> '%s' does already exist...",
                classification_name,
            )

            # Initialize variables - this is important!
            classification_old_name = None
            classification_id = None
            handle_name_clash = False
            classification_modify_date = None
            parent_id = None

            # We create a copy list to not modify original payload
            classification_path = list(bulk_classification.get("path", []))
            result_placeholders = self.replace_bulk_placeholders_list(
                input_list=classification_path,
                row=row,
                replacements=replacements,
            )
            if not result_placeholders:
                classification_path = None

            # We have 4 cases to differentiate:

            # 1. key given + key found = name irrelevant, item exist
            # 2. key given + key not found = if name exist it is a name clash
            # 3. no key given + name found = item exist
            # 4. no key given + name not found = item does not exist

            # We are NOT trying to compensate for a failed key lookup with a name lookup!
            # Names are only relevant if no key is in payload!

            if key:
                # if we have a key we need to also know which category attribute is
                # storing the value for the key:
                key_attribute = next(
                    (cat_attr for cat_attr in categories if cat_attr.get("is_key", False) is True),
                    None,
                )
                if not key_attribute:
                    self.logger.error(
                        "Bulk Classification has a key -> '%s' defined but none of the category attributes is marked as key ('is_key' is missing)!",
                        key,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue
                cat_name = key_attribute.get("name", None)
                att_name = key_attribute.get("attribute", None)
                set_name = key_attribute.get("set", None)

                # Get the parent classification element:
                response = self._otcs_frontend.get_node_by_volume_and_path(
                    volume_type=self._otcs_frontend.VOLUME_TYPE_CLASSIFICATION_VOLUME,
                    path=classification_path,
                    create_path=True,
                )
                parent_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if parent_id:
                    # Try to find the node that has the given key attribute value:
                    response = self._otcs_frontend.lookup_nodes(
                        parent_node_id=parent_id,
                        category=cat_name,
                        attribute=att_name,
                        attribute_set=set_name,
                        value=key,
                    )
                    classification_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                else:
                    # if we couldn't determine the parent ID this means there are
                    # now classification instances for this classification type. Then we set
                    # classification_id = None and let the code go into the else case below:
                    classification_id = None

                if classification_id:
                    # Case 1: key given + key found = name irrelevant, item exist
                    classification_old_name = self._otcs_frontend.get_result_value(
                        response=response,
                        key="name",
                    )
                    self.logger.info(
                        "Found existing classification -> %s (%s) in classification with ID -> %s using key value -> '%s', category -> '%s', and attribute -> '%s'.",
                        classification_old_name,
                        classification_id,
                        parent_id,
                        key,
                        cat_name,
                        att_name,
                    )
                    if classification_old_name != classification_name:
                        self.logger.info(
                            "Existing classification has the old name -> '%s' which is different from the new name -> '%s.'",
                            classification_old_name,
                            classification_name,
                        )
                    else:
                        classification_old_name = None
                    # We get the modify date of the existing classification.
                    classification_modify_date = self._otcs_frontend.get_result_value(
                        response=response,
                        key="modify_date",
                    )
                else:
                    # Case 2: key given + key not found = if name exist it is a name clash
                    self.logger.info(
                        "Couldn't find existing classification with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                        key,
                        cat_name,
                        att_name,
                        parent_id,
                    )
                    handle_name_clash = True
                # end if key_attribute
            # end if key
            else:
                # Get the parent classification element:
                response = self._otcs_frontend.get_node_by_volume_and_path(
                    volume_type=self._otcs_frontend.VOLUME_TYPE_CLASSIFICATION_VOLUME,
                    path=classification_path,
                )
                parent_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                # If we haven't a key we try by parent + name
                response = self._otcs_frontend.get_node_by_parent_and_name(
                    parent_id=parent_id,
                    name=classification_name,
                )
                classification_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if classification_id:
                    # Case 3: no key given + name found = item exist
                    self.logger.info(
                        "Found existing classification -> '%s' (%s).",
                        classification_name,
                        classification_id,
                    )
                    # We get the modify date of the existing classification.
                    classification_modify_date = self._otcs_frontend.get_result_value(
                        response=response,
                        key="modify_date",
                    )
                else:
                    # Case 4: no key given + name not found = item does not exist
                    self.logger.info(
                        "No existing classification with name -> '%s' in path -> %s.",
                        classification_name,
                        classification_path,
                    )

            # Check if we want to recreate an existing classification
            # then we handle the "delete" part of "recreate" first:
            if classification_id and "recreate" in row_operations:
                response = self._otcs_frontend.delete_node(
                    node_id=classification_id,
                    purge=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to recreate existing classification -> '%s' (%s)! Delete failed.",
                        (classification_name if classification_old_name is None else classification_old_name),
                        classification_id,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue
                result["delete_counter"] += 1
                self.logger.info(
                    "Successfully deleted existing classification -> '%s' (%s) as part of the recreate operation.",
                    (classification_name if classification_old_name is None else classification_old_name),
                    classification_id,
                )
                classification_id = None

            # Check if classification does not exists - then we create a new classification
            # if this is requested ("create" or "recreate" value in operations list in payload)
            if not classification_id and ("create" in row_operations or "recreate" in row_operations):
                # for Case 2 (we looked up the classification by key but could have name collisions)
                # we need to see if we have a name collision:
                if handle_name_clash and parent_id:
                    response = self._otcs_frontend.check_node_name(
                        parent_id=parent_id,
                        node_name=classification_name,
                    )
                    # result is a list of names that clash - if it is empty we have no clash
                    if response and response["results"]:
                        # We add the suffix with the key which should be unique:
                        self.logger.warning(
                            "Classification -> '%s' does already exist in folder with ID -> %s and we need to handle the name clash by using name -> '%s'",
                            classification_name,
                            parent_id,
                            classification_name + " (" + key + ")",
                        )
                        classification_name = classification_name + " (" + key + ")"

                # If category data is in payload we substitute
                # the values with data from the current data row:
                if categories:
                    # Make sure the threads are not changing data structures that
                    # are shared between threads. categories is a list of dicts.
                    # list and dicts are "mutable" data structures in Python!
                    worker_categories = self.process_bulk_categories(
                        row=row,
                        index=index,
                        categories=categories,
                        replacements=replacements,
                    )
                    # classification_category_data = self.prepare_item_create_form(
                    #     parent_id=parent_id,
                    #     categories=worker_categories,
                    #     subtype=self._otcs_frontend.ITEM_TYPE_CLASSIFICATION,
                    # )
                    classification_category_data = self.prepare_category_data(
                        categories_payload=worker_categories,
                        source_node_id=parent_id,
                    )
                    if not classification_category_data:
                        self.logger.error(
                            "Failed to prepare the category data for new classification -> '%s'!",
                            classification_name,
                        )
                        result["success"] = False
                        result["failure_counter"] += 1
                        continue  # for index, row in partition.iterrows()
                else:
                    classification_category_data = {}

                self.logger.info(
                    "Bulk create classification -> '%s'...",
                    classification_name,
                )

                # Create the classification with all provided information:
                response = self._otcs_frontend.create_item(
                    parent_id=parent_id,
                    item_type=self._otcs_frontend.ITEM_TYPE_CLASSIFICATION,
                    item_name=classification_name,
                    item_description=description,
                    category_data=classification_category_data,
                    external_create_date=external_create_date,
                    external_modify_date=external_modify_date,
                    show_error=False,
                )
                if response is None:
                    # Potential race condition: see if the classification has been created by a concurrent thread.
                    # So we better check if the classification is there even if the create_item() call delivered
                    # a 'None' response:
                    response = self._otcs_frontend.get_node_by_parent_and_name(
                        parent_id=parent_id,
                        name=classification_name,
                    )
                classification_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if not classification_id:
                    self.logger.error(
                        "Failed to bulk create classification -> '%s' under parent with ID -> %s!",
                        classification_name,
                        parent_id,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue

                # Set the categories separately:
                # if classification_category_data:
                #     response = self._otcs_frontend.update_item(
                #         node_id=classification_id,
                #         category_data=classification_category_data,
                #     )
                self.logger.info(
                    "Created classification -> '%s' with ID -> %s!",
                    classification_name,
                    classification_id,
                )
                result["create_counter"] += 1

            # end if not classification_id and "create" or "recreate" in row_operations

            # If "updates" are an requested row operation we update the existing classification with
            # fresh metadata from the payload. Additionally we check the external
            # modify date to support incremental load for content that has really
            # changed.
            # In addition we check that "delete" is not requested as otherwise it will
            # never go in elif "delete" ... below (and it does not make sense to update a classification
            # that is deleted in the next step...)
            elif (
                classification_id
                and "update" in row_operations
                and "delete" not in row_operations  # note the NOT !
                and OTCS.date_is_newer(
                    date_old=classification_modify_date,
                    date_new=external_modify_date,
                )
            ):
                # get the specific update operations given in the payload
                # if not specified we do all 4 update operations (name, description, categories and nickname)
                update_operations = bulk_classification.get(
                    "update_operations",
                    ["name", "description", "categories", "nickname"],
                )

                # If category data is in payload we substitute
                # the values with data from the current data row.
                # This is only done if "categories" update is not
                # excluded from the update_operations:
                if categories and "categories" in update_operations:
                    # Make sure the threads are not changing data structures that
                    # are shared between threads. categories is a list of dicts.
                    # list and dicts are "mutable" data structures in Python!
                    worker_categories = self.process_bulk_categories(
                        row=row,
                        index=index,
                        categories=categories,
                        replacements=replacements,
                    )
                    # classification_category_data = self.prepare_item_create_form(
                    #     parent_id=parent_id,
                    #     categories=worker_categories,
                    #     subtype=self._otcs_frontend.ITEM_TYPE_CLASSIFICATION,
                    # )
                    # Transform the payload structure into the format
                    # the OTCS REST API requires:
                    classification_category_data = self.prepare_category_data(
                        categories_payload=worker_categories,
                        source_node_id=classification_id,
                    )
                    if not classification_category_data:
                        self.logger.error(
                            "Failed to prepare the updated category data for classification -> '%s'!",
                            classification_name,
                        )
                        result["success"] = False
                        result["failure_counter"] += 1
                        continue  # for index, row in partition.iterrows()
                # end if categories
                else:
                    classification_category_data = {}

                self.logger.info(
                    "Update existing classification -> '%s' (%s) with operations -> %s...",
                    classification_old_name if classification_old_name else classification_name,
                    str(classification_id),
                    str(update_operations),
                )
                # Update the classification with all provided information:
                response = self._otcs_frontend.update_item(
                    classification_id=classification_id,
                    item_name=(classification_name if "name" in update_operations else None),
                    item_description=(description if "description" in update_operations else None),
                    category_data=(classification_category_data if "categories" in update_operations else None),
                    external_create_date=external_create_date,
                    external_modify_date=external_modify_date,
                    show_error=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to update existing classification -> '%s' (%s)!",
                        (classification_old_name if classification_old_name else classification_name),
                        classification_id,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue
                self.logger.info(
                    "Updated existing classification -> '%s' (%s).",
                    classification_name
                    if "name" in update_operations or not classification_old_name
                    else classification_old_name,
                    classification_id,
                )
                result["update_counter"] += 1

            # end elif "update" in row_operations...
            elif classification_id and "delete" in row_operations:
                # We delete with immediate purging to keep recycle bin clean
                # and to not run into issues with nicknames used in deleted items:
                response = self._otcs_frontend.delete_node(
                    node_id=classification_id,
                    purge=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to delete existing classification -> '%s' (%s)!",
                        (classification_old_name if classification_old_name else classification_name),
                        classification_id,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue
                self.logger.info(
                    "Successfully deleted existing classification -> '%s' (%s).",
                    classification_old_name if classification_old_name else classification_name,
                    classification_id,
                )
                result["delete_counter"] += 1
                classification_id = None
            # end elif classification_id and "delete" in row_operations

            # this is the plain old "it does exist and we just skip it case":
            elif classification_id:
                result["skipped_counter"] += 1
                self.logger.info(
                    "Skipped existing classification -> '%s' (%s).",
                    classification_old_name if classification_old_name else classification_name,
                    classification_id,
                )
            # this is the case where we just want to operate on existing classifications (update or delete)
            # but they do not exist:
            elif not classification_id and ("update" in row_operations or "delete" in row_operations):
                result["skipped_counter"] += 1
                self.logger.info(
                    "Skipped update/delete of non-existing classification -> '%s'.",
                    classification_old_name if classification_old_name else classification_name,
                )

            # The following code is executed for all operations
            # (create, recreate, update, delete):

            # Check if we want to set / update the nickname of the classification.
            # Precondition is we have determined a nickname, we have the classification ID
            # and the update of the nickname is either required (create, recreate)
            # or requested (update_operations include "nickname"):
            if (
                nickname
                and classification_id
                and (
                    "create" in row_operations
                    or "recreate" in row_operations
                    or ("update" in row_operations and "nickname" in update_operations)
                )
            ):
                response = self._otcs_frontend.set_node_nickname(
                    node_id=classification_id,
                    nickname=nickname,
                    show_error=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to assign nickname -> '%s' to classification -> '%s'!",
                        nickname,
                        classification_name,
                    )

            # Depending on the bulk operations (create, update, delete)
            # and the related conditions it may well be that classification_id is None.
            # Only if classification ID is not none we want to count this row as success:
            if classification_id is not None:
                result["success_counter"] += 1
                # Record the classification name and ID to allow to read it from failure file
                # and speedup the process.
                result["classifications"][classification_name] = classification_id
        # end for index...

        self.logger.info("End working...")

        results.append(result)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="start_impersonation")
    def start_impersonation(self, username: str, otcs_object: OTCS | None = None) -> bool:
        """Impersonate to a defined user.

        Args:
            username (str):
                The user to impersonate.
            otcs_object (OTCS | None, optional):
                The OTCS object to use. If not provided then the _otcs object
                in the Payload object is used.

        """

        if not otcs_object:
            otcs_object = self._otcs

        # Depending on the authentication type used with OTDS (token or ticket based)
        # the response structure is different:
        response = self._otds.impersonate_user(user_id=username)
        if not response:
            return False

        if "ticket" in response:
            otds_ticket = response.get("ticket", None)
            otcs_object.set_otds_ticket(ticket=otds_ticket)
        elif "access_token" in response:
            access_token = response.get("access_token", None)
            otcs_object.set_otds_token(token=access_token)
        else:
            self.logger.error("Impersonation response does not contain ticket or access_token!")
            return False

        otcs_object.invalidate_authentication_ticket()
        response = otcs_object.authenticate(revalidate=False)

        return bool(response)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="stop_impersonation")
    def stop_impersonation(self, otcs_object: OTCS | None = None) -> bool:
        """Impersonate back to admin user.

        Args:
            username (str):
                The user to impersonate.
            otcs_object (OTCS | None, optional):
                The OTCS object to use. If not provided then the _otcs object
                in the Payload object is used.

        """

        if not otcs_object:
            otcs_object = self._otcs

        # Clear OTDS ticket and token:
        otcs_object.set_otds_ticket(None)
        otcs_object.set_otds_token(None)

        # Clear OTCS ticket:
        otcs_object.invalidate_authentication_ticket()

        # Reauthenticate as admin (using username / password of OTCS object):
        response = otcs_object.authenticate(revalidate=True)

        return bool(response)

    # end method definition

    @tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_nifi_flows")
    def process_nifi_flows(self, section_name: str = "nifi") -> bool:
        """Process Knowledge Discovery Nifi flows in payload and create them in Nifi.

        Args:
            section_name (str, optional):
                The name of the payload section. It can be overridden
                for cases where multiple sections of same type
                are used (e.g. the "Post" sections).
                This name is also used for the "success" status
                files written to the Admin Personal Workspace.

        Returns:
            bool:
                True, if payload has been processed without errors, False otherwise

        """

        if not self._nifi_flows:
            self.logger.info(
                "Payload section -> '%s' is empty. Skipping...",
                section_name,
            )
            return True

        # If this payload section has been processed successfully before we
        # can return True and skip processing it once more:
        if self.check_status_file(payload_section_name=section_name):
            return True

        success: bool = True

        for nifi_flow in self._nifi_flows:
            if "name" not in nifi_flow:
                self.logger.error(
                    "Knowledge Discovery Nifi flow needs a name! Skipping to next Nifi flow...",
                )
                success = False
                continue
            name = nifi_flow["name"]

            # Check if element has been disabled in payload (enabled = false).
            # In this case we skip the element:
            if not nifi_flow.get("enabled", True):
                self.logger.info(
                    "Payload for Knowledge Discovery Nifi flow -> '%s' is disabled. Skipping...",
                    name,
                )
                continue

            if "file" not in nifi_flow:
                self.logger.error(
                    "Knowledge Discovery Nifi flow -> '%s' needs a file! Skipping to next Nifi flow...", name
                )
                success = False
                continue
            filename = nifi_flow["file"]

            parameters = nifi_flow.get("parameters", [])

            if not self._otkd:
                self.logger.error("Knowledge Discovery is not initialized. Stop processing Nifi flows.")
                success = False
                break

            # Optional layout positions of the flow:
            position_x = nifi_flow.get("position_x", 0.0)
            position_y = nifi_flow.get("position_y", 0.0)
            start = nifi_flow.get("start", False)

            self.logger.info("Processing Knowledge Discovery Nifi flow -> '%s'...", name)

            existing = self._otkd.get_process_group_by_name(name=name)
            if existing:
                self.logger.warning("Nifi flow -> '%s' does already exist. Updating parameters only...", name)
                # We better don't start existing flows!? Otherwise this may produce errors.
                start = False
            else:
                response = self._otkd.upload_process_group(
                    file_path=filename, name=name, position_x=position_x, position_y=position_y
                )
                if not response:
                    self.logger.error("Failed to upload new Nifi flow -> '%s' for Knowledge Discovery!", name)
                    success = False
                    continue
                self.logger.info("Sucessfully uploaded new Nifi flow -> '%s' for Knowledge Discovery.", name)

            for parameter in parameters:
                component = parameter.get("component", None)
                if not component:
                    self.logger.error("Missing component in parameter of Nifi flow -> '%s'!", name)
                    success = False
                    continue
                parameter_name = parameter.get("name", None)
                if not parameter_name:
                    self.logger.error(
                        "Missing name in parameter of Nifi flow -> '%s', component -> '%s'!", name, component
                    )
                    success = False
                    continue
                parameter_description = parameter.get("description", "")
                parameter_value = parameter.get("value", None)
                if not parameter_value:
                    self.logger.error(
                        "Missing value in parameter of Nifi flow -> '%s', component -> '%s'!", name, component
                    )
                    success = False
                    continue
                parameter_sensitive = parameter.get("sensitive", False)

                response = self._otkd.update_parameter(
                    component=component,
                    parameter=parameter_name,
                    value=parameter_value,
                    sensitive=parameter_sensitive,
                    description=parameter_description,
                )
                if not response:
                    self.logger.error("Failed to update parameter -> '%s' of Nifi flow -> '%s'!", parameter_name, name)
                    success = False
                    continue
                self.logger.info(
                    "Successfully updated parameter -> '%s' of component -> '%s' in Nifi flow -> '%s' to value -> '%s'.",
                    parameter_name,
                    component,
                    name,
                    parameter_value if not parameter_sensitive else "<sensitive>",
                )
            # end for parameter in parameters:
            if start:
                response = self._otkd.start_all_processors(name=name)
                if response:
                    self.logger.info("Successfully started Nifi flow -> '%s'.", name)
                else:
                    self.logger.error("Failed to start Nifi flow -> '%s'!", name)
                    success = False

                response = self._otkd.set_controller_services_state(name=name, state="ENABLED")
                if response:
                    self.logger.info("Successfully enabled Nifi Controller Services for Nifi flow -> '%s'.", name)
                else:
                    self.logger.error("Failed to enable Nifi Controller Services for Nifi flow -> '%s'!", name)
                    success = False

            else:
                self.logger.info("Don't (re)start Nifi flow -> '%s'.", name)
        # end for nifi_flow in self._nifi_flows:

        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=self._nifi_flows,
        )

        return success

__init__(payload_source, custom_settings_dir, k8s_object, otds_object, otac_object, otcs_backend_object, otcs_frontend_object, otcs_restart_callback, otiv_object, otpd_object, m365_object, core_share_object, placeholder_values, log_header_callback, browser_headless=True, stop_on_error=False, aviator_enabled=False, upload_status_files=True, otawp_object=None, otca_object=None, otkd_object=None, avts_object=None, logger=default_logger)

Initialize the Payload object.

Parameters:

Name Type Description Default
payload_source str

The path or URL to payload source file.

required
custom_settings_dir str

Provide a custom settings directory.

required
k8s_object K8s | None

The Kubernetes object. Pass None if deployment is not running in Kubernetes.

required
otds_object OTDS

The OTDS object. This is mandatory.

required
otac_object OTAC | None

The optional OTAC object (Archive Center). Pass None if Archive Center is not part of the deployment.

required
otcs_backend_object OTCS

The OTCS backend object.

required
otcs_frontend_object OTCS

The OTCS frontend object.

required
otcs_restart_callback Callable

A function to call if OTCS service needs a restart.

required
otiv_object OTIV | None

The OTIV object (Intelligent Viewing). Pass None if Intelligent Viewing is not part of the deployment.

required
otpd_object OTPD | None

The OTPD object (PowerDocs). Pass None if PowerDocs is not part of the deployment.

required
m365_object object

The M365 object to talk to Microsoft Graph API.

required
core_share_object CoreShare | None

The Core Share object.

required
placeholder_values dict

A dictionary of placeholder values to be replaced in admin settings.

required
log_header_callback Callable

Method to print a section break / header line into the log.

required
browser_headless bool

If true, the Browser for the Automation will be started in Headless mode (default)

True
stop_on_error bool

This flag controls if transport deployment should stop if a transport deployment in OTCS fails.

False
aviator_enabled bool

Flag that indicates whether or not the Content Aviator is enabled.

False
upload_status_files bool

Whether or not status file should be uploaded to the peronal workspace of the admin user in Content Server.

True
otawp_object OTAWP

An optional AppWorks Platform object.

None
otca_object OTCA

An optional Content Aviator object.

None
otkd_object OTKD

An optional Knowledge Discovery object.

None
avts_object AVTS

An optional Aviator Search object.

None
logger Logger

The logging object to use for all log messages. Defaults to default_logger.

default_logger
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def __init__(
    self,
    payload_source: str,
    custom_settings_dir: str,
    k8s_object: K8s | None,
    otds_object: OTDS,
    otac_object: OTAC | None,
    otcs_backend_object: OTCS,
    otcs_frontend_object: OTCS,
    otcs_restart_callback: Callable,
    otiv_object: OTIV | None,
    otpd_object: OTPD | None,
    m365_object: M365 | None,
    core_share_object: CoreShare | None,
    placeholder_values: dict,
    log_header_callback: Callable,
    browser_headless: bool = True,
    stop_on_error: bool = False,
    aviator_enabled: bool = False,
    upload_status_files: bool = True,
    otawp_object: OTAWP | None = None,
    otca_object: OTCA | None = None,
    otkd_object: OTKD | None = None,
    avts_object: AVTS | None = None,
    logger: logging.Logger = default_logger,
) -> None:
    """Initialize the Payload object.

    Args:
        payload_source (str):
            The path or URL to payload source file.
        custom_settings_dir (str):
            Provide a custom settings directory.
        k8s_object (K8s | None):
            The Kubernetes object.
            Pass None if deployment is not running in Kubernetes.
        otds_object (OTDS):
            The OTDS object. This is mandatory.
        otac_object (OTAC | None):
            The optional OTAC object (Archive Center).
            Pass None if Archive Center is not part of the deployment.
        otcs_backend_object (OTCS):
            The OTCS backend object.
        otcs_frontend_object (OTCS):
            The OTCS frontend object.
        otcs_restart_callback (Callable):
            A function to call if OTCS service needs a restart.
        otiv_object (OTIV | None):
            The OTIV object (Intelligent Viewing).
            Pass None if Intelligent Viewing is not part of the deployment.
        otpd_object (OTPD | None):
            The OTPD object (PowerDocs).
            Pass None if PowerDocs is not part of the deployment.
        m365_object (object):
            The M365 object to talk to Microsoft Graph API.
        core_share_object (CoreShare | None):
            The Core Share object.
        placeholder_values (dict):
            A dictionary of placeholder values to be replaced in admin settings.
        log_header_callback:
            Method to print a section break / header line into the log.
        browser_headless (bool):
            If true, the Browser for the Automation will be started in Headless mode (default)
        stop_on_error (bool):
            This flag controls if transport deployment should stop
            if a transport deployment in OTCS fails.
        aviator_enabled (bool):
            Flag that indicates whether or not the Content Aviator is enabled.
        upload_status_files (bool, optional):
            Whether or not status file should be uploaded to the peronal workspace
            of the admin user in Content Server.
        otawp_object (OTAWP):
            An optional AppWorks Platform object.
        otca_object (OTCA):
            An optional Content Aviator object.
        otkd_object (OTKD):
            An optional Knowledge Discovery object.
        avts_object (AVTS):
            An optional Aviator Search object.
        logger (logging.Logger, optional):
            The logging object to use for all log messages. Defaults to default_logger.

    """

    self.logger = logger.getChild("payload")
    for logfilter in logger.filters:
        self.logger.addFilter(logfilter)

    self._stop_on_error = stop_on_error
    self._payload_source = payload_source
    self._k8s = k8s_object
    self._otds = otds_object
    self._otac = otac_object
    self._otcs = otcs_backend_object
    self._otcs_backend = otcs_backend_object
    self._otcs_frontend = otcs_frontend_object
    self._otiv = otiv_object
    self._otpd = otpd_object
    self._m365 = m365_object
    self._core_share = core_share_object
    # The SAP, SuccessFactors and Salesforce objects only exists after external systems have been processed
    self._sap = None
    self._successfactors = None
    self._salesforce = None
    self._servicenow = None
    self._guidewire_policy_center = None
    self._guidewire_claims_center = None
    self._otmm = None
    self._otcs_source = None
    self._pht = None  # the OpenText prodcut hierarchy
    self._nhc = None  # National Hurricane Center
    self._otca = otca_object  # Content Aviator
    self._otkd = otkd_object  # Knowledge Discovery
    self._avts = avts_object  # Aviator Search
    self._browser_headless = browser_headless
    self._custom_settings_dir = custom_settings_dir
    self._placeholder_values = placeholder_values
    self._otcs_restart_callback = otcs_restart_callback
    self._log_header_callback = log_header_callback
    self._aviator_enabled = aviator_enabled
    self._http_object = HTTP(logger=self.logger)
    self.upload_status_files = upload_status_files
    self._otawp = otawp_object

add_transport_extractions(extractions)

Determine the number of extrations.

Safe them in a global list self._transport_extractions.

Parameters:

Name Type Description Default
extractions list

A list of extractions from a single transport package.

required

Returns:

Name Type Description
int int

THE number of extractions that have actually extracted data.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="add_transport_extractions")
def add_transport_extractions(self, extractions: list) -> int:
    """Determine the number of extrations.

    Safe them in a global list self._transport_extractions.

    Args:
        extractions (list):
            A list of extractions from a single transport package.

    Returns:
        int:
            THE number of extractions that have actually extracted data.

    """

    counter = 0
    for extraction in extractions:
        if extraction.get("enabled", True) and "data" in extraction:
            self._transport_extractions.append(extraction)
            counter += 1
    self.logger.info("Added -> %s transport extractions.", str(counter))

    return counter

check_external_system(external_system)

Check if external system is reachable.

Parameters:

Name Type Description Default
external_system dict

The payload data structure of external system. We assume here that sanity check for valid data is already done before.

required

Returns:

Name Type Description
bool bool

True = system is reachable, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="check_external_system")
def check_external_system(self, external_system: dict) -> bool:
    """Check if external system is reachable.

    Args:
        external_system (dict):
            The payload data structure of external system.
            We assume here that sanity check for valid data is already done before.

    Returns:
        bool:
            True = system is reachable, False otherwise.

    """

    as_url = external_system["as_url"]

    # Extract the hostname:
    external_system_hostname = urlparse(as_url).hostname
    # Write this information back into the data structure:
    external_system["external_system_hostname"] = external_system_hostname
    # Extract the port:
    external_system_port = urlparse(as_url).port if urlparse(as_url).port else 80
    # Write this information back into the data structure:
    external_system["external_system_port"] = external_system_port

    if self._http_object.check_host_reachable(
        hostname=external_system_hostname,
        port=external_system_port,
    ):
        self.logger.info(
            "Mark external system -> '%s' as reachable for later workspace creation and user synchronization...",
            external_system["external_system_name"],
        )
        external_system["reachable"] = True
        return True
    else:
        external_system["reachable"] = False
        return False

check_status_file(payload_section_name, payload_specific=True, prefix='success_')

Check if the payload section has been processed before.

This is done by checking the existance of a text file in the Admin Personal workspace in Content Server with the name of the payload section.

Parameters:

Name Type Description Default
payload_section_name str

The name of the payload section. This is used to construct the file name

required
payload_specific bool

Whether or not the success should be specific for each payload file or if success is "global" - like for the deletion of the existing M365 teams (which we don't want to execute per payload file)

True
prefix str

The prefix of the file. Typically, either "success_" or "failure_".

'success_'

Returns:

Name Type Description
bool bool

True if the payload has been processed successfully before, False otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="check_status_file")
def check_status_file(
    self,
    payload_section_name: str,
    payload_specific: bool = True,
    prefix: str = "success_",
) -> bool:
    """Check if the payload section has been processed before.

    This is done by checking the existance of a text file in the Admin Personal
    workspace in Content Server with the name of the payload section.

    Args:
        payload_section_name (str):
            The name of the payload section. This
            is used to construct the file name
        payload_specific (bool, optional):
            Whether or not the success should be specific for
            each payload file or if success is "global" - like for the deletion
            of the existing M365 teams (which we don't want to execute per
            payload file)
        prefix (str, optional):
            The prefix of the file. Typically, either "success_" or "failure_".

    Returns:
        bool:
            True if the payload has been processed successfully before, False otherwise

    """

    message = "successfully" if prefix.startswith("success_") else "with failures"

    self.logger.info(
        "Check if payload section -> '%s' has been processed %s before...",
        payload_section_name,
        message,
    )

    while not self._otcs.is_ready():
        self.logger.info(
            "OTCS is not ready. Cannot check status file for -> '%s' in OTCS. Waiting 30 seconds and retry...",
            payload_section_name,
        )
        time.sleep(30)

    response = self._otcs.get_node_by_volume_and_path(
        volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
    )  # write to Personal Workspace of Admin
    target_folder_id = self._otcs.get_result_value(response=response, key="id")
    if not target_folder_id:
        target_folder_id = 2004  # use Personal Workspace of Admin as fallback

    file_name = self.get_status_file_name(
        payload_section_name=payload_section_name,
        payload_specific=payload_specific,
        prefix=prefix,
    )

    status_document = self._otcs.get_node_by_parent_and_name(
        parent_id=int(target_folder_id),
        name=file_name,
        show_error=False,
    )
    if status_document and status_document.get("results", []):
        name = self._otcs.get_result_value(response=status_document, key="name")
        if name == file_name:
            self.logger.info(
                "Payload section -> '%s' has been processed %s before. %s...",
                payload_section_name,
                message,
                "Skipping" if prefix.startswith("success_") else "Retrying",
            )
            return True
    self.logger.info(
        "Payload section -> '%s' has not been processed %s before. Processing...",
        payload_section_name,
        message,
    )
    return False

cleanup_all_teams_m365(section_name='teamsM365Cleanup')

Delete Microsoft Teams that are left-overs from former deployments.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'teamsM365Cleanup'

Returns:

Name Type Description
bool bool

True, if teams have been deleted, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="cleanup_all_teams_m365")
def cleanup_all_teams_m365(self, section_name: str = "teamsM365Cleanup") -> bool:
    """Delete Microsoft Teams that are left-overs from former deployments.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if teams have been deleted, False otherwise.

    """

    if not isinstance(self._m365, M365):
        self.logger.error(
            "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
            section_name,
        )
        return False

    # We want this cleanup to only run once even if we have
    # multiple payload files - so we pass payload_specific=False here:
    if self.check_status_file(
        payload_section_name=section_name,
        payload_specific=False,
    ):
        self.logger.info(
            "Payload section -> '%s' has been processed successfully before. Skip cleanup of M365 teams...",
            section_name,
        )
        return True

    self.logger.info("Processing payload section -> '%s'...", section_name)

    # We don't want to delete MS Teams that are matching the regular OTCS Group Names (like "Sales")
    exception_list = self.get_all_group_names()

    # These are the patterns that each MS Teams needs to match at least one of to be deleted
    # Pattern 1: all MS teams with a name that has a number in brackets, like "(1234)"
    # Pattern 2: all MS Teams with a name that starts with a number followed by a space,
    #            followed by a "-" and followed by another space
    # Pattern 3: all MS Teams with a name that starts with "WS" and a 1-4 digit number
    #            (these are the workspaces for Purchase Contracts generated for Intelligent Filing)
    # Pattern 4: all MS Teams with a name that ends with a 1-2 character + a number in brackets, like (US-1000)
    #            this is a specialization of pattern 1
    # Pattern 5: remove the teams that are created for the dummy copy&paste template for the
    #            Intelligent Filing workspaces
    pattern_list = [
        r"\(\d+\)",
        r"\d+\s-\s",
        r"^WS\d{1,4}$",
        r"^.+?\s\(.{1,2}-\d+\)$",
        r"Purchase\sContract\s\(Template\sfor\sIntelligent\sFiling\)",
        r"^OpenText.*$",
        r"^P-100.*$",
        r"^OILRIG.*$",
        r"^AGILUM.*$",
        r"^HD-102T.*$",
        r"^SG325A.*$",
        r"^[A-Za-z0-9]{18} - .*$",  # delete teams that start with the typical Salesforce IDs (e.g. opportunities)
        r".*\s\([A-Z]{3,4}\)$",  # delete stale Locations from NTSB scenario
    ]

    result = self._m365.delete_all_teams(
        exception_list=exception_list,
        pattern_list=pattern_list,
    )

    # We want this cleanup to only run once even if we have
    # multiple payload files - so we pass payload_specific=False here:
    self.write_status_file(
        success=True,
        payload_section_name=section_name,
        payload_section=exception_list + pattern_list,
        payload_specific=False,
    )

    return result

cleanup_stale_teams_m365(workspace_types)

Delete Microsoft Teams that are left-overs from former deployments.

This method is currently not used.

Parameters:

Name Type Description Default
workspace_types list

The list of all workspace types.

required

Returns:

Name Type Description
bool bool

True if successful, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="cleanup_stale_teams_m365")
def cleanup_stale_teams_m365(self, workspace_types: list) -> bool:
    """Delete Microsoft Teams that are left-overs from former deployments.

    This method is currently not used.

    Args:
        workspace_types (list):
            The list of all workspace types.

    Returns:
        bool:
            True if successful, False otherwise.

    """

    if not isinstance(self._m365, M365):
        self.logger.error(
            "Microsoft 365 connection not setup properly. Skipping cleanup...",
        )
        return False

    if workspace_types == []:
        self.logger.error("Empty workspace type list!")
        return False

    for workspace_type in workspace_types:
        if "name" not in workspace_type:
            self.logger.error(
                "Workspace type -> '%s' does not have a name. Skipping...",
                workspace_type,
            )
            continue

        workspace_instances = self._otcs.get_workspace_instances_iterator(
            type_name=workspace_type["name"],
        )
        for workspace in workspace_instances:
            workspace = workspace.get("data").get("properties")

            workspace_name = workspace["name"]
            self.logger.info(
                "Check if stale Microsoft 365 Teams -> '%s' exist...",
                workspace_name,
            )
            self._m365.delete_teams(name=workspace_name)

    return True

cleanup_value(cleanup_value, regex_list, upper=False, lower=False)

Cleanup field values based on regular expressions.

Parameters:

Name Type Description Default
cleanup_value str

The string to clean up.

required
regex_list list

A list of regular expressions to apply.

required
upper bool

If True, convert name to upper case letters.

False
lower bool

If true, convert name to lower case letters.

False

Returns:

Name Type Description
str str

The cleaned string.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="cleanup_value")
def cleanup_value(
    self,
    cleanup_value: str,
    regex_list: list,
    upper: bool = False,
    lower: bool = False,
) -> str:
    """Cleanup field values based on regular expressions.

    Args:
        cleanup_value (str):
            The string to clean up.
        regex_list (list):
            A list of regular expressions to apply.
        upper (bool, optional):
            If True, convert name to upper case letters.
        lower (bool, optional):
            If true, convert name to lower case letters.

    Returns:
        str:
            The cleaned string.

    """

    cleaned_string = cleanup_value

    if upper:
        cleaned_string = cleaned_string.upper()
    if lower:
        cleaned_string = cleaned_string.lower()

    if regex_list:
        try:
            for regex in regex_list:
                # We use the pipe symbol to divide patterns from replacements
                # this is a short-hand syntax to keep it simple. If there's
                # no pipe in regex string and than we remove the pattern
                # from the string
                parts = regex.split("|")
                pattern = parts[0]
                replacement = ""  # Treat replacement as empty if no pipe specified
                if len(parts) > 1:
                    pattern = r"\b" + pattern + r"\b"  # Match whole words only
                    replacement = parts[1]
                cleaned_string = re.sub(pattern, replacement, cleaned_string)
        except re.error:
            self.logger.error(
                "Invalid regular expression pattern -> %s",
                pattern,
            )
            return cleanup_value

    return cleaned_string

construct_file_name(path, download_name, download_name_wildcards=False, file_extension='')

Construct the file name of the document.

This considers the path given in the download_dir, potential wildcards in the download_name variable and the file extensions provided (if any).

Parameters:

Name Type Description Default
path str

The base base in the file system. Placeholders need to be resolved before calling this method.

required
download_name str

The filenname of document in the file system. This may include wildcards like '.pdf' or 'en/.txt' or '*/.pfd'. Then the actual download file name is determined by a directory traversal using the 'path' parameter.

required
download_name_wildcards bool

Defines whether or not wildcards should be replaced in the download name. If True this walks through the given path and try to find the file in the filesystem that matches the wildcard pattern.

False
file_extension str

The file extension - typically 3 letters like 'pdf'. Defaults to "".

''

Returns:

Name Type Description
str str | None

The file name that is used to find the document in the filesystem (or if it does not yet exist) the name that should be used for download. This can be None

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="construct_file_name")
def construct_file_name(
    self,
    path: str,
    download_name: str,
    download_name_wildcards: bool = False,
    file_extension: str = "",
) -> str | None:
    """Construct the file name of the document.

    This considers the path given in the download_dir, potential wildcards in the
    download_name variable and the file extensions provided (if any).

    Args:
        path (str):
            The base base in the file system. Placeholders need to be resolved before
            calling this method.
        download_name (str):
            The filenname of document in the file system. This may include wildcards
            like '*.pdf' or 'en/*.txt' or '**/*.pfd'. Then the actual download
            file name is determined by a directory traversal using the 'path' parameter.
        download_name_wildcards (bool, optional):
            Defines whether or not wildcards should be replaced in the download name.
            If True this walks through the given path and try to find the file in the
            filesystem that matches the wildcard pattern.
        file_extension (str, optional):
            The file extension - typically 3 letters like 'pdf'. Defaults to "".

    Returns:
        str:
            The file name that is used to find the document in the filesystem (or if it does not yet exist)
            the name that should be used for download. This can be None

    """

    if not download_name:
        self.logger.error("Download name is missing. Cannot construct file name!")
        return None

    if path:
        # Normalize on Linux style path separators. Python is able to handle this also under Windows
        path = path.replace("\\", "/")
        if os.path.exists(path):
            # if we have a path specified it should be a directory and not a file!
            # The isfile() test is only working if the file already exist. It cannot
            # tell the difference between a folder and a file otherwise!
            if os.path.isfile(path):
                self.logger.warning(
                    "Download directory -> '%s' is pointing to an existing file and not a directory - please check the 'download_dir' variable in payload! Stripping path...",
                    path,
                )
                path = os.path.dirname(path)
                self.logger.info("Stripped path -> '%s'...", path)
        else:
            # if we have a path specified but it does not exist, then create it.
            try:
                os.makedirs(path)
            except (OSError, PermissionError):
                self.logger.error(
                    "Cannot create folder -> '%s' in file system!",
                    path,
                )
                return None
        if not path.endswith("/"):
            path += "/"  # slashes are save in Linux and Windows. Python handles this correctly

    # If we have a file extension and the given download does
    # not yet ends with it, we add it to the download name:
    if file_extension:
        if not file_extension.startswith("."):
            file_extension = "." + file_extension
        if not download_name.endswith(file_extension):
            download_name += file_extension

    # Also find files with wildcards in 'download_name' if requested
    # by download_name_wildcards == True.
    # IMPORTANT: this only matches ONE file as this method
    # also processes only ONE file at a time:
    if download_name_wildcards and any(char in download_name for char in "*?[]"):
        if not path:
            self.logger.error(
                "Download name includes wildcards but no (base) path is specified in payload (download_dir missing)! Cannot construct file name!",
            )
            return None
        file_name = None
        # Traverse the directory tree starting at 'path', looking for all files and subdirectories
        for _, _, tmpfiles in os.walk(path):
            # For each file found in the current directory:
            for file_data in tmpfiles:
                # Check if the current file name matches the 'download_name' pattern using wildcards (fnmatch)
                if fnmatch.fnmatch(file_data, download_name):
                    self.logger.debug(
                        "Found file name -> '%s' that is matching download name pattern -> '%s'",
                        file_data,
                        download_name,
                    )
                    # Construct the full file path by concatenating the base 'path' and the file name
                    file_name = path + file_data
                    # If we found a match we stop the (inner loop)
                    break
    # end if download_name_wildcards and any(char in download_name for char in "*?[]")
    else:
        # We have a normal filename without wildcards:
        file_name = path + download_name

    return file_name

determine_group_id(group)

Determine the id of a group - either from payload or from OTCS.

If the group is found in OTCS write back the ID into the payload.

Parameters:

Name Type Description Default
group dict

The group payload element.

required

Returns:

Name Type Description
int int

group ID

Side Effects

the group items are modified by adding an "id" dict element that includes the technical ID of the group in Content Server.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_group_id")
def determine_group_id(self, group: dict) -> int:
    """Determine the id of a group - either from payload or from OTCS.

    If the group is found in OTCS write back the ID into the payload.

    Args:
        group (dict):
            The group payload element.

    Returns:
        int: group ID

    Side Effects:
        the group items are modified by adding an "id" dict element that
        includes the technical ID of the group in Content Server.

    """

    # Is the ID already known in payload? (if determined before)
    if "id" in group:
        return group["id"]

    if "name" not in group:
        self.logger.error("Group needs a name to lookup the ID!")
        return 0
    group_name = group["name"]

    existing_groups = self._otcs.get_group(name=group_name)
    # We use the lookup method here as get_group() could deliver more
    # then 1 result element (in edge cases):
    existing_group_id = self._otcs.lookup_result_value(
        response=existing_groups,
        key="name",
        value=group_name,
        return_key="id",
    )

    # Have we found an exact match?
    if existing_group_id:
        self.logger.debug(
            "Found existing group -> '%s' with ID -> %s. Update ID in payload...",
            group_name,
            existing_group_id,
        )
        # Write ID back into the payload:
        group["id"] = existing_group_id
        return group["id"]
    else:
        self.logger.debug(
            "Cannot find an existing group -> '%s'",
            group_name,
        )
        return 0

determine_group_id_core_share(group)

Determine the id of a Core Share group.

This can either be derived from payload or from Core Share directly.

Parameters:

Name Type Description Default
group dict

The payload dictionary of the group.

required

Returns:

Type Description
str | None

str | None: Core Share Group ID or None.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_group_id_core_share")
def determine_group_id_core_share(self, group: dict) -> str | None:
    """Determine the id of a Core Share group.

    This can either be derived from payload or from Core Share directly.

    Args:
        group (dict):
            The payload dictionary of the group.

    Returns:
        str | None:
            Core Share Group ID or None.

    """

    # Is the ID already known in payload? (if determined before)
    if "core_share_id" in group:
        return group["core_share_id"]

    if "name" not in group:
        self.logger.error("Group needs a name to lookup the Core Share ID!")
        return None

    if not isinstance(self._core_share, CoreShare):
        self.logger.error(
            "Core Share connection not setup properly!",
        )
        return None

    core_share_group = self._core_share.get_group_by_name(name=group["name"])
    core_share_group_id = self._core_share.get_result_value(
        response=core_share_group,
        key="id",
    )

    # Have we found the group?
    if core_share_group_id:
        self.logger.debug(
            "Found existing Core Share group -> '%s' with ID -> %s. Update m365_id in payload...",
            group["name"],
            core_share_group_id,
        )
        # Write ID back into the payload:
        group["core_share_id"] = core_share_group_id
        return group["core_share_id"]
    else:
        self.logger.debug(
            "Cannot find an existing Core Share group -> '%s'",
            group["name"],
        )
        return None

determine_group_id_m365(group)

Determine the id of a M365 group - either from payload or from M365 via Graph API.

If the group is found in M365 write back the M365 group ID into the payload.

Parameters:

Name Type Description Default
group dict

The group payload element.

required

Returns:

Type Description
str | None

str | None: M365 group ID or None if the group is not found.

Side Effects

The group items are modified by adding an "m365_id" dict item that includes the technical ID of the group in Microsoft 365.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_group_id_m365")
def determine_group_id_m365(self, group: dict) -> str | None:
    """Determine the id of a M365 group - either from payload or from M365 via Graph API.

    If the group is found in M365 write back the M365 group ID into the payload.

    Args:
        group (dict):
            The group payload element.

    Returns:
        str | None:
            M365 group ID or None if the group is not found.

    Side Effects:
        The group items are modified by adding an "m365_id" dict item that
        includes the technical ID of the group in Microsoft 365.

    """

    # is the payload already updated with the M365 group ID?
    if "m365_id" in group:
        return group["m365_id"]

    if "name" not in group:
        self.logger.error("Group needs a name to lookup the M365 group ID!")
        return None
    group_name = group["name"]

    existing_group = self._m365.get_group(group_name=group_name)
    existing_group_id = self._m365.get_result_value(
        response=existing_group,
        key="id",
    )
    if existing_group_id:
        self.logger.debug(
            "Found existing Microsoft 365 group -> '%s' with ID -> %s. Update m365_id in payload...",
            group_name,
            existing_group_id,
        )
        # write back the M365 user ID into the payload
        group["m365_id"] = existing_group_id
        return group["m365_id"]
    else:
        self.logger.debug(
            "Cannot find an existing M365 group -> '%s'",
            group_name,
        )
        return None

determine_user_id(user)

Determine the id of a user - either from payload or from OTCS.

If the user is found in OTCS write back the ID into the payload.

Parameters:

Name Type Description Default
user dict

The user payload element.

required

Returns:

Name Type Description
int int

The user ID in OTCS.

Side Effects

The user items are modified by adding an "id" dict element that includes the technical ID of the user in Content Server

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_user_id")
def determine_user_id(self, user: dict) -> int:
    """Determine the id of a user - either from payload or from OTCS.

    If the user is found in OTCS write back the ID into the payload.

    Args:
        user (dict):
            The user payload element.

    Returns:
        int:
            The user ID in OTCS.

    Side Effects:
        The user items are modified by adding an "id" dict element that
        includes the technical ID of the user in Content Server

    """

    # Is the ID already known in payload? (if determined before)
    if "id" in user:
        return user["id"]

    user_name = user.get("name")
    if not user_name:
        self.logger.error("User needs a login name to lookup the ID!")
        return 0

    user_type = 17 if user.get("type", "User") == "ServiceUser" else 0

    response = self._otcs.get_user(name=user_name, user_type=user_type)

    # We use the lookup method here as get_user() could deliver more
    # then 1 result element (in edge cases):
    user_id = self._otcs.lookup_result_value(
        response=response,
        key="name",
        value=user_name,
        return_key="id",
    )

    # Have we found an exact match?
    if user_id:
        # Write ID back into the payload
        user["id"] = user_id
        return user["id"]
    else:
        self.logger.debug(
            "Cannot find an existing user -> '%s'!",
            user_name,
        )
        return 0

determine_user_id_core_share(user)

Determine the ID of a Core Share user.

The ID is either taken from payload or from Core Share directly.

Parameters:

Name Type Description Default
user dict

The payload dictionary of the user.

required

Returns:

Type Description
str | None

str | None: Core Share User ID or None.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_user_id_core_share")
def determine_user_id_core_share(self, user: dict) -> str | None:
    """Determine the ID of a Core Share user.

    The ID is either taken from payload or from Core Share directly.

    Args:
        user (dict):
            The payload dictionary of the user.

    Returns:
        str | None:
            Core Share User ID or None.

    """

    # Is the ID already known in payload? (if determined before)
    if "core_share_id" in user:
        return user["core_share_id"]

    if not isinstance(self._core_share, CoreShare):
        self.logger.error(
            "Core Share connection not setup properly!",
        )
        return False

    core_share_user_id = None

    # Next try to lookup ID via the email address:
    if "email" in user:
        core_share_user = self._core_share.get_user_by_email(email=user["email"])
        core_share_user_id = self._core_share.get_result_value(
            response=core_share_user,
            key="id",
        )

    # Last resort is to lookup the ID via firstname + lastname.
    # This is handy in case the Email has changed:
    if not core_share_user_id and "lastname" in user and "firstname" in user:
        core_share_user = self._core_share.get_user_by_name(
            first_name=user["firstname"],
            last_name=user["lastname"],
        )
        core_share_user_id = self._core_share.get_result_value(
            response=core_share_user,
            key="id",
        )

    # Have we found the user?
    if core_share_user_id:
        # Write ID back into the payload:
        user["core_share_id"] = core_share_user_id
        return user["core_share_id"]
    else:
        if "email" in user:
            self.logger.debug(
                "Did not find an existing Core Share user with email -> '%s'",
                user["email"],
            )
        else:
            self.logger.debug(
                "Cannot find an existing Core Share user -> '%s %s'",
                user.get("firstname"),
                user.get("lastname"),
            )
        return None

determine_user_id_m365(user)

Determine the id of a M365 user - either from payload or from M365 via Graph API.

If the user is found in M365 write back the M365 user ID into the payload.

Parameters:

Name Type Description Default
user dict

The user payload element.

required

Returns:

Type Description
str | None

str | None: The M365 user ID or None if the user is not found.

Side Effects

the user items are modified by adding an "m365_id" dict element that includes the technical ID of the user in Microsoft 365

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_user_id_m365")
def determine_user_id_m365(self, user: dict) -> str | None:
    """Determine the id of a M365 user - either from payload or from M365 via Graph API.

    If the user is found in M365 write back the M365 user ID into the payload.

    Args:
        user (dict):
            The user payload element.

    Returns:
        str | None:
            The M365 user ID or None if the user is not found.

    Side Effects:
        the user items are modified by adding an "m365_id" dict element that
        includes the technical ID of the user in Microsoft 365

    """

    # is the payload already updated with the M365 user ID?
    if "m365_id" in user:
        return user["m365_id"]

    if "name" not in user:
        self.logger.error("User needs a login name to lookup the M365 user ID!")
        return None
    user_name = user["name"]

    m365_user_name = user_name + "@" + self._m365.config()["domain"]
    existing_user = self._m365.get_user(user_email=m365_user_name)
    if existing_user:
        self.logger.debug(
            "Found existing Microsoft 365 user -> '%s' (%s) with ID -> %s. Update m365_id in payload...",
            existing_user["displayName"],
            existing_user["userPrincipalName"],
            existing_user["id"],
        )
        # write back the M365 user ID into the payload
        user["m365_id"] = existing_user["id"]
        return user["m365_id"]
    else:
        self.logger.debug(
            "Did not find an existing M365 user -> '%s'",
            user_name,
        )
        return None

determine_workspace_id(workspace)

Determine the node ID of a workspace - either from payload or from OTCS.

Parameters:

Name Type Description Default
workspace dict

The workspace payload element.

required

Returns:

Name Type Description
int int

The workspace Node ID.

Side Effects

The workspace items are modified by adding an "nodeId" dict element that includes the node ID of the workspace in Content Server.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_workspace_id")
def determine_workspace_id(self, workspace: dict) -> int:
    """Determine the node ID of a workspace - either from payload or from OTCS.

    Args:
        workspace (dict):
            The workspace payload element.

    Returns:
        int:
            The workspace Node ID.

    Side Effects:
        The workspace items are modified by adding an "nodeId" dict element that
        includes the node ID of the workspace in Content Server.

    """

    if "nodeId" in workspace:
        return workspace["nodeId"]

    response = self._otcs.get_workspace_by_type_and_name(
        type_name=workspace["type_name"],
        name=workspace["name"],
    )
    workspace_id = self._otcs.get_result_value(response=response, key="id")
    if workspace_id:
        if not isinstance(workspace_id, int):
            self.logger.warning("Converting workspace ID -> %s to integer...", str(workspace_id))
            workspace_id = int(workspace_id)
        # Write nodeID back into the payload
        workspace["nodeId"] = workspace_id
        return workspace_id
    else:
        self.logger.info(
            "Workspace of type -> '%s' and name -> '%s' does not yet exist.",
            workspace["type_name"],
            workspace["name"],
        )
        return 0

determine_workspace_type_and_template_id(workspace_type_name, workspace_template_name='')

Determine the IDs of type and template based on the provided names.

This depends on the self._workspace_types list to be up to date (see process_workspace_types()).

Parameters:

Name Type Description Default
workspace_type_name str

Name of the workspace type.

required
workspace_template_name str

Name of the workspace template. Defaults to "".

''

Returns:

Type Description
tuple[int | None, int | None]

tuple[int, int]: IDs of the workspace type (first) and workspace template (second).

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="determine_workspace_type_and_template_id")
def determine_workspace_type_and_template_id(
    self,
    workspace_type_name: str,
    workspace_template_name: str = "",
) -> tuple[int | None, int | None]:
    """Determine the IDs of type and template based on the provided names.

    This depends on the self._workspace_types list to be up to date
    (see process_workspace_types()).

    Args:
        workspace_type_name (str):
            Name of the workspace type.
        workspace_template_name (str, optional):
            Name of the workspace template. Defaults to "".

    Returns:
        tuple[int, int]:
            IDs of the workspace type (first) and workspace template (second).

    """

    # Check if the customizer has initialized the workspace type list
    if not self._workspace_types:
        self.logger.error(
            "Workspace type list is not initialized! This should never happen!",
        )
        return (None, None)

    # Find the workspace type with the name given in the payload:
    workspace_type = next(
        (item for item in self._workspace_types if item["name"] == workspace_type_name),
        None,
    )
    if workspace_type is None:
        self.logger.error(
            "Workspace type -> '%s' not found!",
            workspace_type_name,
        )
        return (None, None)

    workspace_type_id = workspace_type["id"]

    if not workspace_type.get("templates", []):
        self.logger.warning(
            "Workspace type -> '%s' does not have templates!",
            workspace_type_name,
        )
        return (workspace_type_id, None)

    if workspace_template_name:
        workspace_template = next(
            (item for item in workspace_type["templates"] if item["name"] == workspace_template_name),
            None,
        )
        if workspace_template:  # does this template exist?
            self.logger.info(
                "Workspace template -> '%s' has been specified in payload and it does exist.",
                workspace_template_name,
            )
        else:
            self.logger.error(
                "Workspace template -> '%s' has been specified in payload but it doesn't exist!",
                workspace_template_name,
            )
            self.logger.error(
                "Workspace type -> '%s' has only these templates -> %s",
                workspace_type_name,
                workspace_type["templates"],
            )
            return (workspace_type_id, None)

    # template to be used is NOT specified in the payload - then we just take the first one:
    else:
        workspace_template = workspace_type["templates"][0]
        self.logger.info(
            "Workspace template has not been specified in payload - taking the first one (%s)...",
            workspace_template,
        )

    workspace_template_id = workspace_template["id"]

    return (workspace_type_id, workspace_template_id)

download_bulk_document(bulk_document, download_name, row, result, source_otcs=None)

Download the bulk document and determine the file name (with path) and the mime type.

Parameters:

Name Type Description Default
bulk_document dict

The payload item for the bulk document.

required
download_name str

The download name of the document.

required
row Series

The current data frame series (row).

required
result dict

The result dictionary (mutable).

required
source_otcs OTCS | None

The Content Server OTCS object if documents should be loaded from Content Server. Defaults to None.

None

Returns:

Type Description
tuple[str, str]

tuple[str, str]: The file name (first) and the mime type (second).

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
25542
25543
25544
25545
25546
25547
25548
25549
25550
25551
25552
25553
25554
25555
25556
25557
25558
25559
25560
25561
25562
25563
25564
25565
25566
25567
25568
25569
25570
25571
25572
25573
25574
25575
25576
25577
25578
25579
25580
25581
25582
25583
25584
25585
25586
25587
25588
25589
25590
25591
25592
25593
25594
25595
25596
25597
25598
25599
25600
25601
25602
25603
25604
25605
25606
25607
25608
25609
25610
25611
25612
25613
25614
25615
25616
25617
25618
25619
25620
25621
25622
25623
25624
25625
25626
25627
25628
25629
25630
25631
25632
25633
25634
25635
25636
25637
25638
25639
25640
25641
25642
25643
25644
25645
25646
25647
25648
25649
25650
25651
25652
25653
25654
25655
25656
25657
25658
25659
25660
25661
25662
25663
25664
25665
25666
25667
25668
25669
25670
25671
25672
25673
25674
25675
25676
25677
25678
25679
25680
25681
25682
25683
25684
25685
25686
25687
25688
25689
25690
25691
25692
25693
25694
25695
25696
25697
25698
25699
25700
25701
25702
25703
25704
25705
25706
25707
25708
25709
25710
25711
25712
25713
25714
25715
25716
25717
25718
25719
25720
25721
25722
25723
25724
25725
25726
25727
25728
25729
25730
25731
25732
25733
25734
25735
25736
25737
25738
25739
25740
25741
25742
25743
25744
25745
25746
25747
25748
25749
25750
25751
25752
25753
25754
25755
25756
25757
25758
25759
25760
25761
25762
25763
25764
25765
25766
25767
25768
25769
25770
25771
25772
25773
25774
25775
25776
25777
25778
25779
25780
25781
25782
25783
25784
25785
25786
25787
25788
25789
25790
25791
25792
25793
25794
25795
25796
25797
25798
25799
25800
25801
25802
25803
25804
25805
25806
25807
25808
25809
25810
25811
25812
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="download_bulk_document")
def download_bulk_document(
    self,
    bulk_document: dict,
    download_name: str,
    row: pd.Series,
    result: dict,
    source_otcs: OTCS | None = None,
) -> tuple[str, str]:
    """Download the bulk document and determine the file name (with path) and the mime type.

    Args:
        bulk_document (dict):
            The payload item for the bulk document.
        download_name (str):
            The download name of the document.
        row (pd.Series):
            The current data frame series (row).
        result (dict):
            The result dictionary (mutable).
        source_otcs (OTCS | None, optional):
            The Content Server OTCS object if documents should be loaded
            from Content Server. Defaults to None.

    Returns:
        tuple[str, str]:
            The file name (first) and the mime type (second).

    """

    #
    # 1. Construct the local file name to upload:
    #

    path = bulk_document.get("download_dir", BULK_DOCUMENT_PATH)
    path = self.replace_bulk_placeholders(input_string=path, row=row)
    self.logger.debug("Download path for bulk document -> '%s'", path)

    # Determine file extensions:
    file_extension = bulk_document.get("file_extension", "")
    if file_extension:
        file_extension = self.replace_bulk_placeholders(
            input_string=file_extension,
            row=row,
        )
    file_extension_alt = bulk_document.get("file_extension_alt", "")
    if file_extension_alt:
        file_extension_alt = self.replace_bulk_placeholders(
            input_string=file_extension_alt,
            row=row,
        )

    # Determine file names:
    file_name = self.construct_file_name(
        path=path,
        download_name=download_name,
        download_name_wildcards=bulk_document.get("download_name_wildcards", False),
        file_extension=file_extension,
    )
    self.logger.debug("Constructed file name -> '%s'", file_name)
    if file_extension_alt and file_extension_alt != file_extension:
        file_name_alt = self.construct_file_name(
            path=path,
            download_name=download_name,
            download_name_wildcards=bulk_document.get(
                "download_name_wildcards",
                False,
            ),
            file_extension=file_extension_alt,
        )
    else:
        file_name_alt = file_name

    # If we couldn't construct the file name nor an alternative file name, we bail out:
    if not file_name and not file_name_alt:
        self.logger.error(
            "Cannot determine file name for document download (path -> '%s', download name -> '%s')!",
            path,
            download_name,
        )
        return None, None

    #
    # 2. Construct the mime type of the file to upload:
    #

    mime_type = bulk_document.get("mime_type", "application/pdf")
    mime_type = self.replace_bulk_placeholders(input_string=mime_type, row=row)
    mime_type_alt = bulk_document.get("mime_type_alt", "text/html")
    if mime_type_alt != "text/html":
        # if it is not the default it may have placeholders:
        mime_type_alt = self.replace_bulk_placeholders(
            input_string=mime_type_alt,
            row=row,
        )

    #
    # 3. Check if the file name(s) do(es) already exist and
    #    if we want to delete existing files (to download fresh ones)
    #

    file_exists = os.path.exists(file_name) if file_name else False

    # make sure there's no name conflict with stale documents:
    delete_download = bulk_document.get("delete_download", True)
    if file_exists and delete_download:
        os.remove(file_name)
        file_exists = False

    # Careful: If the construction of file name and the alternative
    # file name leads to identical result then there's actually
    # no alternative file name!
    if file_name != file_name_alt:
        file_exists_alt = os.path.exists(file_name_alt) if file_name_alt else False
        # make sure there's no name conflict with stale documents:
        if file_exists_alt and delete_download:
            os.remove(file_name_alt)
            file_exists_alt = False
    else:
        file_exists_alt = False

    #
    # 4. if the file does not exist in the local file system
    #    we now go and download it...
    #

    if not file_exists and not file_exists_alt:
        self.logger.debug("File -> '%s' does not exist in filesystem. We need to download it.", file_name)
        # Read "download retry number" and "wait before retry" duration from payload
        # (if specified) otherwise set default values
        wait_time = bulk_document.get("download_wait_time", 30)
        retries = bulk_document.get("download_retries", 2)

        source_type = bulk_document.get("source_type", "URL").lower()
        match source_type:
            case "contentserver":
                # Functionality to download the document from Content Server
                cs_source_id = bulk_document.get("cs_source_id", "")
                cs_source_id = self.replace_bulk_placeholders(
                    input_string=cs_source_id,
                    row=row,
                )
                if source_otcs is not None and source_otcs.otcs_ticket() is not None:
                    self.logger.info(
                        "Downloading document from source Content Server with ID -> %s...",
                        cs_source_id,
                    )

                    if source_otcs.download_document(
                        node_id=cs_source_id,
                        file_path=file_name,
                    ):
                        self.logger.debug(
                            "Successfully downloaded from Content Server using URL -> %s with ID -> %s to local file -> '%s'",
                            source_otcs.cs_public_url,
                            cs_source_id,
                            file_name,
                        )
                    else:
                        self.logger.error(
                            "Cannot download file from Content Server using URL -> %s with ID -> %s to local file -> '%s'. Skipping...",
                            source_otcs.cs_public_url(),
                            cs_source_id,
                            file_name,
                        )
                        result["skipped_counter"] += 1
                        return None, None
                else:
                    self.logger.error(
                        "Cannot download file with ID -> %s from Content Server. OTCS object not configured. Skipping...",
                        cs_source_id,
                    )
                    result["skipped_counter"] += 1
                    return None, None

            case "url":
                # Default case, download from accessible URL
                download_url = bulk_document.get("download_url")
                if download_url:
                    download_url = self.replace_bulk_placeholders(
                        input_string=download_url,
                        row=row,
                    )
                if not download_url:
                    self.logger.error(
                        "Download URL missing and no existing file -> '%s' in file system!",
                        file_name,
                    )
                    result["skipped_counter"] += 1
                    return None, None
                # Use the HHTP class to download the file:
                if not self._http_object.download_file(
                    url=download_url,
                    filename=file_name,
                    retries=retries,
                    wait_time=wait_time,
                    wait_on_status=[403],
                    show_error=False,
                ):
                    # Fetch alternative download URL (if avialable)
                    download_url_alt = bulk_document.get("download_url_alt")
                    if download_url_alt:
                        download_url_alt = self.replace_bulk_placeholders(
                            input_string=download_url_alt,
                            row=row,
                        )
                    # Check if we have an alternative download URL we try this now:
                    if download_url_alt:
                        self.logger.warning(
                            "Cannot download file from -> %s to local file -> '%s'. Trying alternative download -> %s to file -> '%s'...",
                            download_url,
                            file_name,
                            download_url_alt,
                            file_name_alt,
                        )
                        if self._http_object.download_file(
                            url=download_url_alt,
                            filename=file_name_alt,
                            retries=retries,
                            wait_time=wait_time,
                            wait_on_status=[403],
                            show_error=False,
                        ):
                            self.logger.debug(
                                "Successfully downloaded file from alternative URL -> %s to local file -> '%s'. Using this file...",
                                download_url_alt,
                                file_name_alt,
                            )
                            mime_type = mime_type_alt
                            file_name = file_name_alt
                        else:
                            # as we cannot fully rely one data source we don't treat this
                            # as an error but a warning:
                            self.logger.warning(
                                "Cannot download file from alternative URL -> %s to local file -> '%s'. Skipping...",
                                download_url_alt,
                                file_name_alt,
                            )
                            result["skipped_counter"] += 1
                            return None, None
                    # end if download_url_alt
                    else:
                        # as we cannot fully rely one data source we don't treat this
                        # as an error but a warning:
                        self.logger.warning(
                            "Cannot download file from URL -> %s to local file -> '%s'. Skipping...",
                            download_url,
                            file_name,
                        )
                        result["skipped_counter"] += 1
                        return None, None
                else:
                    self.logger.debug(
                        "Successfully downloaded file from -> %s to local file -> '%s'",
                        download_url,
                        file_name,
                    )
    # end if not file_exists and not file_exists_alt
    else:  # we already have a local file to reuse
        # If we found the alternative file name in file system
        # but not the regular one, then we switch to alternative
        # file name:
        if not file_exists and file_exists_alt:
            file_name = file_name_alt
            mime_type = mime_type_alt
        self.logger.info(
            "Reusing existing file -> '%s' in local storage.",
            file_name,
        )

    return file_name, mime_type

download_transport_package(package_url, download_dir=None)

Download the transort package from the given URL.

Parameters:

Name Type Description Default
package_url str

The URL to the transport package.

required
package_name str

The name of the transport package.

required
download_dir str

The file system directory to download to. If None, a temporary directory is automatically determined.

None

Returns:

Type Description
str | None

str | None: Path of the transport package in local file system or None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="download_transport_package")
def download_transport_package(
    self,
    package_url: str,
    download_dir: str | None = None,
) -> str | None:
    """Download the transort package from the given URL.

    Args:
        package_url (str):
            The URL to the transport package.
        package_name (str):
            The name of the transport package.
        download_dir (str, optional):
            The file system directory to download to. If None,
            a temporary directory is automatically determined.

    Returns:
        str | None:
            Path of the transport package in local file system or None in case of an error.

    """

    if not download_dir:
        download_dir = os.path.join(tempfile.gettempdir(), "transports/")

    if not self._http_object:
        self._http_object = HTTP(logger=self.logger)

    # Parse the URL
    parsed_url = urlparse(package_url)

    # Extract the path from the URL
    path = parsed_url.path

    # Get the file name from the path
    file_name = os.path.basename(path)

    download_name = os.path.join(download_dir, file_name)

    os.makedirs(download_dir, exist_ok=True)

    if not self._http_object.download_file(
        url=package_url,
        filename=download_name,
        show_error=True,
    ):
        return None

    self.logger.info(
        "Successfully downloaded transport package from -> '%s' to local file -> '%s'.",
        package_url,
        download_name,
    )

    return download_name

evaluate_conditions(conditions, row, replacements=None)

Evaluate given conditions for a DataFrame series (i.e. a row).

Parameters:

Name Type Description Default
conditions list

A list of dictionaries that have a "field" (mandatory) and an "value" (optional) element.

required
row Series

The current data row to pull data from.

required
replacements dict

The replacements to apply to given fields (dictionary key = field name).

None

Returns:

Name Type Description
bool bool

True if all given conditions evaluate to True. False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="evaluate_conditions")
def evaluate_conditions(
    self,
    conditions: list,
    row: pd.Series,
    replacements: dict | None = None,
) -> bool:
    """Evaluate given conditions for a DataFrame series (i.e. a row).

    Args:
        conditions (list):
            A list of dictionaries that have a "field" (mandatory)
            and an "value" (optional) element.
        row (pd.Series):
            The current data row to pull data from.
        replacements (dict):
            The replacements to apply to given fields (dictionary key = field name).

    Returns:
        bool:
            True if all given conditions evaluate to True. False otherwise.

    """

    evaluated_condition = True

    # We traverse a list of conditions. All conditions must evaluate to true
    # otherwise the current workspace or document (i.e. the data row for these objects)
    # will be skipped:
    for condition in conditions:
        field = condition.get("field", None)
        if not field:
            self.logger.error(
                "Missing field in condition.",
            )
            evaluated_condition = False
            break
        field_value = self.replace_bulk_placeholders(
            input_string=field,
            row=row,
            replacements=replacements,
        )
        self.logger.debug(
            "Evaluated field name -> '%s' to value -> '%s'",
            field,
            field_value,
        )
        # we have 3 options for value:
        # a) it does not exist in payload - then just the existance of the field is tested
        # b) it is a string - then we compare it 1:1 with the field value
        # c) it is a list of string - then the condition is met if one or more
        #    of the list values is equal to the field value
        value = condition.get("value", None)
        #            if not value:
        if value is None:
            # if there's no "value" element in the payload
            # this means that we just check the existance of the field
            if field_value:
                # field does exist and has any non-"" value ==> condition met!
                continue
            # field does not exist ==> condition not met!
            evaluated_condition = False
            break
        #
        # we handle string, boolean, and list values:
        #
        should_be_equal = condition.get("equal", True)
        if isinstance(value, str):
            if (should_be_equal and (value != str(field_value))) or (
                not should_be_equal and (value == str(field_value))
            ):
                self.logger.debug(
                    "String value -> '%s' is %s to field value -> '%s' but it %s. Condition not met for field -> '%s'!",
                    value,
                    "not equal" if should_be_equal else "equal",
                    field_value,
                    "should" if should_be_equal else "shouldn't",
                    field,
                )
                evaluated_condition = False
                break
        elif isinstance(value, bool):
            # We can't do a bool(field_value) as this would return True
            # for any non-empty string. So we explictly convert to a bool value
            # in a safe way:
            bool_field_value: bool = field_value.lower() == "true" if field_value else False
            if (should_be_equal and (value != bool_field_value)) or (
                not should_be_equal and (value == bool_field_value)
            ):
                self.logger.debug(
                    "Boolean value -> '%s' is %s to field value -> '%s' but it %s. Condition not met for field -> '%s'!",
                    value,
                    "not equal" if should_be_equal else "equal",
                    str(bool_field_value),
                    "should" if should_be_equal else "shouldn't",
                    field,
                )
                evaluated_condition = False
                break
        elif isinstance(value, list):
            for value_item in value:
                # Check if we found a matching element and then break.
                # Break equals positive result if should_be_equal is True:
                if should_be_equal and (value_item == field_value):
                    break
            else:  # just executed if the for loop is not interrupted by break
                # not finding a match is only negative if should_be_equal is TRUE:
                if should_be_equal:
                    self.logger.debug(
                        "Value list -> %s does not include field value -> '%s'. Condition not met!",
                        str(value),
                        field_value,
                    )
                    evaluated_condition = False

    return evaluated_condition

extract_properties_from_transport_packages(business_object_type, bo_type_name, bo_type_id)

Extract properties from transport packages for a given Business Object Type.

Parameters:

Name Type Description Default
business_object_type dict

The Business Object Type dictionary to be enriched with properties.

required
bo_type_name str

The name of the Business Object Type.

required
bo_type_id int

The ID of the Business Object Type.

required

Returns:

Name Type Description
bool bool

True if properties were successfully extracted and added, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="extract_properties_from_transport_packages")
def extract_properties_from_transport_packages(
    self, business_object_type: dict, bo_type_name: str, bo_type_id: int
) -> bool:
    """Extract properties from transport packages for a given Business Object Type.

    Args:
        business_object_type (dict):
            The Business Object Type dictionary to be enriched with properties.
        bo_type_name (str):
            The name of the Business Object Type.
        bo_type_id (int):
            The ID of the Business Object Type.

    Returns:
        bool:
            True if properties were successfully extracted and added, False otherwise.

    """

    business_object_type["business_properties"] = []
    business_object_type["business_property_groups"] = []

    # Now we complete the data with what we have extracted from the transport packages
    # for Business Object Types. This is a workaround for the insufficient REST API
    # implementation (see otcs.get_business_object_type)
    if self._transport_extractions:
        self.logger.info(
            "Enrich business object type -> '%s' (%d) with extractions from transport packages (found '%s' extractions)...",
            bo_type_name,
            bo_type_id,
            str(len(self._transport_extractions)),
        )
    else:
        self.logger.info(
            "No transport extractions are recorded. This may be because of customizer restart.",
        )
        extraction_status_file = "transportPackagesExtractions"
        if self.check_status_file(payload_section_name=extraction_status_file):
            self.logger.info(
                "Try to load extractions from success file -> '%s'...",
                extraction_status_file,
            )
            self._transport_extractions = self.get_status_file(
                payload_section_name=extraction_status_file,
            )

    for extraction in self._transport_extractions:
        xpath = extraction.get("data")
        data_list = extraction.get("data")
        if not data_list:
            self.logger.error(
                "Extraction -> '%s' is missing the data element. Skipping...",
                xpath,
            )
            return False
        if not isinstance(data_list, list):
            self.logger.warning(
                "Extracted data for -> '%s' is not a list. Cannot process it. Skipping...",
                xpath,
            )
            return False

        """
        The following loop processes a dictionary of this structure:

        llnode: {
            '@created': '2017-11-23T16:43:35',
            '@createdby': '1000',
            '@createdbyname': 'Terrarium Admin',
            '@description': '',
            '@id': '16013',
            '@modified': '2023-12-09T12:08:21',
            '@name': 'SFDC Order',
            '@objname': 'Business Object Type',
            '@objtype': '889',
            '@ownedby': '1000',
            '@ownedbyname': 'Terrarium Admin',
            '@parentguid': '95F96645-057D-4EAF-9083-BE9F24C0CB6C',
            '@parentid': '2898',
            '@parentname': 'Business Object Types',
            ...
            'Nickname': {'@domain': ''},
            'name': {'@xml:lang': 'en', '#text': 'SFDC Order'},
            'description': {'@xml:lang': 'en'},
            'businessObjectTypeInfo': {
                'basicInfo': {
                    '@businessObjectId': '9',
                    '@businessobjectType': 'Order',
                    '@deleted': 'false',
                    '@name': 'SFDC Order',
                    '@subtype': '889',
                    '@useBusWorkspace': 'true',
                    'displayUrl': {...}
                },
                'businessApplication': {
                    'businessObjectTypeReference': {...}},
                    'businessAttachmentInfo': {
                        '@automaticAddingOfBusinessObject': 'false',
                        '@canbeAddedAsBusinessObject': 'false',
                        '@enableBADIBeforeAddingBO': 'false',
                        '@enableBADIBeforeRemovingBO': 'false',
                        '@enableMetadataMapping': 'false'
                    },
                    'managedObjectTypes': {
                        'managedObjectType': [...]
                    },
                    'multilingualNames': {'language': [...]},
                    'callbacks': {'callback': [...]},
                    'workspaceTypeReference': {'@isDefaultDisplay': 'false', '@isDefaultSearch': 'false', 'businessObjectTypeReference': {...}},
                    'businessPropertyMappings': {
                        'propertyMapping': [...]
                    },
                    'businessPropertyGroupMappings': {
                        'propertyGroupMapping': [...]
                    },
                    'documentTypes': {
                        'documentType': [...]
                    },
                    'CustomBOTypeInfo': None
                }
        }
        """

        for data in data_list:
            #
            # Level 1: llnode
            #
            llnode = data.get("llnode")
            if not llnode:
                self.logger.error(
                    "Missing llnode structure in data. Skipping...",
                )
                return False

            #
            # Level 2: businessobjectTypeInfo
            #
            business_object_type_info = llnode.get(
                "businessobjectTypeInfo",
                None,
            )
            if not business_object_type_info:
                self.logger.error(
                    "Information is missing for business object type -> '%s'. Skipping...",
                    bo_type_name,
                )
                return False

            # Check if this extraction is for the current business object type:
            basic_info = business_object_type_info.get("basicInfo", None)
            if not basic_info:
                self.logger.error(
                    "Cannot find basic info of business object type -> '%s'. Skipping...",
                    bo_type_name,
                )
                return False
            name = basic_info.get("@businessobjectType", "")
            if not name:
                self.logger.error(
                    "Cannot find name of business object type -> '%s'. Skipping...",
                    bo_type_name,
                )
                return False
            obj_type = llnode.get("@objtype", None)
            # we need to compare bo_type and NOT bo_type_name here!
            # Otherwise we don't find the SAP and SuccessFactors data:
            if name != bo_type_name or obj_type != "889":
                continue

            #
            # Level 3: businessPropertyMappings - plain, non-grouped properties
            #
            business_property_mappings = business_object_type_info.get(
                "businessPropertyMappings",
                None,
            )
            if not business_property_mappings:
                self.logger.info(
                    "No property mapping for business object type -> '%s'. Skipping...",
                    bo_type_name,
                )
                continue
            property_mappings = business_property_mappings.get(
                "propertyMapping",
                [],
            )
            # This can happen if there's only 1 propertyMapping;
            if not isinstance(property_mappings, list):
                self.logger.debug(
                    "Found a single property mapping in a dictionary (not in a list). Package it into a list...",
                )
                property_mappings = [property_mappings]

            for property_mapping in property_mappings:
                property_name = property_mapping.get("@propertyName")
                attribute_name = property_mapping.get("@attributeName")
                category_id = property_mapping.get("@categoryId")
                mapping_type = property_mapping.get("@type")
                self.logger.debug(
                    "%s Property Mapping for Business Object -> '%s' property -> '%s' is mapped to attribute -> '%s' (category -> %s)",
                    mapping_type,
                    bo_type_name,
                    property_name,
                    attribute_name,
                    category_id,
                )
                business_object_type["business_properties"].append(
                    property_mapping,
                )

            #
            # Level 3: businessPropertyGroupMappings - grouped properties
            #
            business_property_group_mappings = business_object_type_info.get(
                "businessPropertyGroupMappings",
                None,
            )
            if not business_property_group_mappings:
                self.logger.info(
                    "No property group mapping for business object type -> '%s'. Skipping...",
                    bo_type_name,
                )
                continue

            property_group_mappings = business_property_group_mappings.get(
                "propertyGroupMapping",
                [],
            )
            # This can happen if there's only 1 propertyMapping;
            if isinstance(property_group_mappings, dict):
                self.logger.debug(
                    "Found a single property group mapping in a dictionary (not in a list). Pack it into a list...",
                )
                property_group_mappings = [property_group_mappings]

            for property_group_mapping in property_group_mappings:
                group_name = property_group_mapping.get("@groupName")
                set_name = property_group_mapping.get("@setName")
                category_id = property_group_mapping.get("@categoryId")
                mapping_type = property_group_mapping.get("@type")
                self.logger.debug(
                    "%s property group mapping for business object -> %s: group -> '%s' is mapped to set -> '%s' (category -> %s)",
                    mapping_type,
                    bo_type_name,
                    group_name,
                    set_name,
                    category_id,
                )

                property_mappings = property_group_mapping.get(
                    "propertyMapping",
                    [],
                )
                # This can happen if there's only 1 propertyMapping;
                if not isinstance(property_mappings, list):
                    self.logger.debug(
                        "Found a single property mapping in a dictionary (not in a list). Package it into a list...",
                    )
                    property_mappings = [property_mappings]

                for property_mapping in property_mappings:
                    # for nested mappings we only have 2 fields - the rest is on the group level - see above
                    property_name = property_mapping.get("@propertyName")
                    attribute_name = property_mapping.get("@attributeName")
                    self.logger.debug(
                        "%s Property Mapping inside group for Business Object -> '%s', group -> '%s', property -> '%s' is mapped to set -> %s, attribute -> '%s' (category -> %s)",
                        mapping_type,
                        bo_type_name,
                        group_name,
                        property_name,
                        set_name,
                        attribute_name,
                        category_id,
                    )
                    # we write the group / set information also in the property mapping
                    # tp have a plain list with all information:
                    property_mapping["@groupName"] = group_name
                    property_mapping["@setName"] = set_name
                    property_mapping["@type"] = mapping_type
                    business_object_type["business_property_groups"].append(
                        property_mapping,
                    )
            # end for property_group_mapping in property_group_mappings
        # end for data in data_list
    # end for extraction in self._transport_extractions

    return True

generate_password(length, use_special_chars=False, min_special=1, min_numerical=1, min_upper=1, min_lower=1, override_special=None)

Generate random passwords with a given specification.

Parameters:

Name Type Description Default
length int

Define password length.

required
use_special_chars bool

Define if special characters should be used. Defaults to False.

False
min_special int

Define min amount of special characters. Defaults to 1.

1
min_numerical int

Define if numbers should be used. Defaults to 1.

1
min_upper int

Define mininum number of upper case letters. Defaults to 1.

1
min_lower int

Define minimum number of lower case letters. Defaults to 1.

1
override_special string | None

Define special characters to be used, if not set: !@#$%^&*()_-+=<>?/{}[]. Defaults to None.

None

Returns:

Type Description
str | None

str | None: The generated password. None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def generate_password(
    self,
    length: int,
    use_special_chars: bool = False,
    min_special: int = 1,
    min_numerical: int = 1,
    min_upper: int = 1,
    min_lower: int = 1,
    override_special: str | None = None,
) -> str | None:
    """Generate random passwords with a given specification.

    Args:
        length (int):
            Define password length.
        use_special_chars (bool, optional):
            Define if special characters should be used. Defaults to False.
        min_special (int, optional):
            Define min amount of special characters. Defaults to 1.
        min_numerical (int, optional):
            Define if numbers should be used. Defaults to 1.
        min_upper (int, optional):
            Define mininum number of upper case letters. Defaults to 1.
        min_lower (int, optional):
            Define minimum number of lower case letters. Defaults to 1.
        override_special (string | None, optional):
            Define special characters to be used, if not set: !@#$%^&*()_-+=<>?/{}[]. Defaults to None.

    Returns:
        str | None:
            The generated password. None in case of an error.

    """
    # Define character sets
    lowercase_letters = string.ascii_lowercase
    uppercase_letters = string.ascii_uppercase
    numerical_digits = string.digits
    special_characters = "!@#$%^&*()_-+=<>?/{}[]"

    if override_special:
        special_characters = override_special
    # Ensure minimum requirements are met

    if min_special + min_numerical + min_upper + min_lower > length:
        self.logger.error("Minimum requirements exceed password length! Cannot generate password.")
        return None

    # Initialize the password
    password = []

    # Add required characters
    password.extend(random.sample(lowercase_letters, min_lower))
    password.extend(random.sample(uppercase_letters, min_upper))
    password.extend(random.sample(numerical_digits, min_numerical))

    if use_special_chars:
        password.extend(random.sample(special_characters, min_special))

    # Fill the rest of the password with random characters
    remaining_length = length - len(password)
    all_chars = lowercase_letters + uppercase_letters + numerical_digits

    if use_special_chars:
        all_chars += special_characters

    password.extend(random.choices(all_chars, k=remaining_length))

    # Shuffle the password to ensure randomness
    random.shuffle(password)

    # Convert the password list to a string
    final_password = "".join(password)

    return final_password

get_all_group_names()

Construct a list of all group name.

Returns:

Name Type Description
list list

A list of all group names.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_all_group_names")
def get_all_group_names(self) -> list:
    """Construct a list of all group name.

    Returns:
        list:
            A list of all group names.

    """

    return [group.get("name") for group in self._groups]

get_bulk_document_location(workspace, row, index, replacements)

Determine the workspace location to store a document in bulk processing.

This method is used by bulk document and bulk item workers.

Parameters:

Name Type Description Default
workspace dict

The payload for the workspace inside bulk document.

required
row Series

The current row in the data frame.

required
index int

The index of current row in the data frame.

required
replacements dict

The replacement configuration for placeholders.

required

Returns:

Name Type Description
int | None

int | None: Parent ID of folder or workspace to upload the document to or None in case of an error or insufficient payload information causing the workspace to be skipped.

bool bool

True, if success. False if a failure has happened.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
25816
25817
25818
25819
25820
25821
25822
25823
25824
25825
25826
25827
25828
25829
25830
25831
25832
25833
25834
25835
25836
25837
25838
25839
25840
25841
25842
25843
25844
25845
25846
25847
25848
25849
25850
25851
25852
25853
25854
25855
25856
25857
25858
25859
25860
25861
25862
25863
25864
25865
25866
25867
25868
25869
25870
25871
25872
25873
25874
25875
25876
25877
25878
25879
25880
25881
25882
25883
25884
25885
25886
25887
25888
25889
25890
25891
25892
25893
25894
25895
25896
25897
25898
25899
25900
25901
25902
25903
25904
25905
25906
25907
25908
25909
25910
25911
25912
25913
25914
25915
25916
25917
25918
25919
25920
25921
25922
25923
25924
25925
25926
25927
25928
25929
25930
25931
25932
25933
25934
25935
25936
25937
25938
25939
25940
25941
25942
25943
25944
25945
25946
25947
25948
25949
25950
25951
25952
25953
25954
25955
25956
25957
25958
25959
25960
25961
25962
25963
25964
25965
25966
25967
25968
25969
25970
25971
25972
25973
25974
25975
25976
25977
25978
25979
25980
25981
25982
25983
25984
25985
25986
25987
25988
25989
25990
25991
25992
25993
25994
25995
25996
25997
25998
25999
26000
26001
26002
26003
26004
26005
26006
26007
26008
26009
26010
26011
26012
26013
26014
26015
26016
26017
26018
26019
26020
26021
26022
26023
26024
26025
26026
26027
26028
26029
26030
26031
26032
26033
26034
26035
26036
26037
26038
26039
26040
26041
26042
26043
26044
26045
26046
26047
26048
26049
26050
26051
26052
26053
26054
26055
26056
26057
26058
26059
26060
26061
26062
26063
26064
26065
26066
26067
26068
26069
26070
26071
26072
26073
26074
26075
26076
26077
26078
26079
26080
26081
26082
26083
26084
26085
26086
26087
26088
26089
26090
26091
26092
26093
26094
26095
26096
26097
26098
26099
26100
26101
26102
26103
26104
26105
26106
26107
26108
26109
26110
26111
26112
26113
26114
26115
26116
26117
26118
26119
26120
26121
26122
26123
26124
26125
26126
26127
26128
26129
26130
26131
26132
26133
26134
26135
26136
26137
26138
26139
26140
26141
26142
26143
26144
26145
26146
26147
26148
26149
26150
26151
26152
26153
26154
26155
26156
26157
26158
26159
26160
26161
26162
26163
26164
26165
26166
26167
26168
26169
26170
26171
26172
26173
26174
26175
26176
26177
26178
26179
26180
26181
26182
26183
26184
26185
26186
26187
26188
26189
26190
26191
26192
26193
26194
26195
26196
26197
26198
26199
26200
26201
26202
26203
26204
26205
26206
26207
26208
26209
26210
26211
26212
26213
26214
26215
26216
26217
26218
26219
26220
26221
26222
26223
26224
26225
26226
26227
26228
26229
26230
26231
26232
26233
26234
26235
26236
26237
26238
26239
26240
26241
26242
26243
26244
26245
26246
26247
26248
26249
26250
26251
26252
26253
26254
26255
26256
26257
26258
26259
26260
26261
26262
26263
26264
26265
26266
26267
26268
26269
26270
26271
26272
26273
26274
26275
26276
26277
26278
26279
26280
26281
26282
26283
26284
26285
26286
26287
26288
26289
26290
26291
26292
26293
26294
26295
26296
26297
26298
26299
26300
26301
26302
26303
26304
26305
26306
26307
26308
26309
26310
26311
26312
26313
26314
26315
26316
26317
26318
26319
26320
26321
26322
26323
26324
26325
26326
26327
26328
26329
26330
26331
26332
26333
26334
26335
26336
26337
26338
26339
26340
26341
26342
26343
26344
26345
26346
26347
26348
26349
26350
26351
26352
26353
26354
26355
26356
26357
26358
26359
26360
26361
26362
26363
26364
26365
26366
26367
26368
26369
26370
26371
26372
26373
26374
26375
26376
26377
26378
26379
26380
26381
26382
26383
26384
26385
26386
26387
26388
26389
26390
26391
26392
26393
26394
26395
26396
26397
26398
26399
26400
26401
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_bulk_document_location")
def get_bulk_document_location(
    self,
    workspace: dict,
    row: pd.Series,
    index: int,
    replacements: dict,
) -> tuple[int | None, bool]:
    """Determine the workspace location to store a document in bulk processing.

    This method is used by bulk document and bulk item workers.

    Args:
        workspace (dict):
            The payload for the workspace inside bulk document.
        row (pd.Series):
            The current row in the data frame.
        index (int):
            The index of current row in the data frame.
        replacements (dict):
            The replacement configuration for placeholders.

    Returns:
        int | None:
            Parent ID of folder or workspace to upload the document to or None in case of
            an error or insufficient payload information causing the workspace to be skipped.
        bool:
            True, if success. False if a failure has happened.

    """

    success = True

    if "workspace_name" not in workspace:
        self.logger.error(
            "No workspace name field specified for document upload! Cannot upload document to this workspace...",
        )
        success = False
        return None, success
    workspace_name_field = workspace["workspace_name"]
    workspace_name_field_alt = workspace.get("workspace_name_alt")

    workspace_name = self.replace_bulk_placeholders(
        input_string=workspace_name_field,
        row=row,
        replacements=replacements,
    )
    # If we cannot get the workspace name from the
    # workspace_name_field we try the alternative name field
    # (if specified in payload via "workspace_name_alt"):
    if not workspace_name and workspace_name_field_alt:
        self.logger.debug(
            "Row -> %s does not have the data to resolve the placeholders in workspace name field -> %s! Trying alternative name field -> %s...",
            str(index),
            workspace_name_field,
            workspace_name_field_alt,
        )
        workspace_name = self.replace_bulk_placeholders(
            input_string=workspace_name_field_alt,
            row=row,
            replacements=replacements,
        )

    # it could be that the current data row does not have the
    # required fields to resolve the workspace name placeholders
    # then we skip uploading the document to this workspace
    # but still keep status as successful (don't set success = False)
    if not workspace_name:
        self.logger.warning(
            "Row -> %s does not have the required data to resolve workspace name field -> '%s' specified for document upload! Skipping document upload to this workspace...",
            str(index),
            workspace_name_field,
        )
        # We keep success = True as this is a data problem and not a config problem!
        return None, success

    # Cleanse the workspace name (allowed characters, maximum length):
    workspace_name = OTCS.cleanse_item_name(workspace_name)

    # Check if all data conditions to create the workspace are met
    conditions = workspace.get("conditions")
    if conditions:
        evaluated_condition = self.evaluate_conditions(
            conditions=conditions,
            row=row,
        )
        if not evaluated_condition:
            self.logger.info(
                "Workspace condition for row -> %s not met. Skipping row for document upload to workspace -> '%s'...",
                str(index),
                workspace_name,
            )
            # We keep success = True as this is a data problem and not a config problem!
            return None, success

    if "workspace_type" not in workspace:
        self.logger.error(
            "No workspace type specified for document upload! Skipping workspace -> '%s'...",
            workspace_name,
        )
        success = False
        return None, success
    workspace_type = workspace["workspace_type"]
    workspace_type = self.replace_bulk_placeholders(
        input_string=workspace_type,
        row=row,
        replacements=replacements,
    )
    workspace_data_source_name = workspace.get("data_source")
    # Try to find the workspace by name/synonym and type:
    (workspace_id, workspace_name) = self.process_bulk_workspaces_lookup(
        workspace_name=workspace_name,
        workspace_type=workspace_type,
        data_source_name=workspace_data_source_name,
    )
    if not workspace_id:
        self.logger.warning(
            "Cannot find workspace with name/synonym -> '%s' and type -> '%s'.",
            workspace_name,
            workspace_type,
        )
        success = False
        return None, success

    # If the workspace payload element has a "data_source" key,
    # then add all columns from the given data source to the bulk
    # document row to also support the lookup of values from the workspace
    # data source. These fields get a "lookup_" prefix to avoid name clashes.
    # the values must be specified with this "lookup_" prefix in the payload.
    # We CANNOT do this at the very beginning of the workspace loop as we
    # need the workspace_name to be properly resolved (incl. synonyms):
    if workspace_data_source_name:
        self.logger.info(
            "Workspace for bulk documents has a data source -> '%s' with lookup values. Adding them as row columns...",
            workspace_data_source_name,
        )
        workspace_data_source = next(
            (item for item in self._bulk_datasources if item["name"] == workspace_data_source_name),
            None,
        )
        # Read the synonym column and the name column from the data source payload item:
        workspace_data_source_name_column = workspace_data_source.get(
            "name_column",
            None,  # e.g. "Name"
        )

        if workspace_data_source_name_column:
            # Get additional data from workspace data source
            # for lookups. Synonyms are already resolved at
            # this point in time (workspace_name has been updated above
            # in case the initial workspace name wasn't found)
            lookup_row = self.lookup_data_source_value(
                data_source=workspace_data_source,
                lookup_column=workspace_data_source_name_column,
                lookup_value=workspace_name,
            )

            # Adding all values of the lookup row with the prefix lookup_ to the bulk documents row
            # for replacement of placeholders:
            if lookup_row is not None:
                for k, value in lookup_row.items():
                    row["lookup_" + k] = value

    # "workspace_folder" can be used if the payload contains
    # the path as a comma-separated string (top down)
    # otherwise "workspace_path" has precedence over "workspace_folder"
    workspace_folder = workspace.get("workspace_folder", "")
    workspace_path = workspace.get("workspace_path")

    if workspace_folder and not workspace_path:
        workspace_folder = self.replace_bulk_placeholders(
            input_string=workspace_folder,
            row=row,
            replacements=replacements,
        )
        workspace_path = (
            [item.strip() for item in workspace_folder.split(",")]
            if "," in workspace_folder
            else [workspace_folder]
        )

    if workspace_path:
        if isinstance(workspace_path, str):
            # if the path is actually a list in a string
            # we need to convert it to a python list in a safe way:
            try:
                workspace_path = self.replace_bulk_placeholders(
                    input_string=workspace_path,
                    index=None,  # None is VERY important as otherwise index=0 is the default and we only get the first element
                    row=row,
                    replacements=replacements,
                )
                workspace_path = literal_eval(workspace_path) if workspace_path else None
            except (SyntaxError, ValueError):
                self.logger.error(
                    "Cannot parse list-type folder path wrapped in string -> '%s'!",
                    workspace_path,
                )
                workspace_path = None
        elif isinstance(workspace_path, list):
            # We create a copy list to not modify original payload
            workspace_path = list(workspace_path)
            # Replace placeholders in payload for the path elements:
            # Note: workspace_path is a mutable data type that is changed in place!
            result_placeholders = self.replace_bulk_placeholders_list(
                input_list=workspace_path,
                row=row,
                replacements=replacements,
            )
            if not result_placeholders:
                workspace_path = None
        else:
            self.logger.warning("Unsupported data type for workspace path!")
            workspace_path = None

        if not workspace_path:
            # we put the document into the root of the workspace
            # if we couldn't determine a path inside the workspace:
            self.logger.info(
                "Workspace folder path for workspace -> '%s' of workspace type -> '%s' has no value. Using workspace root for document upload.",
                workspace_name,
                workspace_type,
            )
            parent_id = workspace_id
        else:
            # Check if the folder path does already exist and get the target folder at the end of the path:
            self.logger.info(
                "Check if path -> %s does already exist in workspace -> '%s' (%s) or otherwise create it...",
                str(workspace_path),
                workspace_name,
                workspace_id,
            )
            response = self._otcs_frontend.get_node_by_workspace_and_path(
                workspace_id=workspace_id,
                path=workspace_path,
                create_path=True,  # we want the path to be created if it doesn't exist
                show_error=False,
            )
            parent_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
            if not parent_id:
                self.logger.error(
                    "Failed to create path -> %s in workspace -> '%s' (%s)!",
                    str(workspace_path),
                    workspace_name,
                    workspace_id,
                )
                success = False
                return None, success
            else:
                self.logger.info(
                    "Using path -> %s inside workspace -> '%s' (%s). Node ID for target folder -> %s.",
                    str(workspace_path),
                    workspace_name,
                    workspace_id,
                    str(parent_id),
                )
    # end if workspace_path
    else:
        self.logger.info(
            "Workspace folder path for workspace -> '%s' of workspace type -> '%s' is not specified. Using workspace root for document upload.",
            workspace_name,
            workspace_type,
        )
        # we put the document into the root of the workspace:
        parent_id = workspace_id

    # Check if we have sub-workspaces configured.
    # If not, then we are done and can return the found
    # parent ID for the document:
    if "sub_workspace_type" not in workspace or "sub_workspace_name" not in workspace:
        self.logger.debug(
            "No sub workspace configured in payload. Return parent ID -> %s",
            parent_id,
        )
        return parent_id, success

    #
    # Determine (or create) the Sub workspace:
    #

    sub_workspace_type = workspace["sub_workspace_type"]
    sub_workspace_type = self.replace_bulk_placeholders(
        input_string=sub_workspace_type,
        row=row,
        replacements=replacements,
    )
    sub_workspace_name = workspace["sub_workspace_name"]
    sub_workspace_name = self.replace_bulk_placeholders(
        input_string=sub_workspace_name,
        row=row,
        replacements=replacements,
    )
    response = self._otcs_frontend.get_node_by_parent_and_name(
        name=sub_workspace_name,
        parent_id=parent_id,
    )
    # Check if the sub-workspaces does already exist:
    sub_workspace_id = self._otcs_frontend.get_result_value(
        response=response,
        key="id",
    )
    # Sub workspaces are dynamically created
    # during the processing of bulk documents...
    if not sub_workspace_id:
        self.logger.info(
            "Creating sub workspace -> '%s' of type -> '%s' and parent -> %s...",
            sub_workspace_name,
            sub_workspace_type,
            parent_id,
        )
        sub_workspace_template = workspace.get("sub_workspace_template", "")

        sub_workspace_template = self.replace_bulk_placeholders(
            input_string=sub_workspace_template,
            row=row,
            replacements=replacements,
        )
        # Now we try to determine the IDs for the sub-workspace type and template:
        (sub_workspace_type_id, sub_workspace_template_id) = self.determine_workspace_type_and_template_id(
            workspace_type_name=sub_workspace_type,
            workspace_template_name=sub_workspace_template,
        )
        # if either of the two couldn't be determined we cannot create the sub-workspace
        if not sub_workspace_type_id or not sub_workspace_template_id:
            self.logger.error(
                "Couldn't dertermine workspace template ID and workspace type ID of sub-workspace!",
            )
            success = False
            return None, success

        # Check if we have categories for the sub-workspace:
        if "categories" not in workspace:
            self.logger.debug(
                "Sub-Workspace payload has no category data! Will leave category attributes empty...",
            )
            sub_workspace_category_data = {}
        else:
            self.logger.debug(
                "Sub-Workspace payload has category data! Will prepare category data for workspace creation...",
            )
            worker_categories = self.process_bulk_categories(
                row=row,
                index=index,
                categories=workspace["categories"],
                replacements=replacements,
            )
            self.logger.debug(
                "Prepare category data for sub-workspace with template -> %s and parent -> %s",
                sub_workspace_template_id,
                parent_id,
            )
            sub_workspace_category_data = self.prepare_workspace_create_form(
                categories=worker_categories,
                template_id=sub_workspace_template_id,
                parent_workspace_node_id=parent_id,
            )
            if not sub_workspace_category_data:
                self.logger.error(
                    "Failed to prepare the category data for sub-workspace -> '%s'!",
                    sub_workspace_name,
                )
                success = False
                return None, success
        # Now we create the sub-workspace:
        response = self._otcs_frontend.create_workspace(
            workspace_template_id=sub_workspace_template_id,
            workspace_name=sub_workspace_name,
            workspace_description="",
            workspace_type=sub_workspace_type_id,
            category_data=sub_workspace_category_data,
            parent_id=parent_id,
            show_error=False,
        )
        if response is None:
            # Potential race condition: see if the sub-workspace has been created by a concurrent thread.
            # So we better check if the workspace is there even if the create_workspace() call delivered
            # a 'None' response:
            response = self._otcs_frontend.get_node_by_parent_and_name(
                parent_id=parent_id,
                name=sub_workspace_name,
            )
        sub_workspace_id = self._otcs_frontend.get_result_value(
            response=response,
            key="id",
        )
        if not sub_workspace_id:
            self.logger.error(
                "Failed to create sub-workspace -> '%s' with type ID -> %s under parent with ID -> %s!",
                sub_workspace_name,
                sub_workspace_type_id,
                parent_id,
            )
            parent_id = None
            success = False
            return None, success
        else:
            self.logger.info(
                "Successfully created sub-workspace -> '%s' (%s).",
                sub_workspace_name,
                sub_workspace_id,
            )

        # Create Business Relationship between workspace and sub-workspace:
        if workspace_id and sub_workspace_id:
            # Check if workspace relationship does already exist in OTCS
            # (this is an additional safety measure to avoid errors):
            response = self._otcs_frontend.get_workspace_relationships(
                workspace_id=workspace_id,
                related_workspace_name=sub_workspace_name,
            )
            current_workspace_relationships = self._otcs_frontend.exist_result_item(
                response=response,
                key="id",
                value=sub_workspace_id,
            )
            if current_workspace_relationships:
                self.logger.info(
                    "Workspace relationship between workspace -> '%s' (%s) and sub-workspace -> '%s' (%s) does already exist. Skipping...",
                    workspace_name,
                    workspace_id,
                    sub_workspace_name,
                    sub_workspace_id,
                )
            else:
                self.logger.info(
                    "Create workspace relationship %s -> %s...",
                    workspace_id,
                    sub_workspace_id,
                )
                response = self._otcs_frontend.create_workspace_relationship(
                    workspace_id=workspace_id,
                    related_workspace_id=sub_workspace_id,
                    show_error=False,  # we don't want to show an error because of race conditions handled below
                )
                if response:
                    self.logger.info(
                        "Successfully created workspace relationship between workspace ID -> %s and sub-workspace ID -> %s.",
                        workspace_id,
                        sub_workspace_id,
                    )
                else:
                    # Potential race condition: see if the workspace-2-sub-workspace relationship has been created by a concurrent thread.
                    # So we better check if the relationship is there even if the create_workspace_relationship() call delivered
                    # a 'None' response:
                    response = self._otcs_frontend.get_workspace_relationships(
                        workspace_id=workspace_id,
                        related_workspace_name=sub_workspace_name,
                    )
                    current_workspace_relationships = self._otcs_frontend.exist_result_item(
                        response=response,
                        key="id",
                        value=sub_workspace_id,
                    )
                    if not current_workspace_relationships:
                        self.logger.error(
                            "Failed to create workspace relationship between workspace ID -> %s and sub-workspace ID -> %s!",
                            workspace_id,
                            sub_workspace_id,
                        )
                    else:
                        self.logger.info(
                            "Successfully created workspace relationship between workspace ID -> %s and sub-workspace ID -> %s.",
                            workspace_id,
                            sub_workspace_id,
                        )

    # end if sub-workspace does not exist
    else:
        self.logger.info(
            "Using existing sub workspace -> '%s' (%s) of type -> '%s'...",
            sub_workspace_name,
            str(sub_workspace_id),
            sub_workspace_type,
        )

    #
    # Get the target folder in the sub-workspace by the provided sub workspace path
    #

    # "sub_workspace_folder" can be used if the payload contains
    # the path as a comma-separated string (top down)
    sub_workspace_folder = workspace.get("sub_workspace_folder", "")
    sub_workspace_path = workspace.get("sub_workspace_path")

    if not sub_workspace_path and sub_workspace_folder:
        sub_workspace_folder = self.replace_bulk_placeholders(
            input_string=sub_workspace_folder,
            row=row,
            replacements=replacements,
        )
        sub_workspace_path = (
            [item.strip() for item in sub_workspace_folder.split(",")]
            if "," in sub_workspace_folder
            else [sub_workspace_folder]
        )

    if sub_workspace_path:
        if isinstance(sub_workspace_path, str):
            # if the path is actually a list in a string
            # we need to convert it to a python list in a safe way:
            try:
                sub_workspace_path = self.replace_bulk_placeholders(
                    input_string=sub_workspace_path,
                    index=None,  # None is VERY important as otherwise index=0 is the default and we only get the first element
                    row=row,
                    replacements=replacements,
                )
                sub_workspace_path = literal_eval(sub_workspace_path) if sub_workspace_path else None
            except (SyntaxError, ValueError):
                self.logger.error(
                    "Cannot parse list-type folder path wrapped in string -> '%s'!",
                    sub_workspace_path,
                )
                sub_workspace_path = None
        elif isinstance(sub_workspace_path, list):
            # We create a copy list to not modify original payload
            sub_workspace_path = list(sub_workspace_path)
            # Replace placeholders in payload for the path elements:
            # Note: sub_workspace_path is a mutable data type that is changed in place!
            result_placeholders = self.replace_bulk_placeholders_list(
                input_list=sub_workspace_path,
                row=row,
                replacements=replacements,
            )
            if not result_placeholders:
                sub_workspace_path = None
        else:
            self.logger.warning("Unsupported data type for sub workspace path!")
            sub_workspace_path = None

        if not sub_workspace_path:
            self.logger.warning(
                "Sub-Workspace folder path for sub workspace -> '%s' of sub workspace type -> '%s' cannot be resolved (placeholder issue). Using sub workspace root for document upload.",
                sub_workspace_name,
                sub_workspace_type,
            )
            # we put the document into the root of the workspace:
            parent_id = sub_workspace_id
        else:
            # Check if the folder path does already exist and get the target folder at the end of the path:
            self.logger.info(
                "Check if path -> %s does already exist in workspace -> '%s' (%s)... (otherwise create it)",
                str(sub_workspace_path),
                sub_workspace_name,
                sub_workspace_id,
            )
            response = self._otcs_frontend.get_node_by_workspace_and_path(
                workspace_id=sub_workspace_id,
                path=sub_workspace_path,
                create_path=True,  # we want the path to be created if it doesn't exist
                show_error=False,
            )
            parent_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
            if not parent_id:
                self.logger.error(
                    "Failed to create path -> %s in sub workspace -> '%s' (%s)!",
                    str(sub_workspace_path),
                    sub_workspace_name,
                    sub_workspace_id,
                )
                success = False
                return None, success
            else:
                self.logger.info(
                    "Successfully created path -> %s in sub-workspace -> '%s' (%s). Node ID for target folder -> %s.",
                    str(sub_workspace_path),
                    sub_workspace_name,
                    sub_workspace_id,
                    str(parent_id),
                )
    else:
        self.logger.info(
            "Folder path inside sub-workspace -> '%s' with sub workspace type -> '%s' is not specified. Using root of sub-workspace for document upload.",
            sub_workspace_name,
            sub_workspace_type,
        )
        # we put the document into the root of the workspace:
        parent_id = sub_workspace_id

    return parent_id, success

get_bulk_workspace_relationship_endpoint(bulk_workspace_relationship, row, index, endpoint, replacements=None, nickname_additional_regex_list=None, show_error=True)

Determine the node ID of the workspace that is one of the endpoints of the workspace relationship (either 'from' or 'to').

Parameters:

Name Type Description Default
bulk_workspace_relationship dict

The payload element of the bulk workspace relationship

required
row Series

The data frame row.

required
index int

The index of the data frame row.

required
endpoint str

The name of the endpoint - either "from" or "to".

required
replacements dict | None

Replacements for placeholders. Defaults to None.

None
nickname_additional_regex_list list | None

Additional regex replacements for nicknames. Defaults to None.

None
show_error bool

Whether or not to log an error. If False just a warning is logged.

True

Returns:

Type Description
tuple[int | None, str | None]

tuple[int | None, str | None]: The workspace ID and the looked up workspace name.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_bulk_workspace_relationship_endpoint")
def get_bulk_workspace_relationship_endpoint(
    self,
    bulk_workspace_relationship: dict,
    row: pd.Series,
    index: int,
    endpoint: str,
    replacements: dict | None = None,
    nickname_additional_regex_list: list | None = None,
    show_error: bool = True,
) -> tuple[int | None, str | None]:
    """Determine the node ID of the workspace that is one of the endpoints of the workspace relationship (either 'from' or 'to').

    Args:
        bulk_workspace_relationship (dict):
            The payload element of the bulk workspace relationship
        row (pd.Series):
            The data frame row.
        index (int):
            The index of the data frame row.
        endpoint (str):
            The name of the endpoint - either "from" or "to".
        replacements (dict | None, optional):
            Replacements for placeholders. Defaults to None.
        nickname_additional_regex_list (list | None, optional):
            Additional regex replacements for nicknames. Defaults to None.
        show_error (bool, optional):
            Whether or not to log an error. If False just a warning is logged.

    Returns:
        tuple[int | None, str | None]:
            The workspace ID and the looked up workspace name.

    """

    if endpoint not in ["from", "to"]:
        self.logger.error("The endpoint must be either 'from' or 'to'!")
        return (None, None)

    # Determine the workspace nickname field:
    workspace_nickname_field = bulk_workspace_relationship.get(
        "{}_workspace".format(endpoint),
        None,
    )
    workspace_nickname = self.replace_bulk_placeholders(
        input_string=workspace_nickname_field,
        row=row,
        replacements=replacements,
        additional_regex_list=nickname_additional_regex_list,
    )
    if not workspace_nickname:
        self.logger.warning(
            "Row -> %s does not have the required data to resolve -> '%s' for the workspace nickname (endpoint -> '%s')!",
            str(index),
            workspace_nickname_field,
            endpoint,
        )
        return (None, None)

    # Get the workspace type if specified:
    workspace_type = bulk_workspace_relationship.get(
        "{}_workspace_type".format(endpoint),
        None,
    )

    # Get the workspace name if specified:
    workspace_name_field = bulk_workspace_relationship.get(
        "{}_workspace_name".format(endpoint),
        None,
    )
    if workspace_name_field:
        workspace_name = self.replace_bulk_placeholders(
            input_string=workspace_name_field,
            row=row,
            replacements=replacements,
        )
        if not workspace_name:
            self.logger.warning(
                "Row -> %s does not have the required data to resolve -> '%s' for the workspace name (endpoint -> '%s')!",
                str(index),
                workspace_name_field,
                endpoint,
            )
            return (None, None)
    else:
        workspace_name = None

    # Get the workspace data source if specified:
    workspace_data_source = bulk_workspace_relationship.get(
        "{}_workspace_data_source".format(endpoint),
        None,
    )

    # Based on the given information, we now try to determine
    # the name and the ID of the workspace that is the endpoint
    # for the workspace relationship:
    (workspace_id, workspace_name) = self.process_bulk_workspaces_lookup(
        workspace_nickname=workspace_nickname,
        workspace_name=workspace_name,
        workspace_type=workspace_type,
        data_source_name=workspace_data_source,
        show_error=show_error,
    )

    if not workspace_id:
        self.logger.warning(
            "Cannot find workspace to establish relationship (endpoint -> '%s')%s%s%s%s",
            endpoint,
            (", nickname -> '{}'".format(workspace_nickname) if workspace_nickname else ""),
            (", workspace name -> '{}'".format(workspace_name) if workspace_name else ""),
            (", workspace type -> '{}'".format(workspace_type) if workspace_type else ""),
            (", data source -> '{}'".format(workspace_data_source) if workspace_data_source else ""),
        )
        return (None, None)

    # See if a sub-workspace is configured:
    sub_workspace_name_field = bulk_workspace_relationship.get(
        "{}_sub_workspace_name".format(endpoint),
        None,
    )
    # If no sub-workspace is configured we can already
    # return the resulting workspace ID and name here:
    if not sub_workspace_name_field:
        return (workspace_id, workspace_name)

    # Otherwise we are no processing the sub-workspaces to return
    # its ID instead:
    sub_workspace_name = self.replace_bulk_placeholders(
        input_string=sub_workspace_name_field,
        row=row,
        replacements=replacements,
    )
    if not sub_workspace_name:
        self.logger.warning(
            "Row -> %s does not have the required data to resolve -> %s for the sub-workspace name (endpoint -> '%s')!",
            str(index),
            sub_workspace_name_field,
            endpoint,
        )
        return (None, None)

    # See if a sub-workspace is in a sub-path of the main workspace:
    sub_workspace_path = bulk_workspace_relationship.get(
        "{}_sub_workspace_path".format(endpoint),
        None,
    )
    if sub_workspace_path:
        # sub_workspace_path is a mutable that is changed in place!
        result = self.replace_bulk_placeholders_list(
            input_list=sub_workspace_path,
            row=row,
            replacements=replacements,
        )
        if not result:
            self.logger.warning(
                "Row -> %s does not have the required data to resolve -> %s for the sub-workspace path (endpoint -> '%s')!",
                str(index),
                sub_workspace_path,
                endpoint,
            )
            return None

        self.logger.info(
            "Endpoint has a sub-workspace -> '%s' configured. Try to find the sub-workspace in workspace path -> %s...",
            sub_workspace_name,
            sub_workspace_path,
        )

        # We now want to retrieve the folder in the main workspace that
        # includes the sub-workspace:
        response = self._otcs_frontend.get_node_by_workspace_and_path(
            workspace_id=workspace_id,
            path=sub_workspace_path,
            create_path=False,  # we don't want the path to be created if it doesn't exist
            show_error=True,
        )
        parent_id = self._otcs_frontend.get_result_value(
            response=response,
            key="id",
        )
        if not parent_id:
            self.logger.error(
                "Failed to find path -> %s in workspace -> '%s' (%s)!",
                str(sub_workspace_path),
                workspace_name,
                workspace_id,
            )
            return (None, None)
    # end if sub_workspace_path_field
    else:
        # the sub-workspace is immediately under the main workspace:
        parent_id = workspace_id

    response = self._otcs_frontend.get_node_by_parent_and_name(
        parent_id=parent_id,
        name=sub_workspace_name,
        show_error=True,
    )
    sub_workspace_id = self._otcs_frontend.get_result_value(
        response=response,
        key="id",
    )

    return (sub_workspace_id, sub_workspace_name)

get_business_object_properties(bo_type_name)

Get a dictionary with all property mapping of a business object type.

We contruct this dictionary from the two lists for the given business object types (property mapping and property group mappings) These two lists have been created before by process_business_object_types()

This method is used for creation of business objects in Salesforce.

Parameters:

Name Type Description Default
bo_type_name str

The name of the business object type

required

Returns:

Type Description
dict | None

dict | None: A dictionary with keys that are either the attribute name or a key that is contructed like this: set name + "-" + attribute name. This allows for an easy lookup in methods that have access to the category data of business workspaces.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_business_object_properties")
def get_business_object_properties(self, bo_type_name: str) -> dict | None:
    """Get a dictionary with all property mapping of a business object type.

    We contruct this dictionary from the two lists for the given
    business object types (property mapping and property group mappings)
    These two lists have been created before by process_business_object_types()

    This method is used for creation of business objects in Salesforce.

    Args:
        bo_type_name (str):
            The name of the business object type

    Returns:
        dict | None:
            A dictionary with keys that are either the attribute name or
            a key that is contructed like this: set name + "-" + attribute name.
            This allows for an easy lookup in methods that have access to
            the category data of business workspaces.

    """

    if not self._business_object_types:
        self.logger.warning(
            "List of business object types is empty / not initialized! Cannot lookup type -> '%s'!",
            bo_type_name,
        )
        return None

    # Find the matching business object type:
    business_object_type = next(
        (item for item in self._business_object_types if item["name"] == bo_type_name),
        None,
    )
    if not business_object_type:
        self.logger.warning(
            "Cannot find business object type -> '%s'!",
            bo_type_name,
        )
        return None

    business_properties = business_object_type.get("business_properties")
    business_property_groups = business_object_type.get("business_property_groups")

    lookup_dict = {}

    # 25.3 uses a new REST API to retreive the business object type details.
    # Pre 25.3 we use extractions from the transport package to get this information
    # See process_business_object_type() method.
    otcs_version = float(self._otcs.get_server_version())
    if otcs_version < 25.3:
        att_key = "@attributeName"
        set_key = "@setName"
    else:
        att_key = "att_name"
        set_key = "set_name"

    for mapping in business_properties:
        attribute_name = mapping.get(att_key)
        lookup_dict[attribute_name] = mapping

    for mapping in business_property_groups:
        set_name = mapping.get(set_key)
        attribute_name = mapping.get(att_key)
        lookup_dict[set_name + "-" + attribute_name] = mapping

    return lookup_dict

get_groups()

Get all groups in payload.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def get_groups(self) -> list:
    """Get all groups in payload."""
    return self._groups

get_guidewire_business_object(external_system, object_type, search_field, search_value)

Get the Guidewire ID (str) of an Guidewire object by querying the Guidewire API.

Parameters:

Name Type Description Default
external_system dict

Payload of the external System. Required if Guidewire python object needs to be re-initialized.

required
object_type str

The business object type (like "Claim", "Policy", "Account").

required
search_field str

Search field to find business object in external system.

required
search_value str

Search value to find business object in external system.

required

Returns:

Type Description
str | None

str | None: The technical ID of the business object in Guidewire. None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_guidewire_business_object")
def get_guidewire_business_object(
    self,
    external_system: dict,
    object_type: str,
    search_field: str,
    search_value: str,
) -> str | None:
    """Get the Guidewire ID (str) of an Guidewire object by querying the Guidewire API.

    Args:
        external_system (dict):
            Payload of the external System. Required if Guidewire python object
            needs to be re-initialized.
        object_type (str):
            The business object type (like "Claim", "Policy", "Account").
        search_field (str):
            Search field to find business object in external system.
        search_value (str):
            Search value to find business object in external system.

    Returns:
        str | None:
            The technical ID of the business object in Guidewire. None in case of an error.

    """

    if object_type in ["Account", "Policy", "account", "policy", "gw.account", "gw.policy"]:
        if not self._guidewire_policy_center:
            self._guidewire_policy_center = self.init_guidewire(guidewire_external_system=external_system)
        guidewire_object = self._guidewire_policy_center
    elif object_type in ["Claim", "claim", "gw.claim"]:
        if not self._guidewire_claims_center:
            self._guidewire_claims_center = self.init_guidewire(guidewire_external_system=external_system)
        guidewire_object = self._guidewire_claims_center
    else:
        self.logger.error("Not supported Guidewire object type -> '%s'", object_type)
        return None

    if not guidewire_object:
        self.logger.error(
            "Guidewire connection not initialized! Cannot connect to Guidewire API!",
        )
        return None

    self.logger.debug(
        "Workspaces is connected to Guidewire and we need to lookup the business object ID...",
    )
    guidewire_token = guidewire_object.authenticate()
    if not guidewire_token:
        self.logger.error("Failed to authenticate with Guidewire!")
        return None

    match object_type:
        case "Account" | "account" | "gw.account":
            response = guidewire_object.search_account(
                attributes={search_field: search_value},
            )
            bo_id = guidewire_object.get_result_value(response=response, key="id")
        case "Policy" | "policy" | "gw.policy":
            response = guidewire_object.search_policy(
                attributes={search_field: search_value},
            )
            bo_id = guidewire_object.get_result_value(response=response, key="policyId")
        case _:
            self.logger.warning("Currently we only support lookup of 'Account' and 'Policy' objects in Guidewire!")
            return None

    num_of_bos = int(response.get("count", 0)) if (response is not None and "count" in response) else 0
    if num_of_bos > 1:
        self.logger.warning(
            "Guidewire lookup delivered %s business objects of type -> '%s'. Picking the first one with ID -> %s.",
            str(num_of_bos),
            str(object_type),
            bo_id,
        )
    if not bo_id:
        self.logger.warning(
            "Business object of type -> '%s' with '%s' = '%s' does not exist in Guidewire!",
            object_type,
            search_field,
            search_value,
        )
    else:  # BO found
        self.logger.info(
            "Retrieved ID -> %s for Guidewire object type -> '%s' (looking up -> '%s' in field -> '%s').",
            bo_id,
            object_type,
            search_value,
            search_field,
        )

    return bo_id

get_k8s()

Get the K8s object.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def get_k8s(self) -> object:
    """Get the K8s object."""
    return self._k8s

get_m365()

Get the M365 object.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def get_m365(self) -> object:
    """Get the M365 object."""
    return self._m365

get_otcs_backend()

Get OTCS Backend object.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def get_otcs_backend(self) -> object:
    """Get OTCS Backend object."""
    return self._otcs_backend

get_otcs_frontend()

Get OTCS Frontend oject.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def get_otcs_frontend(self) -> object:
    """Get OTCS Frontend oject."""
    return self._otcs_frontend

get_otds()

Get the OTDS object.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def get_otds(self) -> object:
    """Get the OTDS object."""
    return self._otds

get_payload(drop_bulk_datasources_data=False)

Get the Payload as reference.

Optional a copy of the payload can be delivered the does not include the "data" value of "bulkDatasource" (its content can be HUGE and many times we don't want it).

Parameters:

Name Type Description Default
drop_bulk_datasources_data bool

If True, returns a !copy! of the payload without the bulkDatasources "data" in it. Defaults to False.

False

Returns:

Name Type Description
dict dict

Returns the payload

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_payload")
def get_payload(self, drop_bulk_datasources_data: bool = False) -> dict:
    """Get the Payload as reference.

    Optional a copy of the payload can be delivered the does not include the
    "data" value of "bulkDatasource" (its content can be HUGE and many times we don't
    want it).

    Args:
        drop_bulk_datasources_data (bool, optional):
            If True, returns a !copy! of the payload without the bulkDatasources
            "data" in it. Defaults to False.

    Returns:
        dict: Returns the payload

    """

    if drop_bulk_datasources_data and "bulkDatasources" in self._payload:
        # Create a Copy of the payload, but without the bulkDatasources
        # using deepcopy would require significantly more memory, just to drop it directly after
        payload = {k: v for k, v in self._payload.items() if k != "bulkDatasources"}

        payload["bulkDatasources"] = [
            {k: v for k, v in data.items() if k != "data"} for data in self._payload.get("bulkDatasources", {})
        ]
        return payload

    return self._payload

get_payload_section(payload_section_name)

Get a specific section of the payload based on its name.

The section is delivered as a list of settings. It deliveres an empty list if this payload section is disabled by the corresponding payload switch (this is read from the payloadSections dictionary of the payload)

Parameters:

Name Type Description Default
payload_section_name str

The name of the dict element in the payload structure.

required

Returns:

Name Type Description
list list

The section of the payload as a Python list. Empty list if section does not exist or section is disabled by the corresponding payload switch.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def get_payload_section(self, payload_section_name: str) -> list:
    """Get a specific section of the payload based on its name.

    The section is delivered as a list of settings.
    It deliveres an empty list if this payload section is disabled by the corresponding
    payload switch (this is read from the payloadSections dictionary of the payload)

    Args:
        payload_section_name (str):
            The name of the dict element in the payload structure.

    Returns:
        list:
            The section of the payload as a Python list. Empty list if section does not exist
            or section is disabled by the corresponding payload switch.

    """

    if not isinstance(self._payload, dict):
        return []

    # if the secton is not in the payload we return an empty list:
    if not self._payload.get(payload_section_name):
        return []

    # Return an empty list if the payload section does not exist or is disabled:
    sections = self._payload.get("payloadSections")
    if sections:
        section = next(
            (item for item in sections if item["name"] == payload_section_name),
            None,
        )
        if not section or not section.get("enabled", False):
            return []

    return self._payload[payload_section_name]

get_salesforce_business_object(workspace, object_type, search_field, search_value)

Get the Salesforce ID (str) of an Salesforce object by querying the Salesforce API.

Parameters:

Name Type Description Default
workspace dict

The workspace payload element.

required
object_type str

The business object type.

required
search_field str

Search field to find business object in external system.

required
search_value str

Search value to find business object in external system.

required

Returns:

Type Description
str | None

str | None: The technical ID of the business object in Salesforce.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_salesforce_business_object")
def get_salesforce_business_object(
    self,
    workspace: dict,
    object_type: str,
    search_field: str,
    search_value: str,
) -> str | None:
    """Get the Salesforce ID (str) of an Salesforce object by querying the Salesforce API.

    Args:
        workspace (dict):
            The workspace payload element.
        object_type (str):
            The business object type.
        search_field (str):
            Search field to find business object in external system.
        search_value (str):
            Search value to find business object in external system.

    Returns:
        str | None:
            The technical ID of the business object in Salesforce.

    """

    if not self._salesforce:
        self.logger.error(
            "Salesforce connection not initialized! Cannot connect to Salesforce API!",
        )
        return None

    self.logger.debug(
        "Workspaces is connected to Salesforce and we need to lookup the business object ID...",
    )
    salesforce_token = self._salesforce.authenticate()
    if not salesforce_token:
        self.logger.error("Failed to authenticate with Salesforce!")
        return None

    response = self._salesforce.get_object(
        object_type=object_type,
        search_field=search_field,
        search_value=search_value,
        result_fields=["Id"],
    )
    bo_id = self._salesforce.get_result_value(response=response, key="Id")
    num_of_bos = int(response.get("totalSize", 0)) if (response is not None and "totalSize" in response) else 0
    if num_of_bos > 1:
        self.logger.warning(
            "Salesforce lookup delivered %s business objects of type -> '%s'! Picking the first one with ID -> %s...",
            str(num_of_bos),
            str(object_type),
            bo_id,
        )
    if not bo_id:
        self.logger.warning(
            "Business object of type -> '%s' with '%s' = '%s' does not exist in Salesforce!",
            object_type,
            search_field,
            search_value,
        )
        self.logger.info(
            "We try to create the Salesforce object of type -> '%s'...",
            object_type,
        )

        # Get a helper dict to quickly lookup Salesforce properties
        # for given set + attribute name:
        property_lookup = self.get_business_object_properties(
            bo_type_name=object_type,
        )
        # In case we couldn't find properties for the given Business Object Type
        # we bail out...
        if not property_lookup:
            self.logger.warning(
                "Cannot create Salesforce object - no business object properties found!",
            )
            return None

        categories = workspace.get("categories", [])
        parameter_dict = {}
        # We process all category entries in workspace payload
        # and see if we have a matching mapping to a business property
        # in the BO Type definition:
        for category in categories:
            # generate the lookup key:
            key = ""
            if "set" in category:
                key += category["set"] + "-"
            key += category.get("attribute")
            # get the attribute value:
            value = category.get("value")
            # lookup the mapping
            mapping = property_lookup.get(key, None)
            # Check if we have a mapping:
            if mapping:
                property_name = mapping.get("@propertyName", None)
                self.logger.debug(
                    "Found business property -> '%s' for attribute -> '%s'",
                    property_name,
                    category.get("attribute"),
                )
                parameter_dict[property_name] = value
            else:
                self.logger.debug(
                    "Attribute -> '%s' (key -> %s) does not have a mapped business property.",
                    category.get("attribute"),
                    key,
                )

        if not parameter_dict:
            self.logger.warning(
                "Cannot create Salesforce object of type -> '%s' - no parameters found!",
                object_type,
            )
            return None

        self.logger.info(
            "Create Salesforce object of type -> '%s' with parameters -> %s...",
            object_type,
            str(parameter_dict),
        )
        #
        # Now we try to create the Salesforce object
        #
        response = self._salesforce.add_object(
            object_type=object_type,
            **parameter_dict,
        )
        bo_id = self._salesforce.get_result_value(response=response, key="id")
        if bo_id:
            self.logger.info(
                "Successfully created Salesforce business object with ID -> %s of type -> '%s'.",
                bo_id,
                object_type,
            )
        else:
            self.logger.error(
                "Failed to create Salesforce business object of type -> '%s'!",
                object_type,
            )
    else:  # BO found
        self.logger.debug(
            "Retrieved ID -> %s for Salesforce object type -> '%s' (looking up -> '%s' in field -> '%s')",
            bo_id,
            object_type,
            search_value,
            search_field,
        )

    return bo_id

get_status_file(payload_section_name, payload_specific=True, prefix='success_')

Get the status file and read it into a list of dictionaries.

Parameters:

Name Type Description Default
payload_section_name str

The name of the payload section. This is used to construct the file name.

required
payload_specific bool

Whether or not the success should be specific for each payload file or if success is "global" - like for the deletion of the existing M365 teams (which we don't want to execute per payload file)

True
prefix str

The prefix of the file. Typically, either "success_" or "failure_".

'success_'

Returns:

Name Type Description
list list | None

Content of the status file as a list of dictionaries or None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_status_file")
def get_status_file(
    self,
    payload_section_name: str,
    payload_specific: bool = True,
    prefix: str = "success_",
) -> list | None:
    """Get the status file and read it into a list of dictionaries.

    Args:
        payload_section_name (str):
            The name of the payload section. This
            is used to construct the file name.
        payload_specific (bool, optional):
            Whether or not the success should be specific for
            each payload file or if success is "global" - like for the deletion
            of the existing M365 teams (which we don't want to execute per
            payload file)
        prefix (str, optional):
            The prefix of the file. Typically, either "success_" or "failure_".

    Returns:
        list:
            Content of the status file as a list of dictionaries or None in case of an error.

    """

    self.logger.info(
        "Get the status file of the payload section -> '%s'...",
        payload_section_name,
    )

    while not self._otcs.is_ready():
        self.logger.info(
            "OTCS is not ready. Cannot read status file for -> '%s' from OTCS. Waiting 30 seconds and retry...",
            payload_section_name,
        )
        time.sleep(30)

    response = self._otcs.get_node_by_volume_and_path(
        volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
    )  # read from Personal Workspace of Admin
    source_folder_id = self._otcs.get_result_value(response=response, key="id")
    if not source_folder_id:
        source_folder_id = 2004  # use Personal Workspace ID of Admin as fallback

    file_name = self.get_status_file_name(
        payload_section_name=payload_section_name,
        payload_specific=payload_specific,
        prefix=prefix,
    )

    status_document = self._otcs.get_node_by_parent_and_name(
        parent_id=int(source_folder_id),
        name=file_name,
        show_error=True,
    )
    status_file_id = self._otcs.get_result_value(response=status_document, key="id")
    if not status_file_id:
        self.logger.error("Cannot find status file -> '%s'!", file_name)
        return None

    return self._otcs.get_json_document(node_id=status_file_id)

get_status_file_name(payload_section_name, payload_specific=True, prefix='success_')

Construct the name of the status file.

Parameters:

Name Type Description Default
payload_section_name str

The name of the payload section. This is used to construct the file name

required
payload_specific bool

Whether or not the success should be specific for each payload file or if success is "global" - like for the deletion of the existing M365 teams (which we don't want to execute per payload file)

True
prefix str

The prefix of the file name. Typically, either "success_" or "failure_".

'success_'

Returns:

Name Type Description
str str

The constructed name of the payload section file.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="get_status_file_name")
def get_status_file_name(
    self,
    payload_section_name: str,
    payload_specific: bool = True,
    prefix: str = "success_",
) -> str:
    """Construct the name of the status file.

    Args:
        payload_section_name (str):
            The name of the payload section. This
            is used to construct the file name
        payload_specific (bool, optional):
            Whether or not the success should be specific for
            each payload file or if success is "global" - like for the deletion
            of the existing M365 teams (which we don't want to execute per
            payload file)
        prefix (str, optional):
            The prefix of the file name. Typically, either "success_" or "failure_".

    Returns:
        str:
            The constructed name of the payload section file.

    """

    # Some sections are actually not payload specific like teamsM365Cleanup
    # we don't want external payload runs to re-apply this processing:
    if payload_specific:
        file_name = os.path.basename(self._payload_source)  # remove directories
        # Split once at the first occurance of a dot
        # as the _payload_source may have multiple suffixes
        # such as .yml.gz.b64:
        file_name = file_name.split(".", 1)[0]
        file_name = prefix + file_name + "_" + payload_section_name + ".json"
    else:
        file_name = prefix + payload_section_name + ".json"

    return file_name

get_users()

Get all users in payload.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def get_users(self) -> list:
    """Get all users in payload."""
    return self._users

get_workspaces()

Get all workspaces in payload.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def get_workspaces(self) -> list:
    """Get all workspaces in payload."""
    return self._workspaces

init_guidewire(guidewire_external_system)

Initialize Guidewire object for workspace creation.

This is needed to query Guidewire REST API to lookup IDs of Guidewire objects.

Parameters:

Name Type Description Default
guidewire_external_system dict

The payload of the Guidewire external system created before.

required

Returns:

Type Description
Guidewire | None

Guidewire | None: Guidewire object or None in case an error occured.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_guidewire")
def init_guidewire(self, guidewire_external_system: dict) -> Guidewire | None:
    """Initialize Guidewire object for workspace creation.

    This is needed to query Guidewire REST API to lookup IDs of Guidewire objects.

    Args:
        guidewire_external_system (dict):
            The payload of the Guidewire external system created before.

    Returns:
        Guidewire | None:
            Guidewire object or None in case an error occured.

    """

    if not guidewire_external_system:
        return None

    system_name = guidewire_external_system["external_system_name"]
    username = guidewire_external_system["username"]
    password = guidewire_external_system["password"]
    base_url = guidewire_external_system["base_url"]
    as_url = guidewire_external_system["as_url"]
    auth_type = guidewire_external_system.get("auth_type", "basic").lower()

    self.logger.info("Connection parameters for %s:", system_name)
    self.logger.info("Guidewire base URL        = %s", base_url)
    self.logger.info("Guidewire application URL = %s", as_url)
    self.logger.info("Guidewire username        = %s", username)
    self.logger.debug("Guidewire password        = %s", password)
    guidewire_object = Guidewire(
        base_url=base_url,
        as_url=as_url,
        auth_type=auth_type,
        username=username,
        password=password,
        logger=self.logger,
    )

    return guidewire_object

init_payload()

Read the YAML or Terraform HCL payload file.

Returns:

Type Description
dict | None

dict | None: The payload as a Python dict. Elements are the different payload sections. None in case the file couldn't be found or read.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_payload")
def init_payload(self) -> dict | None:
    """Read the YAML or Terraform HCL payload file.

    Args:
        None

    Returns:
        dict | None:
            The payload as a Python dict. Elements are the different payload sections.
            None in case the file couldn't be found or read.

    """

    self._payload = load_payload(self._payload_source, self.logger)
    if not self._payload:
        return None

    self._payload_sections = self._payload.get("payloadSections", None)
    self._payload_options = self._payload.get("payloadOptions", None)

    # Retrieve all the payload sections and store them in lists
    # (these are object variables that we want to initialize
    # even if payload is empty):
    self._webhooks = self.get_payload_section("webHooks")
    self._webhooks_post = self.get_payload_section("webHooksPost")
    self._resources = self.get_payload_section("resources")
    self._partitions = self.get_payload_section("partitions")
    self._licenses = self.get_payload_section("licenses")
    self._synchronized_partitions = self.get_payload_section(
        "synchronizedPartitions",
    )
    self._oauth_clients = self.get_payload_section("oauthClients")
    self._application_roles = self.get_payload_section("applicationRoles")
    self._auth_handlers = self.get_payload_section("authHandlers")
    self._trusted_sites = self.get_payload_section("trustedSites")
    self._system_attributes = self.get_payload_section("systemAttributes")
    self._docgen_settings = self.get_payload_section("docgenSettings")
    self._groups = self.get_payload_section("groups")
    self._users = self.get_payload_section("users")
    if self._users:
        # Check if multiple user instances should be created
        self.init_payload_user_instances()
    self._admin_settings = self.get_payload_section("adminSettings")
    self._admin_settings_post = self.get_payload_section("adminSettingsPost")
    self._exec_pod_commands = self.get_payload_section("execPodCommands")
    self._exec_commands = self.get_payload_section("execCommands")
    self._kubernetes = self.get_payload_section("kubernetes")
    self._exec_database_commands = self.get_payload_section("execDatabaseCommands")
    self._external_systems = self.get_payload_section("externalSystems")
    self._transport_packages = self.get_payload_section("transportPackages")
    self._content_transport_packages = self.get_payload_section(
        "contentTransportPackages",
    )
    self._transport_packages_post = self.get_payload_section(
        "transportPackagesPost",
    )
    self._workspace_templates = self.get_payload_section("workspaceTemplates")
    self._workspaces = self.get_payload_section("workspaces")
    self._bulk_datasources = self.get_payload_section("bulkDatasources")
    self._bulk_workspaces = self.get_payload_section("bulkWorkspaces")
    self._bulk_workspace_relationships = self.get_payload_section(
        "bulkWorkspaceRelationships",
    )
    self._bulk_documents = self.get_payload_section("bulkDocuments")
    self._bulk_items = self.get_payload_section("bulkItems")
    self._bulk_classifications = self.get_payload_section("bulkClassifications")
    self._sap_rfcs = self.get_payload_section("sapRFCs")
    self._sap_rfcs_post = self.get_payload_section("sapRFCsPost")
    self._web_reports = self.get_payload_section("webReports")
    self._web_reports_post = self.get_payload_section("webReportsPost")
    self._cs_applications = self.get_payload_section("csApplications")
    self._additional_group_members = self.get_payload_section(
        "additionalGroupMemberships",
    )
    self._additional_access_role_members = self.get_payload_section(
        "additionalAccessRoleMemberships",
    )
    self._additional_application_role_assignments = self.get_payload_section(
        "additionalApplicationRoleAssignments",
    )
    self._renamings = self.get_payload_section("renamings")
    self._items = self.get_payload_section("items")
    self._items_post = self.get_payload_section("itemsPost")
    self._permissions = self.get_payload_section("permissions")
    self._permissions_post = self.get_payload_section("permissionsPost")
    self._workspace_permissions = self.get_payload_section("workspacePermissions")
    self._assignments = self.get_payload_section("assignments")
    self._security_clearances = self.get_payload_section("securityClearances")
    self._supplemental_markings = self.get_payload_section("supplementalMarkings")
    self._records_management_settings = self.get_payload_section(
        "recordsManagementSettings",
    )
    self._holds = self.get_payload_section("holds")
    self._doc_generators = self.get_payload_section("documentGenerators")
    self._workflows = self.get_payload_section("workflows")
    self._browser_automations = self.get_payload_section("browserAutomations")
    self._browser_automations_post = self.get_payload_section(
        "browserAutomationsPost",
    )
    self._test_automations = self.get_payload_section("testAutomations")
    self._appworks_configurations = self.get_payload_section("appworks")
    self._avts_repositories = self.get_payload_section("avtsRepositories")
    self._avts_questions = self.get_payload_section("avtsQuestions")
    self._embeddings = self.get_payload_section("embeddings")
    self._nifi_flows = self.get_payload_section("nifi")

    return self._payload

init_payload_user_instances()

Read setting for Multiple User instances.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_payload_user_instances")
def init_payload_user_instances(self) -> None:
    """Read setting for Multiple User instances."""

    for dic in self._payload_sections:
        if dic.get("name") == "users":
            users_payload = dic
            break
    user_instances = users_payload.get("additional_instances", 0)

    if user_instances == 0:
        self.logger.info("No additional user instances configured (default = 0).")
        return

    i = 0

    original_users = copy.deepcopy(self._users)
    while i <= user_instances:
        for user in copy.deepcopy(original_users):
            user["name"] = user["name"] + "-" + str(i).zfill(2)
            user["lastname"] = user["lastname"] + " " + str(i).zfill(2)
            user["enable_sap"] = False
            user["enable_o365"] = False
            user["enable_core_share"] = False
            user["enable_salesforce"] = False
            user["enable_successfactors"] = False

            self.logger.info(
                "Creating additional user instance -> '%s'",
                user["name"],
            )
            self.logger.debug("Create user instance -> %s", user)
            self._users.append(user)

        i = i + 1

    return

init_salesforce(salesforce_external_system)

Initialize Salesforce object for workspace creation.

This is needed to query Salesforce API to lookup IDs of Salesforce objects.

Parameters:

Name Type Description Default
salesforce_external_system dict

The payload of the Salesforce external system created before.

required

Returns:

Type Description
Salesforce | None

Salesforce | None: Salesforce object or None in case an error occured.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_salesforce")
def init_salesforce(self, salesforce_external_system: dict) -> Salesforce | None:
    """Initialize Salesforce object for workspace creation.

    This is needed to query Salesforce API to lookup IDs of Salesforce objects.

    Args:
        salesforce_external_system (dict):
            The payload of the Salesforce external system created before.

    Returns:
        Salesforce | None:
            Salesforce object or None in case an error occured.

    """

    if not salesforce_external_system:
        return None

    username = salesforce_external_system["username"]
    password = salesforce_external_system["password"]
    base_url = salesforce_external_system["base_url"]
    authorization_url = salesforce_external_system.get("token_endpoint", "")
    client_id = salesforce_external_system["oauth_client_id"]
    client_secret = salesforce_external_system["oauth_client_secret"]

    self.logger.info("Connection parameters Salesforce:")
    self.logger.info("Salesforce base URL          = %s", base_url)
    self.logger.info("Salesforce authorization URL = %s", base_url)
    self.logger.info("Salesforce username          = %s", username)
    self.logger.debug("Salesforce password          = %s", password)
    self.logger.info("Salesforce client ID         = %s", client_id)
    self.logger.debug("Salesforce client secret     = %s", client_secret)
    salesforce_object = Salesforce(
        base_url=base_url,
        client_id=client_id,
        client_secret=client_secret,
        username=username,
        password=password,
        authorization_url=authorization_url,
        logger=self.logger,
    )

    self._salesforce = salesforce_object

    return salesforce_object

init_sap(sap_external_system, direct_application_server_login=True)

Initialize SAP object for RFC communication with SAP S/4HANA.

Parameters:

Name Type Description Default
sap_external_system dict

SAP external system created before

required
direct_application_server_login bool

Flag to control whether we comminicate directly with SAP application server or via a load balancer

True

Returns:

Type Description
SAP | None

SAP | None: The SAP object or None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_sap")
def init_sap(
    self,
    sap_external_system: dict,
    direct_application_server_login: bool = True,
) -> SAP | None:
    """Initialize SAP object for RFC communication with SAP S/4HANA.

    Args:
        sap_external_system (dict):
            SAP external system created before
        direct_application_server_login (bool, optional):
            Flag to control whether we comminicate directly with
            SAP application server or via a load balancer

    Returns:
        SAP | None:
            The SAP object or None in case of an error.

    """

    if not sap_external_system:
        return None

    username = sap_external_system["username"]
    password = sap_external_system["password"]
    # "external_system_hostname" is extracted from as_url in process_external_systems()
    host = sap_external_system["external_system_hostname"]
    client = sap_external_system.get("client", "100")
    system_number = sap_external_system.get("external_system_number", "00")
    system_id = sap_external_system["external_system_name"]
    group = sap_external_system.get("group", "PUBLIC")
    destination = sap_external_system.get("destination", "")

    self.logger.info("Connection parameters SAP:")
    self.logger.info("SAP Hostname             = %s", host)
    self.logger.info("SAP Client               = %s", client)
    self.logger.info("SAP System Number        = %s", system_number)
    self.logger.info("SAP System ID            = %s", system_id)
    self.logger.info("SAP User Name            = %s", username)
    if not direct_application_server_login:
        self.logger.info("SAP Group Name (for RFC) = %s", group)
    if destination:
        self.logger.info("SAP Destination          = %s", destination)

    if direct_application_server_login:
        self.logger.info(
            "SAP Login                = Direct Application Server (ashost)",
        )
        sap_object = SAP(
            username=username,
            password=password,
            ashost=host,
            client=client,
            system_number=system_number,
            system_id=system_id,
            destination=destination,
            logger=self.logger,
        )
    else:
        self.logger.info(
            "SAP Login                = Logon with load balancing (mshost)",
        )
        sap_object = SAP(
            username=username,
            password=password,
            mshost=host,
            group=group,
            client=client,
            system_number=system_number,
            system_id=system_id,
            destination=destination,
            logger=self.logger,
        )

    self._sap = sap_object

    if (
        "archive_logical_name" in sap_external_system
        and "archive_certificate_file" in sap_external_system
        and self._otac
    ):
        self.logger.info(
            "Put certificate file -> '%s' for logical archive -> '%s' into Archive Center...",
            sap_external_system["archive_certificate_file"],
            sap_external_system["archive_logical_name"],
        )
        response = self._otac.put_cert(
            sap_external_system["external_system_name"],
            sap_external_system["archive_logical_name"],
            sap_external_system["archive_certificate_file"],
        )
        if not response:
            self.logger.error("Failed to install Archive Center certificate!")
        else:
            self.logger.info(
                "Enable certificate file -> '%s' for logical archive -> '%s'...",
                sap_external_system["archive_certificate_file"],
                sap_external_system["archive_logical_name"],
            )
            response = self._otac.enable_cert(
                auth_id=sap_external_system["external_system_name"],
                logical_archive=sap_external_system["archive_logical_name"],
                enable=True,
            )
            if not response:
                self.logger.debug("Failed to enable Archive Center certificate!")

    return sap_object

init_successfactors(sucessfactors_external_system)

Initialize SuccessFactors object for workspace creation.

This is needed synchronize user passwords and emails with SuccessFactors.

Parameters:

Name Type Description Default
sucessfactors_external_system dict

The payload of the SuccessFactors external system created before.

required

Returns:

Name Type Description
SuccessFactors SuccessFactors | None

The SuccessFactors object.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="init_successfactors")
def init_successfactors(
    self,
    sucessfactors_external_system: dict,
) -> SuccessFactors | None:
    """Initialize SuccessFactors object for workspace creation.

    This is needed synchronize user passwords and emails with SuccessFactors.

    Args:
        sucessfactors_external_system (dict):
            The payload of the SuccessFactors external system created before.

    Returns:
        SuccessFactors: The SuccessFactors object.

    """

    def extract_company_from_url(url: str) -> str:
        parsed_url = urlparse(url)
        query_params = parse_qs(parsed_url.query)
        company_value = query_params.get("company", [""])[0]
        return company_value

    if not sucessfactors_external_system:
        return None

    username = sucessfactors_external_system["username"]
    password = sucessfactors_external_system["password"]
    base_url = sucessfactors_external_system["base_url"]
    as_url = sucessfactors_external_system["as_url"]
    saml_url = sucessfactors_external_system.get("saml_url", "")
    company_id = extract_company_from_url(saml_url)
    client_id = sucessfactors_external_system["oauth_client_id"]
    client_secret = sucessfactors_external_system["oauth_client_secret"]

    self.logger.info("Connection parameters SuccessFactors:")
    self.logger.info("SuccessFactors base URL            = %s", base_url)
    self.logger.info("SuccessFactors application URL     = %s", as_url)
    self.logger.info("SuccessFactors username            = %s", username)
    self.logger.debug("SuccessFactors password            = %s", password)
    self.logger.info("SuccessFactors client ID           = %s", client_id)
    self.logger.debug("SuccessFactors client secret       = %s", client_secret)
    self.logger.info("SuccessFactors company ID (tenant) = %s", company_id)
    successfactors_object = SuccessFactors(
        base_url=base_url,
        as_url=as_url,
        client_id=client_id,
        client_secret=client_secret,
        username=username,
        password=password,
        company_id=company_id,
        logger=self.logger,
    )

    self._successfactors = successfactors_object

    return successfactors_object

lookup_data_source_value(data_source, lookup_column, lookup_value, separator='|')

Lookup a value in a given data source (specified by payload dict).

If the data source has not been loaded before then load the data source. As this runs in a multi-threading environment we need to protect the data source update from multiple threads doing it at the same time. A global data_load_lock variable acts as a mutex.

Parameters:

Name Type Description Default
data_source dict

The payload dictionary of the data source definition.

required
lookup_column str

The name of the column in the data frame (see Data class).

required
lookup_value str

The value to lookup - selection criteria for the result row.

required
separator str

The string list delimiter / separator. The pipe symbol | is the default as it is unlikely to appear in a normal string (other than a plain comma). The separator is NOT looked for in the lookup_value but in the column that is given by lookup_column!

'|'

Returns:

Type Description
Series | None

pd.Series | None: Row that matches the lookup value in the lookup column.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="lookup_data_source_value")
def lookup_data_source_value(
    self,
    data_source: dict,
    lookup_column: str,
    lookup_value: str,
    separator: str = "|",
) -> pd.Series | None:
    """Lookup a value in a given data source (specified by payload dict).

    If the data source has not been loaded before then load the data source.
    As this runs in a multi-threading environment we need to protect
    the data source update from multiple threads doing it at the same time.
    A global data_load_lock variable acts as a mutex.

    Args:
        data_source (dict):
            The payload dictionary of the data source definition.
        lookup_column (str):
            The name of the column in the data frame (see Data class).
        lookup_value (str):
            The value to lookup - selection criteria for the result row.
        separator (str, optional):
            The string list delimiter / separator. The pipe symbol | is the default
            as it is unlikely to appear in a normal string (other than a plain comma).
            The separator is NOT looked for in the lookup_value but in the column that
            is given by lookup_column!

    Returns:
        pd.Series | None:
            Row that matches the lookup value in the lookup column.

    """

    data_source_name = data_source.get("name")
    if not data_source_name:
        self.logger.error("Data source has no name!")
        return None

    # We don't want multiple threads to trigger a data source load at the same time,
    # so we use a lock (mutex) to avoid this:
    data_load_lock.acquire()
    try:
        # First we check if the data source has been loaded already.
        # If not, we load the data source on the fly:
        data_source_data: Data = data_source.get("data")
        if not data_source_data:
            self.logger.info(
                "Lookup data source -> '%s' has no data yet. Reloading...",
                data_source_name,
            )
            data_source_data = self.process_bulk_datasource(
                data_source_name=data_source_name,
                force_reload=True,
            )
    finally:
        # Ensure the lock is released even if an error occurs
        data_load_lock.release()

    # If we still don't have data from this data source we bail out:
    if data_source_data is None:  # important to use "is None" here!
        self.logger.error(
            "Lookup data source -> '%s' has no data and reload did not work. Cannot lookup value -> '%s' in column -> '%s'!",
            data_source_name,
            lookup_value,
            lookup_column,
        )
        return None

    # Lookup the data frame row (pd.Series) in the given
    # column with the given lookup value:
    lookup_row: pd.Series = data_source_data.lookup_value(
        lookup_column=lookup_column,
        lookup_value=lookup_value,
        separator=separator,
    )

    return lookup_row

lookup_external_system(ext_system_id, prefix='success_payload_')

Lookup an external system.

Considers the current payload but also payload processed before (which is stored as JSON file in the Admin Personal Workspace).

TODO: This is a workaround as the REST API for external systems is too limited and does not return the actual configuration settings of external systems. Check for newer OTCS versions.

Parameters:

Name Type Description Default
ext_system_id str

The external system name to lookup.

required
prefix str

The prefix of the success file in the Admin personal workspace.

'success_payload_'

Returns:

Type Description
dict | None

dict | None: The configuration data of the external system.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="lookup_external_system")
def lookup_external_system(self, ext_system_id: str, prefix: str = "success_payload_") -> dict | None:
    """Lookup an external system.

    Considers the current payload but also payload processed before (which is stored
    as JSON file in the Admin Personal Workspace).

    TODO: This is a workaround as the REST API for external systems is too limited
    and does not return the actual configuration settings of external systems.
    Check for newer OTCS versions.

    Args:
        ext_system_id (str):
            The external system name to lookup.
        prefix (str):
            The prefix of the success file in the Admin personal workspace.

    Returns:
        dict | None:
            The configuration data of the external system.

    """

    # First check if external system is in current payload:
    external_system = next(
        (item for item in self._external_systems if item["external_system_name"] == ext_system_id), None
    )
    if external_system:
        self.logger.info("Found external system -> '%s' declared in current payload.", ext_system_id)
        return external_system

    # Now we try to load it from previous processed payloads:

    if not self.check_status_file(payload_section_name="externalSystems", payload_specific=False, prefix=prefix):
        self.logger.error("Cannot find external system -> '%s'", ext_system_id)
        return None

    additional_systems = self.get_status_file(
        payload_section_name="externalSystems", payload_specific=False, prefix=prefix
    )

    # Merge avoiding duplicates and only enabled entries. existing_names is a set:
    existing_names = {item["external_system_name"] for item in self._external_systems}
    for sys in additional_systems:
        if sys["enabled"] and sys["external_system_name"] not in existing_names:
            self._external_systems.append(sys)
            existing_names.add(sys["external_system_name"])

    # Try finding the external system payload again after merging:
    external_system = next(
        (item for item in self._external_systems if item["external_system_name"] == ext_system_id), None
    )

    if external_system:
        self.logger.info("Found external system -> '%s' in previously processed payload file.", ext_system_id)
    else:
        self.logger.error(
            "Cannot find external system -> '%s' in list of external systems -> %s!",
            ext_system_id,
            str(existing_names),
        )

    return external_system

prepare_category_data(categories_payload, source_node_id, source_is_document=False)

Prepare the metadata structure for a new workspace or document.

This translates the category information from the PAYLOAD into the structure required by the OTCS REST API for category updates.

The difficulty is to get the category schema for the OTCS category. The method tries different approaches to get the schema: 1. Check if the category schema is on a source node (e.g. inherited from a parent node) 2. Find the category definition by nickname and then assign it to the source node (then retrying approach 1) 3. Find the category definition by a unique name and then assign it to the source node (then retrying approach 1)

Parameters:

Name Type Description Default
categories_payload dict

The payload information for the workspace or document categories.

required
source_node_id int

The item to derive or inherit the category data from. We expect this to be a folder, workspace or document that has the category assigned.

required
source_is_document bool

If the source node type is a document we need to handle category assignment a bit different. Default is False.

False

Returns:

Type Description
dict | None

dict | None: Category data structure required for subsequent document upload OTCS REST call.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
24790
24791
24792
24793
24794
24795
24796
24797
24798
24799
24800
24801
24802
24803
24804
24805
24806
24807
24808
24809
24810
24811
24812
24813
24814
24815
24816
24817
24818
24819
24820
24821
24822
24823
24824
24825
24826
24827
24828
24829
24830
24831
24832
24833
24834
24835
24836
24837
24838
24839
24840
24841
24842
24843
24844
24845
24846
24847
24848
24849
24850
24851
24852
24853
24854
24855
24856
24857
24858
24859
24860
24861
24862
24863
24864
24865
24866
24867
24868
24869
24870
24871
24872
24873
24874
24875
24876
24877
24878
24879
24880
24881
24882
24883
24884
24885
24886
24887
24888
24889
24890
24891
24892
24893
24894
24895
24896
24897
24898
24899
24900
24901
24902
24903
24904
24905
24906
24907
24908
24909
24910
24911
24912
24913
24914
24915
24916
24917
24918
24919
24920
24921
24922
24923
24924
24925
24926
24927
24928
24929
24930
24931
24932
24933
24934
24935
24936
24937
24938
24939
24940
24941
24942
24943
24944
24945
24946
24947
24948
24949
24950
24951
24952
24953
24954
24955
24956
24957
24958
24959
24960
24961
24962
24963
24964
24965
24966
24967
24968
24969
24970
24971
24972
24973
24974
24975
24976
24977
24978
24979
24980
24981
24982
24983
24984
24985
24986
24987
24988
24989
24990
24991
24992
24993
24994
24995
24996
24997
24998
24999
25000
25001
25002
25003
25004
25005
25006
25007
25008
25009
25010
25011
25012
25013
25014
25015
25016
25017
25018
25019
25020
25021
25022
25023
25024
25025
25026
25027
25028
25029
25030
25031
25032
25033
25034
25035
25036
25037
25038
25039
25040
25041
25042
25043
25044
25045
25046
25047
25048
25049
25050
25051
25052
25053
25054
25055
25056
25057
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="prepare_category_data")
def prepare_category_data(
    self,
    categories_payload: dict,
    source_node_id: int,
    source_is_document: bool = False,
) -> dict | None:
    """Prepare the metadata structure for a new workspace or document.

    This translates the category information from the PAYLOAD into the structure
    required by the OTCS REST API for category updates.

    The difficulty is to get the category schema for the OTCS category. The method tries different
    approaches to get the schema:
    1. Check if the category schema is on a source node (e.g. inherited from a parent node)
    2. Find the category definition by nickname and then assign it to the source node (then retrying approach 1)
    3. Find the category definition by a unique name and then assign it to the source node (then retrying approach 1)

    Args:
        categories_payload (dict):
            The payload information for the workspace or document categories.
        source_node_id (int):
            The item to derive or inherit the category data from. We expect this to
            be a folder, workspace or document that has the category assigned.
        source_is_document (bool, optional):
            If the source node type is a document we need to handle category assignment
            a bit different. Default is False.

    Returns:
        dict | None:
            Category data structure required for subsequent document upload OTCS REST call.

    """

    # Determine the category names in the payload. For this we use a
    # comprehension to create a set (with the curly braces) of unique category
    # names used in the payload that then is converted to a list:
    category_names = list({category["name"] for category in categories_payload})

    # Determine the names of all categories inherited from the source_node_id:
    response = self._otcs_frontend.get_node_categories(node_id=source_node_id)
    if response and response["results"]:
        inherited_category_names = [
            next(iter(item["metadata"]["categories"].values()))["name"]
            for item in response["results"]
            if item.get("metadata")
        ]
    else:
        inherited_category_names = []

    # Calculate the category names we have in payload that are NOT inherited (set difference):
    missing_categories: set[str] = set(category_names) - set(inherited_category_names)

    # Are any of the payload categories NOT inherited from the source_node_id ?
    if missing_categories:
        self.logger.info(
            "Categories -> %s are in payload but not found on source node with ID -> %s. Try to find these categories by nicknames or unique names...",
            str(missing_categories),
            str(source_node_id),
        )
        for category_name in missing_categories:  # category_names:
            category_node_id = None
            # First try via nickname. We expect prefix "cat_" and snake case nicknames:
            category_nickname = (
                "cat_"
                + category_name.replace(" - ", "_")
                .replace("-", "_")
                .replace(" ", "_")
                .replace("___", "_")
                .replace("(", "")
                .replace(")", "")
                .lower()
            )
            category_node = self._otcs_frontend.get_node_from_nickname(
                nickname=category_nickname,
            )
            category_node_id = self._otcs_frontend.get_result_value(
                response=category_node,
                key="id",
            )
            if category_node_id:
                self.logger.info(
                    "Found category -> '%s' with ID -> %s via nickname -> '%s'.",
                    category_name,
                    category_node_id,
                    category_nickname,
                )
            # Next try via unique names:
            else:
                response = self._otcs_frontend.get_unique_names(
                    names=[category_name],
                    subtype=self._otcs_frontend.ITEM_TYPE_CATEGORY,
                )
                if response and "results" in response and response["results"]:
                    category_node_id = next(
                        (result["NodeId"] for result in response["results"] if result["NodeName"] == category_name),
                        None,
                    )
                    if category_node_id:
                        self.logger.info(
                            "Found category -> '%s' with ID -> %s via unique name lookup!",
                            category_name,
                            category_node_id,
                        )

            # if we found the payload category via nickname or unique name
            # we assign it to thew source node (typically the parent node):
            if category_node_id:
                self._otcs_frontend.assign_category(
                    node_id=source_node_id,
                    category_id=category_node_id,
                    inheritance=None if source_is_document else True,
                    apply_to_sub_items=not source_is_document,
                )
        # end for category_name in missing_categories

        # Now with the category assigned to the parent (source node id)
        # We retry getting the inherited category:
        response = self._otcs_frontend.get_node_categories(node_id=source_node_id)
    # end if missing_categories:

    # Initialize the result dict we will return at the end of the method
    # and the list of inherited categories:
    category_data = {}
    inherited_categories = []

    # Redo the test...
    if not response or not response["results"]:
        self.logger.warning(
            "Source node with ID -> %s does not inherit categories but we have category payload! Processing without assiging category...",
            source_node_id,
        )
    else:
        inherited_categories = response["results"]

    # we iterate over all parent categories that are inherited
    # to the new document and try to find matching payload values...
    for inherited_category in inherited_categories:
        # We use the "metadata_order" which is a list of typically one
        # element that includes the category ID:
        metadata_order = inherited_category["metadata_order"]

        # If it is not a list or empty we continue with the
        # next inherited category:
        if not metadata_order["categories"] or not isinstance(
            metadata_order["categories"],
            list,
        ):
            continue
        inherited_category_id = metadata_order["categories"][0]

        # We use the "metadata" dict to determine the category name
        # the keys of the dict are the category ID and attribute IDs
        # the first element in the dict is always the category itself.
        metadata = inherited_category["metadata"]["categories"]
        category_name = metadata[str(inherited_category_id)]["name"]

        self.logger.debug(
            "Source node ID -> %s has category -> '%s' (%s)",
            source_node_id,
            category_name,
            inherited_category_id,
        )

        # The following method returns two values: the category ID and
        # a dict of the attributes. If the category is not found
        # on the parent node it returns -1 for the category ID
        # and an empty dict for the attribute definitions:
        (
            category_id,
            attribute_definitions,
        ) = self._otcs_frontend.get_node_category_definition(
            node_id=source_node_id,
            category_name=category_name,
        )
        if category_id == -1:
            self.logger.error(
                "The item with ID -> %s does not have the specified category -> '%s' assigned. Skipping...",
                source_node_id,
                category_name,
            )
            continue

        # We now initialize the substructure for this particular category:
        category_data[str(category_id)] = {}

        self.logger.debug(
            "Processing the attributes in payload to find values for the inherited category -> '%s' (%s)...",
            category_name,
            category_id,
        )
        # now we fill the prepared (but empty) category_data
        # with the actual attribute values from the payload.
        # For this we traverse all category dicts in the payload
        # and check if they include data for the currently processed
        # category:
        for attribute in categories_payload:
            attribute_name = attribute["attribute"]
            set_name = attribute.get("set", "")
            row = attribute.get("row", "")
            if attribute["name"] != category_name:
                self.logger.debug(
                    "Attribute -> '%s' does not belong to category -> '%s'. Skipping...",
                    attribute_name,
                    category_name,
                )
                continue
            attribute_value = attribute["value"]

            # Set attributes are constructed with <set>:<attribute>
            # by method get_node_category_definition(). This is not
            # an OTCS REST syntax but specific for payload.py
            if set_name:
                attribute_name = set_name + ":" + attribute_name

            if attribute_name not in attribute_definitions:
                self.logger.error(
                    "Illegal attribute name -> '%s' in payload for category -> '%s'",
                    attribute_name,
                    category_name,
                )
                continue
            attribute_type = attribute_definitions[attribute_name]["type"]
            attribute_id = attribute_definitions[attribute_name]["id"]
            # For multi-line sets the "x" is the placeholder for the
            # row number. We need to replace it with the actual row number
            # given in the payload:
            if "_x_" in attribute_id:
                if not row:
                    self.logger.error(
                        "Row number is not specified in payload for attribute -> '%s' (%s)",
                        attribute_name,
                        attribute_id,
                    )
                    continue
                attribute_id = attribute_id.replace("_x_", "_" + str(row) + "_")

            # Special treatment for type user: determine the ID for the login name.
            # the ID is the actual value we have to put in the attribute:
            if attribute_type == "user":
                user = self._otcs_frontend.get_user(
                    name=attribute_value,
                    show_error=True,
                )
                user_id = self._otcs_frontend.get_result_value(
                    response=user,
                    key="id",
                )
                if not user_id:
                    self.logger.error(
                        "Cannot find user with login name -> '%s'. Skipping...",
                        attribute_value,
                    )
                    continue
                attribute_value = user_id
            category_data[str(category_id)][attribute_id] = attribute_value
        # end for attribute in categories_payload:

        # If for none of the attributes of the current category ID a value was found
        # in the payload we remove the dictionary entry to not cause problems
        # for later category updates:
        if not category_data[str(category_id)]:
            del category_data[str(category_id)]
    # end for inherited_category in inherited_categories:

    self.logger.debug("Resulting category data -> %s", str(category_data))

    return category_data

prepare_datasource_file(data_source, filename)

Download data source from HTTP/HTTPS link and store it in a local temp file.

Parameters:

Name Type Description Default
data_source dict

The data source object from payload.

required
filename str

The filename to check if it is a reference to an external file.

required

Returns:

Name Type Description
str str

Filename or Filename of the local temp file.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="prepare_datasource_file")
def prepare_datasource_file(self, data_source: dict, filename: str) -> str:
    """Download data source from HTTP/HTTPS link and store it in a local temp file.

    Args:
        data_source (dict):
            The data source object from payload.
        filename (str):
            The filename to check if it is a reference to an external file.

    Returns:
        str:
            Filename or Filename of the local temp file.

    """

    if not filename.startswith(("http://", "https://")):
        return filename

    name = data_source.get("name", "")
    tmp_filename = os.path.join(tempfile.gettempdir(), "{}_{}".format(name, os.path.basename(filename)))

    if os.path.isfile(tmp_filename):
        self.logger.info("Reusing previously downloaded file -> '%s'.", tmp_filename)
        return tmp_filename

    try:
        self.logger.info(
            "Downloading data source from -> %s to -> %s...",
            filename,
            tmp_filename,
        )
        response = requests.get(filename, stream=True, timeout=10)

        with open(tmp_filename, "wb") as f:
            for chunk in response.iter_content(chunk_size=1024):
                if chunk:
                    f.write(chunk)

    except Exception:
        self.logger.error("Error downloading JSON data source -> '%s'!", filename)

    return tmp_filename

prepare_item_create_form(parent_id, categories, subtype=OTCS.ITEM_TYPE_DOCUMENT)

Prepare the category structure for the item creation.

Parameters:

Name Type Description Default
parent_id int

The node the category should be applied to.

required
categories list

The categories list from the document payload.

required
subtype int

The subtype of the new node. Default is document.

ITEM_TYPE_DOCUMENT

Returns:

Type Description
dict | None

dict | None: Category structure for workspace creation or None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="prepare_item_create_form")
def prepare_item_create_form(
    self,
    parent_id: int,
    categories: list,
    subtype: int = OTCS.ITEM_TYPE_DOCUMENT,
) -> dict | None:
    """Prepare the category structure for the item creation.

    Args:
        parent_id (int):
            The node the category should be applied to.
        categories (list):
            The categories list from the document payload.
        subtype (int):
            The subtype of the new node. Default is document.

    Returns:
        dict | None:
            Category structure for workspace creation or None
            in case of an error.

    """

    category_create_data = {}

    category_ids = []

    # Get unique combination of volume, path, name to get a list of all categories
    unique_categories = {
        (entry.get("volume", OTCS.VOLUME_TYPE_CATEGORIES_VOLUME), entry.get("path", None), entry["name"])
        for entry in categories
    }

    # Determine the names of all categories inherited from the parent_id:
    response = self._otcs_frontend.get_node_categories(node_id=parent_id)
    if response and response["results"]:
        category_ids = [
            next(iter(item["metadata_order"]["categories"])) for item in response["results"] if item.get("metadata")
        ]

    for volume, path, name in unique_categories:
        self.logger.debug("%s : %s", name, path)

        if isinstance(path, str):
            path = [path, name]
        elif isinstance(path, list):
            path.append(name)
        else:
            continue

        response = self._otcs_frontend.get_node_by_volume_and_path(
            volume_type=volume,
            path=path,
        )
        cat_id = self._otcs_frontend.get_result_value(response=response, key="id")
        if cat_id and cat_id not in category_ids:
            category_ids.append(cat_id)
    # end for volume, path, name in unique_categories:

    response = self._otcs_frontend.get_node_create_form(
        parent_id=parent_id,
        subtype=subtype,
        category_ids=category_ids,
    )
    if response is None or "forms" not in response:
        self.logger.error(
            "Failed to retrieve create information for subtype -> %s in parent with ID -> %d!",
            subtype,
            parent_id,
        )
        return None

    self.logger.debug(
        "Successfully retrieved create information for subtype -> %s in parent with ID -> %d.",
        subtype,
        parent_id,
    )

    # Process category information
    forms = response["forms"]

    categories_form = {}

    # Typically the the create item form delivers 4-5 forms:
    # 1. Form for System Attributes (has no role name)
    # 2. Form for Category Data (role name = "categories")
    # 3. Form for Classifications (role name = "classifications")
    # 4. Form for importing team members from another existing team
    # 5. Form for Microsoft Teams integration (role name = "microsoftteams")
    for form in forms:
        if "role_name" not in form:
            self.logger.debug("Found 'system attributes' form -> %s", str(form))
            continue
        match form["role_name"]:
            case "categories":
                categories_form = form
                self.logger.debug("Found 'categories' form -> %s", form)
                continue
            case "classifications":
                self.logger.debug("Found 'classifications' form -> %s", form)
                continue
            case "rmclassifications":
                self.logger.debug("Found 'RM classifications' form -> %s", form)
                continue
            case "importteam":
                self.logger.debug("Found 'import team participants' form -> %s", form)
                continue
            case "microsoftteams":
                self.logger.debug("Found Import Team participants form -> %s", form)
                continue
            case _:
                # the remaining option is that this form is the system attributes form:
                self.logger.warning("Unknown form -> %s", str(form))
        # end match form["role_name"]:
    # end for form in forms:

    # We are just interested in the single category data set (role_name = "categories"):
    data = categories_form.get("data", None)

    if not data:
        self.logger.debug("No categories data found.")
        return category_create_data

    self.logger.debug("Categories data found -> %s", data)
    schema = categories_form["schema"]["properties"]
    self.logger.debug("Categories schema found -> %s", schema)
    # Loop over categories:
    for cat_id in data:  # these are the category IDs (dict keys):
        self.logger.debug("Category ID -> %s", cat_id)
        data_attributes = data[cat_id]
        self.logger.debug("Data attributes -> %s", data_attributes)
        schema_attributes = schema[cat_id]["properties"]
        self.logger.debug("Schema attributes -> %s", schema_attributes)
        cat_name = schema[cat_id]["title"]
        self.logger.debug("Category name -> %s", cat_name)
        # Loop over attributes:
        # * Sets with one (fixed) row have type = object
        # * Multi-value Sets with (multiple) rows have type = array and "properties" in "items" schema
        # * Multi-value attributes have also type = array but NO "properties" in "items" schema
        for attr_id in data_attributes:  # these a attribute IDs (dict keys)
            self.logger.debug("Attribute ID -> %s", attr_id)
            self.logger.debug("Attribute data -> %s", data_attributes[attr_id])
            self.logger.debug(
                "Attribute schema -> %s",
                schema_attributes[attr_id],
            )
            attr_type = schema_attributes[attr_id]["type"]
            self.logger.debug("Attribute type -> %s", attr_type)
            if "title" not in schema_attributes[attr_id]:
                self.logger.debug("Attribute has no title. Skipping...")
                continue

            #
            # Handle multi-line set:
            #
            if attr_type == "array" and ("properties" in schema_attributes[attr_id]["items"]):
                set_name = schema_attributes[attr_id]["title"]
                self.logger.debug("Multi-line set -> %s", set_name)
                set_data_attributes = data_attributes[attr_id]  # this is a list []
                if set_data_attributes is None:
                    self.logger.debug("Attribute has no value in data. Skipping...")
                    continue
                self.logger.debug("Set data attributes -> %s", set_data_attributes)
                set_schema_attributes = schema_attributes[attr_id]["items"]["properties"]
                self.logger.debug(
                    "Set schema attributes -> %s",
                    set_schema_attributes,
                )
                set_schema_max_rows = schema_attributes[attr_id]["items"]["maxItems"]
                self.logger.debug("Set schema max rows -> %s", set_schema_max_rows)
                set_data_max_rows = len(set_data_attributes)
                self.logger.debug("Set data xax rows -> %s", set_data_max_rows)
                row = 1
                # it can happen that the payload contains more rows than the
                # initial rows in the set data structure. In this case we use
                # a copy of the data structure from row 0 as template...
                first_row = dict(set_data_attributes[0])
                # We don't know upfront how many rows of data we will find in payload
                # but we at max process the maxItems specified in the schema:
                while row <= set_schema_max_rows:
                    # Test if we have any payload for this row:
                    attribute = next(
                        (
                            item
                            for item in categories
                            if (
                                item["name"] == cat_name
                                and "set" in item  # not all items may have a "set" key
                                and item["set"] == set_name
                                and "row" in item  # not all items may have a "row" key
                                and item["row"] == row
                            )
                        ),
                        None,
                    )
                    # stop if there's no payload for the row:
                    if attribute is None:
                        self.logger.debug(
                            "No payload found for set -> %s, row -> %s",
                            set_name,
                            row,
                        )
                        # we assume that if there's no payload for row n there will be no payload for rows > n
                        # and break the while loop:
                        break
                    # do we need to create a new row in the data frame?
                    if row > set_data_max_rows:
                        # use the row we stored above to create a new empty row:
                        self.logger.debug(
                            "Found payload for row -> %s, we need a new data row for it",
                            row,
                        )
                        self.logger.debug(
                            "Adding an additional row -> %s to set data -> '%s'",
                            row,
                            set_name,
                        )
                        # add the empty dict to the list:
                        set_data_attributes.append(dict(first_row))
                        set_data_max_rows += 1
                    else:
                        self.logger.debug(
                            "Found payload for row -> %s %s we can store in existing data row",
                            row,
                            set_name,
                        )
                    # traverse all attributes in a single row:
                    for set_attr_id in set_schema_attributes:
                        self.logger.debug(
                            "Set attribute ID -> %s (row -> %s)",
                            set_attr_id,
                            row,
                        )
                        self.logger.debug(
                            "Set attribute schema -> %s (row -> %s)",
                            set_schema_attributes[set_attr_id],
                            row,
                        )
                        set_attr_type = set_schema_attributes[set_attr_id]["type"]
                        self.logger.debug(
                            "Set attribute type -> %s (row -> %s)",
                            set_attr_type,
                            row,
                        )
                        set_attr_name = set_schema_attributes[set_attr_id]["title"]
                        self.logger.debug(
                            "Set attribute name -> %s (row -> %s)",
                            set_attr_name,
                            row,
                        )
                        # Lookup the attribute with the right category, set, attribute name, and row number in payload:
                        attribute = next(
                            (
                                item
                                for item in categories
                                if (
                                    item["name"] == cat_name
                                    and "set" in item  # not all items may have a "set" key
                                    and item["set"] == set_name
                                    and item["attribute"] == set_attr_name
                                    and "row" in item  # not all items may have a "row" key
                                    and item["row"] == row
                                )
                            ),
                            None,
                        )
                        if attribute is None:
                            self.logger.debug(
                                "Set -> '%s', attribute -> '%s', row -> %s not found in payload.",
                                set_name,
                                set_attr_name,
                                row,
                            )

                            # need to use row - 1 as index starts with 0 but payload rows start with 1
                            set_data_attributes[row - 1][set_attr_id] = ""
                        else:
                            if set_attr_type == "array" and "items" in set_schema_attributes[set_attr_id]:
                                set_attr_type = set_schema_attributes[set_attr_id]["items"].get(
                                    "type",
                                    set_attr_type,
                                )

                            self.logger.debug(
                                "Set -> '%s', attribute -> '%s', row -> %s found in payload, value -> '%s'",
                                set_name,
                                set_attr_name,
                                row,
                                attribute["value"],
                            )
                            set_data_attributes[row - 1][set_attr_id] = self.resolve_attribute_values(
                                attribute_name=set_attr_name,
                                attribute_type=set_attr_type,
                                attribute_values=attribute["value"],
                            )
                    row += 1  # continue the while loop with the next row
                # end while row <= set_schema_max_rows:
            # end if attr_type == "array" and ("properties" in schema_attributes[attr_id]["items"]):

            #
            # Handle single-line set:
            #
            elif attr_type == "object":
                set_name = schema_attributes[attr_id]["title"]
                self.logger.debug("Single-line set -> '%s'", set_name)
                set_data_attributes = data_attributes[attr_id]
                self.logger.debug("Set data attributes -> %s", set_data_attributes)

                set_schema_attributes = schema_attributes[attr_id]["properties"]
                self.logger.debug(
                    "Set schema attributes -> %s",
                    str(set_schema_attributes),
                )
                # Loop over set attributes:
                for set_attr_id in set_data_attributes:
                    self.logger.debug("Set attribute ID -> %s", set_attr_id)
                    self.logger.debug(
                        "Set attribute data -> %s",
                        str(set_data_attributes[set_attr_id]),
                    )
                    self.logger.debug(
                        "Set attribute schema -> %s",
                        str(set_schema_attributes[set_attr_id]),
                    )
                    set_attr_type = set_schema_attributes[set_attr_id]["type"]
                    self.logger.debug("Set attribute type -> %s", set_attr_type)
                    set_attr_name = set_schema_attributes[set_attr_id]["title"]
                    self.logger.debug("Set attribute name -> '%s'", set_attr_name)
                    # Lookup the attribute with the right category, set and attribute name in payload:
                    attribute = next(
                        (
                            item
                            for item in categories
                            if (
                                item["name"] == cat_name
                                and "set" in item  # not all items may have a "set" key
                                and item["set"] == set_name
                                and item["attribute"] == set_attr_name
                            )
                        ),
                        None,
                    )
                    if attribute is None:
                        self.logger.debug(
                            "Category -> '%s', set -> %s, attribute -> '%s' not found in payload.",
                            cat_name,
                            set_name,
                            set_attr_name,
                        )
                        set_data_attributes[set_attr_id] = ""
                    else:
                        self.logger.debug(
                            "Category -> '%s', set -> %s, attribute -> '%s' found in payload, value -> %s",
                            cat_name,
                            set_name,
                            set_attr_name,
                            attribute["value"],
                        )
                        # Resolve any special cases (e.g. user picker, group picker):
                        set_data_attributes[set_attr_id] = self.resolve_attribute_values(
                            attribute_name=set_attr_name,
                            attribute_type=set_attr_type,
                            attribute_values=attribute["value"],
                        )
                # end for set_attr_id in set_data_attributes:
            # end elif attr_type == "object":

            #
            # Handle plain attribute (not inside a set):
            #
            else:
                attr_name = schema_attributes[attr_id]["title"]
                self.logger.debug("Attribute name -> '%s'", attr_name)
                # Lookup the attribute with the right category and attribute name in payload:
                attribute = next(
                    (item for item in categories if (item["name"] == cat_name and item["attribute"] == attr_name)),
                    None,
                )

                if attr_type == "array" and "items" in schema_attributes[attr_id]:
                    attr_type = schema_attributes[attr_id]["items"].get("type", attr_type)

                if attribute is None:
                    self.logger.debug(
                        "Category -> '%s', attribute -> '%s' not found in payload.",
                        cat_name,
                        attr_name,
                    )
                    data_attributes[attr_id] = ""
                else:
                    self.logger.debug(
                        "Category -> '%s', attribute -> '%s' found in payload, value -> %s",
                        cat_name,
                        attr_name,
                        attribute["value"],
                    )
                    # Resolve any special cases (e.g. user picker, group picker):
                    data_attributes[attr_id] = self.resolve_attribute_values(
                        attribute_name=attr_name,
                        attribute_type=attr_type,
                        attribute_values=attribute["value"],
                    )
            # end else (plain attribute)
        # end for attr_data, attr_schema
        category_create_data[cat_id] = data_attributes
    # end for cat_data, cat_schema

    self.logger.debug("Category Create Data -> %s", category_create_data)

    return category_create_data

prepare_workspace_business_objects(workspace, business_objects)

Prepare the business object data for the workspace creation.

This supports multiple external system connections. This methods also checks if the external system is reachable and tries to create missing business objects in the leading system if they are missing.

Parameters:

Name Type Description Default
workspace dict

The payload data for the workspace.

required
business_objects list

The payload data for the business object connections.

required

Returns:

Type Description
list | None

list | None: A list of business object data connections (list elements are dictionaries).

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="prepare_workspace_business_objects")
def prepare_workspace_business_objects(
    self,
    workspace: dict,
    business_objects: list,
) -> list | None:
    """Prepare the business object data for the workspace creation.

    This supports multiple external system connections. This methods
    also checks if the external system is reachable and tries to create
    missing business objects in the leading system if they are missing.

    Args:
        workspace (dict):
            The payload data for the workspace.
        business_objects (list):
            The payload data for the business object connections.

    Returns:
        list | None:
            A list of business object data connections
            (list elements are dictionaries).

    """

    business_object_list = []

    for business_object_data in business_objects:
        business_object = {}

        name = workspace.get("name")

        # Read business object data from workspace payload.
        # business_object_data is a dict with 3-5 elements:
        if "external_system" in business_object_data:
            ext_system_id = business_object_data["external_system"]
        else:
            self.logger.error(
                "Missing External System in Business Object payload for workspace -> '%s'.",
                name,
            )
            continue
        if "bo_type" in business_object_data:
            bo_type = business_object_data["bo_type"]
        else:
            self.logger.error(
                "Missing type in Business Object payload for workspace -> '%s'.",
                name,
            )
            continue

        if "bo_id" in business_object_data:
            bo_id = business_object_data["bo_id"]
            bo_search_field = None
            bo_search_value = None
        elif "bo_search_field" not in business_object_data or "bo_search_value" not in business_object_data:
            self.logger.error(
                "Missing BO search fields (bo_search_field, bo_search_value) in Business Object payload without BO ID for workspace -> '%s'.",
                name,
            )
            continue
        else:
            bo_search_field = business_object_data["bo_search_field"]
            bo_search_value = business_object_data["bo_search_value"]
            bo_id = None

        # Lookup external system in this payload or a former processed payload:
        external_system = self.lookup_external_system(ext_system_id=ext_system_id)

        # If the external system is not in the current payload but in the system
        # we try to avoid errors in the following code.
        # TODO: review REST APIs in OTCS 26.1 version to see if we can improve this.
        if not external_system and self._otcs.get_external_system_connection(connection_name=ext_system_id):
            # As the REST API for reading external system data is pretty much limited
            # we try to do the bare minimum here:
            if "Guidewire" in business_object_data["external_system"]:
                external_system_type = "Guidewire"
            elif "Salesforce" in business_object_data["external_system"]:
                external_system_type = "Salesforce"
            external_system = {"enabled": True, "reachable": True, "external_system_type": external_system_type}
            self.logger.info(
                "External system -> '%s' is not found in current payload but it exists in the system. Construct minimum external system information -> %s.",
                ext_system_id,
                str(external_system),
            )

        if not external_system:
            self.logger.warning(
                "External System -> '%s' does not exist. Cannot connect workspace -> '%s' to -> %s. Create workspace without connection.",
                ext_system_id,
                name,
                ext_system_id,
            )
            continue
        if not external_system.get("enabled", True):
            self.logger.info(
                "External System -> '%s' is disabled in payload. Cannot connect workspace -> '%s' to -> (%s, %s, %s, %s, %s). Create workspace without connection...",
                ext_system_id,
                name,
                ext_system_id,
                bo_type,
                bo_id,
                bo_search_field,
                bo_search_value,
            )
            continue
        if not external_system.get("reachable"):
            self.logger.warning(
                "External System -> '%s' is not reachable. Cannot connect workspace -> '%s' to -> (%s, %s, %s, %s, %s). Create workspace without connection...",
                ext_system_id,
                name,
                ext_system_id,
                bo_type,
                bo_id,
                bo_search_field,
                bo_search_value,
            )
            continue
        external_system_type = external_system.get("external_system_type", "")

        # For Salesforce we try to determine the actual business object ID (technical ID) if it is
        # not specified directly (but instead a search field and search value):
        if external_system_type == "Salesforce" and not bo_id:
            # If Salesforce external system is used across payloads it may not be initialized here:
            if not self._salesforce:
                self._salesforce = self.init_salesforce(external_system)
            bo_id = self.get_salesforce_business_object(
                workspace,
                object_type=bo_type,
                search_field=bo_search_field,
                search_value=bo_search_value,
            )
            if not bo_id:
                self.logger.warning(
                    "Workspace -> '%s' will not be connected to Salesforce as the Business Object ID couldn't be determined (type -> '%s', search_field -> '%s', search_value -> '%s')!",
                    name,
                    bo_type,
                    bo_search_field,
                    bo_search_value,
                )
                continue
        # end if external_system_type == "Salesforce" and not bo_id

        # For Guidewire we try to determine the actual business object ID (technical ID) if it is
        # not specified in the payload (but instead a search field and search value):
        if external_system_type == "Guidewire" and not bo_id:
            bo_id = self.get_guidewire_business_object(
                external_system=external_system,
                object_type=bo_type,
                search_field=bo_search_field,
                search_value=bo_search_value,
            )
            if not bo_id:
                self.logger.warning(
                    "Workspace -> '%s' will not be connected to Guidewire as the business object ID couldn't be determined (type -> '%s', search_field -> '%s', search_value -> '%s')!",
                    name,
                    bo_type,
                    bo_search_field,
                    bo_search_value,
                )
                continue
        # end if external_system_type == "Guidewire" and not bo_id

        self.logger.info(
            "Workspace -> '%s' will be connected with external system -> '%s' (%s) with (type -> '%s', ID -> '%s').",
            name,
            external_system_type,
            ext_system_id,
            bo_type,
            bo_id,
        )

        business_object["ext_system_id"] = ext_system_id
        business_object["bo_type"] = bo_type
        business_object["bo_id"] = bo_id

        business_object_list.append(business_object)

    return business_object_list

prepare_workspace_create_form(categories, template_id, ext_system_id=None, bo_type=None, bo_id=None, parent_workspace_node_id=None)

Prepare the category structure for the workspace creation.

Parameters:

Name Type Description Default
categories list

The categories list from the workspace payload.

required
template_id int

The workspace template ID.

required
ext_system_id str

The optional external system ID.

None
bo_type str

The optional business object type ID.

None
bo_id str

the business object ID.

None
parent_workspace_node_id int

The optional parent Workspace ID.

None

Returns:

Type Description
dict | None

dict | None: Category structure for workspace creation or None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="prepare_workspace_create_form")
def prepare_workspace_create_form(
    self,
    categories: list,
    template_id: int,
    ext_system_id: str | None = None,
    bo_type: str | None = None,
    bo_id: str | None = None,
    parent_workspace_node_id: int | None = None,
) -> dict | None:
    """Prepare the category structure for the workspace creation.

    Args:
        categories (list):
            The categories list from the workspace payload.
        template_id (int):
            The workspace template ID.
        ext_system_id (str, optional):
            The optional external system ID.
        bo_type (str, optional):
            The optional business object type ID.
        bo_id (str, optional):
            the business object ID.
        parent_workspace_node_id (int, optional):
            The optional parent Workspace ID.

    Returns:
        dict | None:
            Category structure for workspace creation or None
            in case of an error.

    """

    category_create_data = {}

    response = self._otcs_frontend.get_workspace_create_form(
        template_id=template_id,
        external_system_id=ext_system_id,
        bo_type=bo_type,
        bo_id=bo_id,
        parent_id=parent_workspace_node_id,
    )
    if response is None or "forms" not in response:
        self.logger.error(
            "Failed to retrieve create information for template ID -> %s!",
            template_id,
        )
        return None

    self.logger.debug(
        "Successfully retrieved create information for template ID -> %d!",
        template_id,
    )

    # Process category information
    forms = response["forms"]

    categories_form = {}

    # Typically the the create workspace form delivers 4-5 forms:
    # 1. Form for System Attributes (has no role name)
    # 2. Form for Category Data (role name = "categories")
    # 3. Form for Classifications (role name = "classifications")
    # 4. Form for importing team members from another existing team
    # 5. Form for Microsoft Teams integration (role name = "microsoftteams")
    for form in forms:
        if "role_name" not in form:
            self.logger.debug("Found 'system attributes' form -> %s", str(form))
            continue
        match form["role_name"]:
            case "categories":
                categories_form = form
                self.logger.debug("Found 'categories' form -> %s", str(form))
                continue
            case "classifications":
                self.logger.debug("Found 'classifications' form -> %s", str(form))
                continue
            case "rmclassifications":
                self.logger.debug("Found 'RM classifications' form -> %s", str(form))
                continue
            case "importteam":
                self.logger.debug("Found 'import team participants' form -> %s", str(form))
                continue
            case "microsoftteams":
                self.logger.debug("Found 'microsoft teams' form -> %s", str(form))
                continue
            case _:
                # the remaining option is that this form is the system attributes form:
                self.logger.warning("Unknown form -> %s", str(form))
        # end match form["role_name"]:
    # end for form in forms:

    # We are just interested in the single category data set (role_name = "categories"):
    data = categories_form.get("data", None)

    if not data:
        self.logger.debug("No categories data found.")
        return category_create_data

    self.logger.debug("Categories data found -> %s", data)
    schema = categories_form["schema"]["properties"]
    self.logger.debug("Categories schema found -> %s", schema)
    # Loop over categories:
    for cat_id in data:  # these are the category IDs (dict keys):
        self.logger.debug("Category ID -> %s", cat_id)
        data_attributes = data[cat_id]
        self.logger.debug("Data attributes -> %s", data_attributes)
        schema_attributes = schema[cat_id]["properties"]
        self.logger.debug("Schema attributes -> %s", schema_attributes)
        cat_name = schema[cat_id]["title"]
        self.logger.debug("Category name -> %s", cat_name)
        # Loop over attributes:
        # * Sets with one (fixed) row have type = object
        # * Multi-value Sets with (multiple) rows have type = array and "properties" in "items" schema
        # * Multi-value attributes have also type = array but NO "properties" in "items" schema
        for attr_id in data_attributes:  # these a attribute IDs (dict keys)
            self.logger.debug("Attribute ID -> %s", attr_id)
            self.logger.debug("Attribute data -> %s", data_attributes[attr_id])
            self.logger.debug(
                "Attribute schema -> %s",
                str(schema_attributes[attr_id]),
            )
            attr_type = schema_attributes[attr_id]["type"]
            self.logger.debug("Attribute type -> %s", attr_type)
            if "title" not in schema_attributes[attr_id]:
                self.logger.debug("Attribute has no title in schema. Skipping...")
                continue

            #
            # Handle multi-line set:
            #
            if attr_type == "array" and ("properties" in schema_attributes[attr_id]["items"]):
                set_name = schema_attributes[attr_id]["title"]
                self.logger.debug("Multi-line set -> '%s'", set_name)
                set_data_attributes = data_attributes[attr_id]  # this is a list []
                if set_data_attributes is None:
                    self.logger.debug("Attribute has no value in data. Skipping...")
                    continue
                self.logger.debug("Set data attributes -> %s", set_data_attributes)
                set_schema_attributes = schema_attributes[attr_id]["items"]["properties"]
                self.logger.debug(
                    "Set schema attributes -> %s",
                    set_schema_attributes,
                )
                set_schema_max_rows = schema_attributes[attr_id]["items"]["maxItems"]
                self.logger.debug("Set schema max rows -> %s", set_schema_max_rows)
                set_data_max_rows = len(set_data_attributes)
                self.logger.debug("Set data max rows -> %s", set_data_max_rows)
                row = 1
                # it can happen that the payload contains more rows than the
                # initial rows in the set data structure. In this case we use
                # a copy of the data structure from row 0 as template...
                first_row = dict(set_data_attributes[0])
                # We don't know upfront how many rows of data we will find in payload
                # but we at max process the maxItems specified in the schema:
                while row <= set_schema_max_rows:
                    # Test if we have any payload for this row:
                    attribute = next(
                        (
                            item
                            for item in categories
                            if (
                                item["name"] == cat_name
                                and "set" in item  # not all items may have a "set" key
                                and item["set"] == set_name
                                and "row" in item  # not all items may have a "row" key
                                and item["row"] == row
                            )
                        ),
                        None,
                    )
                    # stop if there's no payload for the row:
                    if attribute is None:
                        self.logger.debug(
                            "No payload found for set -> %s, row -> %s",
                            set_name,
                            row,
                        )
                        # we assume that if there's no payload for row n there will be no payload for rows > n
                        # and break the while loop:
                        break
                    # do we need to create a new row in the data frame?
                    if row > set_data_max_rows:
                        # use the row we stored above to create a new empty row:
                        self.logger.debug(
                            "Found payload for row -> %s, we need a new data row for it",
                            row,
                        )
                        self.logger.debug(
                            "Adding an additional row -> %s to set data -> '%s'",
                            row,
                            set_name,
                        )
                        # add the empty dict to the list:
                        set_data_attributes.append(dict(first_row))
                        set_data_max_rows += 1
                    else:
                        self.logger.debug(
                            "Found payload for row -> %s %s we can store in existing data row",
                            row,
                            set_name,
                        )
                    # traverse all attributes in a single set row:
                    for set_attr_id in set_schema_attributes:
                        self.logger.debug(
                            "Set attribute ID -> %s (row -> %s)",
                            set_attr_id,
                            row,
                        )
                        self.logger.debug(
                            "Set attribute schema -> %s (row -> %s)",
                            set_schema_attributes[set_attr_id],
                            row,
                        )
                        set_attr_type = set_schema_attributes[set_attr_id]["type"]
                        self.logger.debug(
                            "Set attribute type -> %s (row -> %s)",
                            set_attr_type,
                            row,
                        )
                        set_attr_name = set_schema_attributes[set_attr_id]["title"]
                        self.logger.debug(
                            "Set attribute name -> %s (row -> %s)",
                            set_attr_name,
                            row,
                        )
                        # Lookup the attribute with the right category, set, attribute name, and row number in payload:
                        attribute = next(
                            (
                                item
                                for item in categories
                                if (
                                    item["name"] == cat_name
                                    and "set" in item  # not all items may have a "set" key
                                    and item["set"] == set_name
                                    and item["attribute"] == set_attr_name
                                    and "row" in item  # not all items may have a "row" key
                                    and item["row"] == row
                                )
                            ),
                            None,
                        )
                        if attribute is None:
                            self.logger.debug(
                                "Set -> '%s', attribute -> '%s', row -> %s not found in payload.",
                                set_name,
                                set_attr_name,
                                row,
                            )

                            # need to use row - 1 as index starts with 0 but payload rows start with 1
                            set_data_attributes[row - 1][set_attr_id] = ""
                        else:
                            if set_attr_type == "array" and "items" in set_schema_attributes[set_attr_id]:
                                set_attr_type = set_schema_attributes[set_attr_id]["items"].get(
                                    "type",
                                    set_attr_type,
                                )

                            self.logger.debug(
                                "Set -> '%s', attribute -> '%s', row -> %s found in payload, value -> '%s'",
                                set_name,
                                set_attr_name,
                                row,
                                attribute["value"],
                            )
                            set_data_attributes[row - 1][set_attr_id] = self.resolve_attribute_values(
                                attribute_name=set_attr_name,
                                attribute_type=set_attr_type,
                                attribute_values=attribute["value"],
                            )
                    row += 1  # continue the while loop with the next row
                # end while row <= set_schema_max_rows:
            # end if attr_type == "array" and ("properties" in schema_attributes[attr_id]["items"]):

            #
            # Handle single-line set:
            #
            elif attr_type == "object":
                set_name = schema_attributes[attr_id]["title"]
                self.logger.debug("Single-line set -> '%s'", set_name)
                set_data_attributes = data_attributes[attr_id]
                self.logger.debug("Set data attributes -> %s", set_data_attributes)

                set_schema_attributes = schema_attributes[attr_id]["properties"]
                self.logger.debug(
                    "Set schema attributes -> %s",
                    str(set_schema_attributes),
                )
                # Loop over set attributes:
                for set_attr_id in set_data_attributes:
                    self.logger.debug("Set attribute ID -> %s", set_attr_id)
                    self.logger.debug(
                        "Set attribute data -> %s",
                        str(set_data_attributes[set_attr_id]),
                    )
                    self.logger.debug(
                        "Set attribute schema -> %s",
                        str(set_schema_attributes[set_attr_id]),
                    )
                    set_attr_type = set_schema_attributes[set_attr_id]["type"]
                    self.logger.debug("Set attribute type -> %s", set_attr_type)
                    set_attr_name = set_schema_attributes[set_attr_id]["title"]
                    self.logger.debug("Set attribute name -> '%s'", set_attr_name)
                    # Lookup the attribute with the right category, set and attribute name in payload:
                    attribute = next(
                        (
                            item
                            for item in categories
                            if (
                                item["name"] == cat_name
                                and "set" in item  # not all items may have a "set" key
                                and item["set"] == set_name
                                and item["attribute"] == set_attr_name
                            )
                        ),
                        None,
                    )
                    if attribute is None:
                        self.logger.debug(
                            "Category -> '%s', set -> %s, attribute -> '%s' not found in payload.",
                            cat_name,
                            set_name,
                            set_attr_name,
                        )
                        set_data_attributes[set_attr_id] = ""
                    else:
                        self.logger.debug(
                            "Category -> '%s', set -> %s, attribute -> '%s' found in payload, value -> %s",
                            cat_name,
                            set_name,
                            set_attr_name,
                            attribute["value"],
                        )
                        # Resolve any special cases (e.g. user picker, group picker):
                        set_data_attributes[set_attr_id] = self.resolve_attribute_values(
                            attribute_name=set_attr_name,
                            attribute_type=set_attr_type,
                            attribute_values=attribute["value"],
                        )
                # end for set_attr_id in set_data_attributes:
            # end elif attr_type == "object":

            #
            # Handle plain attribute (not inside a set):
            #
            else:
                attr_name = schema_attributes[attr_id]["title"]
                self.logger.debug("Attribute name -> '%s'", attr_name)
                # Lookup the attribute with the right category and attribute name in payload:
                attribute = next(
                    (item for item in categories if (item["name"] == cat_name and item["attribute"] == attr_name)),
                    None,
                )

                if attr_type == "array" and "items" in schema_attributes[attr_id]:
                    attr_type = schema_attributes[attr_id]["items"].get("type", attr_type)

                if attribute is None:
                    self.logger.debug(
                        "Category -> '%s', attribute -> '%s' not found in payload.",
                        cat_name,
                        attr_name,
                    )
                    data_attributes[attr_id] = ""
                else:
                    self.logger.debug(
                        "Category -> '%s', attribute -> '%s' found in payload, value -> %s",
                        cat_name,
                        attr_name,
                        attribute["value"],
                    )
                    # Resolve any special cases (e.g. user picker, group picker):
                    data_attributes[attr_id] = self.resolve_attribute_values(
                        attribute_name=attr_name,
                        attribute_type=attr_type,
                        attribute_values=attribute["value"],
                    )
            # end else (plain attribute)
        # end for attr_data, attr_schema
        category_create_data[cat_id] = data_attributes
    # end for cat_data, cat_schema

    self.logger.debug("Category create data -> %s", category_create_data)

    return category_create_data

process_additional_access_role_members(section_name='additionalAccessRoleMemberships')

Process additional access role memberships we want to have in OTDS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'additionalAccessRoleMemberships'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_additional_access_role_members")
def process_additional_access_role_members(
    self,
    section_name: str = "additionalAccessRoleMemberships",
) -> bool:
    """Process additional access role memberships we want to have in OTDS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._additional_access_role_members:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for additional_access_role_member in self._additional_access_role_members:
        access_role = additional_access_role_member.get("access_role")
        if not access_role:
            self.logger.error(
                "Missing access role! Skipping additional role members...",
            )
            continue

        # Check if additional access role member has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not additional_access_role_member.get("enabled", True):
            self.logger.info(
                "Payload for additional member for access role -> '%s' is disabled. Skipping...",
                access_role,
            )
            continue

        if (
            ("user_name" not in additional_access_role_member)
            and ("group_name" not in additional_access_role_member)
            and ("partition_name" not in additional_access_role_member)
        ):
            self.logger.error(
                "Either group_name or user_name need to be specified! Skipping...",
            )
            success = False
            continue
        if "group_name" in additional_access_role_member:
            group_name = additional_access_role_member["group_name"]
            self.logger.info(
                "Adding group -> '%s' to access role -> '%s' in OTDS.",
                group_name,
                access_role,
            )
            response = self._otds.add_group_to_access_role(
                access_role=access_role,
                group=group_name,
            )
            if not response:
                self.logger.error(
                    "Failed to add group -> '%s' to access role -> '%s' in OTDS!",
                    group_name,
                    access_role,
                )
                success = False
        elif "user_name" in additional_access_role_member:
            user_name = additional_access_role_member["user_name"]
            self.logger.info(
                "Adding user -> '%s' to access role -> '%s' in OTDS.",
                user_name,
                access_role,
            )
            response = self._otds.add_user_to_access_role(
                access_role=access_role,
                user_id=user_name,
            )
            if not response:
                self.logger.error(
                    "Failed to add user -> '%s' to access role -> '%s' in OTDS!",
                    user_name,
                    access_role,
                )
                success = False
        elif "partition_name" in additional_access_role_member:
            partition_name = additional_access_role_member["partition_name"]
            self.logger.info(
                "Adding partition -> '%s' to access role -> '%s' in OTDS.",
                partition_name,
                access_role,
            )
            response = self._otds.add_partition_to_access_role(
                access_role=access_role,
                partition=partition_name,
            )
            if not response:
                self.logger.error(
                    "Failed to add partition -> '%s' to access role -> '%s' in OTDS!",
                    partition_name,
                    access_role,
                )
                success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._additional_access_role_members,
    )

    return success

process_additional_application_role_assignments(section_name='additionalApplicationRoleAssignments')

Process additional application role assignments we want to have in OTDS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'additionalApplicationRoleAssignments'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(
    attributes=OTEL_TRACING_ATTRIBUTES, name="process_additional_application_role_assignments"
)
def process_additional_application_role_assignments(
    self,
    section_name: str = "additionalApplicationRoleAssignments",
) -> bool:
    """Process additional application role assignments we want to have in OTDS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._additional_application_role_assignments:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for additional_role_assignment in self._additional_application_role_assignments:
        role = additional_role_assignment.get("role_name")
        role_parts = role.split("@", 1)
        role_name = role_parts[0]
        role_partition = role_parts[1] if len(role_parts) > 1 else "OAuthClients"

        if not role_name:
            self.logger.error(
                "Missing application role! Skipping additional role members...",
            )
            continue

        # Check if additional access role member has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not additional_role_assignment.get("enabled", True):
            self.logger.info(
                "Payload for additional assignment for application role -> '%s' is disabled. Skipping...",
                role_name,
            )
            continue

        if ("user_name" not in additional_role_assignment) and ("group_name" not in additional_role_assignment):
            self.logger.error(
                "Either group_name or user_name need to be specified for application role assignment! Skipping...",
            )
            success = False
            continue

        if "group_name" in additional_role_assignment:
            group = additional_role_assignment["group_name"]
            group_parts = group.split("@", 1)
            group_name = group_parts[0]
            group_partition = group_parts[1] if len(group_parts) > 1 else self._otcs.config()["partition"]

            self.logger.info(
                "Adding group -> '%s' in partition -> '%s' to application role -> '%s' in partition -> '%s'.",
                group_name,
                group_partition,
                role_name,
                role_partition,
            )

            response = self._otds.assign_group_to_application_role(
                group_id=group_name,
                group_partition=group_partition,
                role_name=role_name,
                role_partition=role_partition,
            )

            if not response:
                self.logger.error(
                    "Failed to assign group -> '%s' in partition -> '%s' to application role -> '%s' in partition -> '%s'!",
                    group_name,
                    group_partition,
                    role_name,
                    role_partition,
                )
                success = False

        elif "user_name" in additional_role_assignment:
            user = additional_role_assignment["user_name"]
            user_parts = user.split("@", 1)
            user_name = user_parts[0]
            user_partition = user_parts[1] if len(user_parts) > 1 else self._otcs.config()["partition"]

            self.logger.info(
                "Adding user -> '%s' in partition -> '%s' to application role -> '%s' in partition -> '%s'!",
                user_name,
                user_partition,
                role_name,
                role_partition,
            )

            response = self._otds.assign_user_to_application_role(
                user_id=user_name,
                user_partition=user_partition,
                role_name=role_name,
                role_partition=role_partition,
            )
            if not response:
                self.logger.error(
                    "Failed to assign user -> '%s' in partition -> '%s' to application role -> '%s' in partition -> '%s'!",
                    user_name,
                    user_partition,
                    role_name,
                    role_partition,
                )
                success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._additional_application_role_assignments,
    )

    return success

process_additional_group_members(section_name='additionalGroupMemberships')

Process additional groups memberships we want to have in OTDS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'additionalGroupMemberships'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_additional_group_members")
def process_additional_group_members(
    self,
    section_name: str = "additionalGroupMemberships",
) -> bool:
    """Process additional groups memberships we want to have in OTDS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._additional_group_members:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for additional_group_member in self._additional_group_members:
        parent_group = additional_group_member.get("parent_group")
        if not parent_group:
            self.logger.error("Missing parent group! Skipping...")
            continue

        # Check if additional group member has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not additional_group_member.get("enabled", True):
            self.logger.info(
                "Payload for additional group member with parent group -> '%s' is disabled. Skipping...",
                parent_group,
            )
            continue

        if ("user_name" not in additional_group_member) and ("group_name" not in additional_group_member):
            self.logger.error(
                "Either group name or user name need to be specified! Skipping additional group members...",
            )
            success = False
            continue
        if "group_name" in additional_group_member:
            group_name = additional_group_member["group_name"]
            self.logger.info(
                "Adding group -> '%s' to parent group -> '%s' in OTDS.",
                group_name,
                parent_group,
            )
            response = self._otds.add_group_to_parent_group(
                group=group_name,
                parent_group=parent_group,
            )
            if not response:
                self.logger.error(
                    "Failed to add group -> '%s' to parent group -> '%s' in OTDS!",
                    group_name,
                    parent_group,
                )
                success = False
        elif "user_name" in additional_group_member:
            user_name = additional_group_member["user_name"]
            self.logger.info(
                "Adding user -> '%s' to group -> '%s' in OTDS.",
                user_name,
                parent_group,
            )
            response = self._otds.add_user_to_group(
                user=user_name,
                group=parent_group,
            )
            if not response:
                self.logger.error(
                    "Failed to add user -> '%s' to group -> '%s' in OTDS!",
                    user_name,
                    parent_group,
                )
                success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._additional_group_members,
    )

    return success

process_admin_settings(admin_settings, section_name='adminSettings')

Process admin settings in payload and import them to Content Server.

The payload section is a list of dicts with these items:
{
    enabled: True or False to enable or disable the payload item
    filename: The filename of the XML file with admin settings.
              It needs to be the plain filename like "admin.xml".
              The files reside inside the container in /settings root
              directory. They are placed there by the Terraform automation
              and are taken from the ./settings/payload directory.
    description: Some description about the purpose of the settings.
                 Just for information and optional.
}

Parameters:

Name Type Description Default
admin_settings list

List of admin settings. We need this parameter as we process two different lists.

required
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'adminSettings'

Returns:

Name Type Description
bool bool

True, if a restart of the OTCS pods is required. False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_admin_settings")
def process_admin_settings(
    self,
    admin_settings: list,
    section_name: str = "adminSettings",
) -> bool:
    """Process admin settings in payload and import them to Content Server.

        The payload section is a list of dicts with these items:
        {
            enabled: True or False to enable or disable the payload item
            filename: The filename of the XML file with admin settings.
                      It needs to be the plain filename like "admin.xml".
                      The files reside inside the container in /settings root
                      directory. They are placed there by the Terraform automation
                      and are taken from the ./settings/payload directory.
            description: Some description about the purpose of the settings.
                         Just for information and optional.
        }

    Args:
        admin_settings (list):
            List of admin settings. We need this parameter
            as we process two different lists.
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if a restart of the OTCS pods is required. False otherwise.

    """

    if not admin_settings:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return False  # important to return False here as otherwise we are triggering a restart of services!!

    # If this payload section has been processed successfully before we
    # can return False and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return False  # important to return False here as otherwise we are triggering a restart of services!!

    restart_required: bool = False
    success: bool = True

    for admin_setting in admin_settings:
        # Sanity checks:
        if "filename" not in admin_setting:
            self.logger.error(
                "Filename is missing. Skipping to next admin setting...",
            )
            continue
        filename = admin_setting["filename"]

        # Check if admin setting has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not admin_setting.get("enabled", True):
            self.logger.info(
                "Payload for setting file -> '%s' is disabled. Skipping...",
                filename,
            )
            continue

        settings_file = self._custom_settings_dir + filename
        if os.path.exists(settings_file):
            description = admin_setting.get("description")
            self.logger.info(
                "Processing admin settings from file -> '%s'%s...",
                filename,
                " ({})".format(description) if description else "",
            )

            # Read the config file:
            with open(settings_file, encoding="utf-8") as file:
                file_content = file.read()

            self.logger.debug(
                "Replace Placeholder -> '%s' in file -> %s",
                self._placeholder_values,
                file_content,
            )

            file_content = self.replace_placeholders(content=file_content)

            # Write the updated config file:
            tmpfile = os.path.join(tempfile.gettempdir(), os.path.basename(settings_file))
            with open(tmpfile, "w", encoding="utf-8") as file:
                file.write(file_content)

            response = self._otcs.apply_config(xml_file_path=tmpfile)
            if response and response["results"]["data"]["restart"]:
                self.logger.info(
                    "A restart of the Content Server service is required.",
                )
                restart_required = True

                if admin_setting.get("restart", False):
                    self.logger.info(
                        "Immediate restart requested - restart of OTCS services...",
                    )
                    # Restart OTCS frontend and backend pods:
                    self._otcs_restart_callback(
                        backend=self._otcs_backend,
                        frontend=self._otcs_frontend,
                    )

                    restart_required = False

        else:
            self.logger.error(
                "Admin settings file -> '%s' not found!",
                settings_file,
            )
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=admin_settings,
    )

    return restart_required

process_application_roles(section_name='applicationRoles')

Process OTDS application rolesin payload and create them in OTDS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'applicationRoles'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_application_roles")
def process_application_roles(self, section_name: str = "applicationRoles") -> bool:
    """Process OTDS application rolesin payload and create them in OTDS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._application_roles:
        self.logger.info("Payload section -> %s is empty. Skipping...", section_name)
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for role in self._application_roles:
        role_name = role.get("name")
        if not role_name:
            self.logger.error("Application role does not have a name in payload! Skipping...")
            success = False
            continue

        # Check if application role has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not role.get("enabled", True):
            self.logger.info(
                "Payload for application roles -> '%s' is disabled. Skipping...",
                role_name,
            )
            continue

        role_description = role.get("description")
        role_partition = role.get("partition", "OAuthClients")

        # Check if application role does already exist
        # (in an attempt to make the code idem-potent)
        self.logger.info(
            "Check if application role -> '%s' does already exist in partition -> '%s'...",
            role_name,
            role_partition,
        )
        response = self._otds.get_application_role(
            name=role,
            partition=role_partition,
            show_error=False,
        )
        if response:
            self.logger.info(
                "Application role -> '%s' does already exist in partition -> '%s'. Skipping...",
                role_name,
                role_partition,
            )
            continue
        self.logger.info(
            "Application role -> '%s' does not exist in partition -> '%s'. Creating...", role_name, role_partition
        )

        response = self._otds.add_application_role(
            name=role_name,
            partition_id=role_partition,
            description=role_description,
            values=role.get("values", None),
            custom_attributes=role.get("custom_attributes", None),
        )
        if response:
            self.logger.info(
                "Successfully added OTDS Application role -> '%s' to partition -> '%s'.", role_name, role_partition
            )
        else:
            self.logger.error(
                "Failed to add OTDS Application role -> '%s' to partition -> '%s'!", role_name, role_partition
            )
            success = False
            continue

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._application_roles,
    )

    return success

process_appworks_configurations(section_name='appworks')

Process the configurations for AppWorks projects.

This method is responsible for setting up the necessary configurations for AppWorks projects. If the payload contains a appworks section, it will execute the corresponding actions to process and apply the custom configuration.

This includes: * Resource configuration - add the OTCS user partition to the OTDS access role for the AppWorks organization - add the OTCS admin partition to the OTDS access role for the AppWorks organization - add an OTAWP license * Solution configuration - create the AppWorks artifacts

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'appworks'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_appworks_configurations")
def process_appworks_configurations(self, section_name: str = "appworks") -> bool:
    """Process the configurations for AppWorks projects.

    This method is responsible for setting up the necessary configurations for AppWorks projects.
    If the payload contains a `appworks` section, it will execute the corresponding actions
    to process and apply the custom configuration.

    This includes:
    * Resource configuration
        - add the OTCS user partition to the OTDS access role for the AppWorks organization
        - add the OTCS admin partition to the OTDS access role for the AppWorks organization
        - add an OTAWP license
    * Solution configuration
        - create the AppWorks artifacts

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._appworks_configurations:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    #
    # 1: Loop to create the required OTDS resources for the AppWorks organization:
    #
    for appworks_configuration in self._appworks_configurations:
        organization = appworks_configuration.get("organization", None)
        if not organization:
            self.logger.error("AppWorks configuration is missing the organization name! Skipping...")
            success = False
            continue

        self._log_header_callback(
            text="Process AppWorks resource configuration for '{}'".format(organization), char="-"
        )

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not appworks_configuration.get("enabled", True):
            self.logger.info(
                "Payload for AppWorks configuration -> '%s' is disabled. Skipping...",
                organization,
            )
            continue

        if not appworks_configuration.get("resource_config", False):
            self.logger.info("AppWorks resource configuration is disabled for -> '%s'. Skipping...", organization)
            continue

        access_role_name = "Access to " + organization

        # make sure code is idempotent and only try to add ressource if it doesn't exist already:
        awp_resource = self._otds.get_resource(organization)
        if not awp_resource:
            self.logger.info(
                "OTDS resource -> '%s' for AppWorks Platform does not yet exist. Creating...",
                organization,
            )
            awp_resource = self._otds.add_resource(
                name=organization,
                description="AppWorks Platform - {}".format(organization),
                display_name="AppWorks Platform - {}".format(organization),
                additional_payload=OTAWP.resource_payload(
                    org_name=organization,
                    username=self._otawp.username(),
                    password=self._otawp.password(),
                ),
            )
        else:
            self.logger.info(
                "OTDS resource -> '%s' for AppWorks Platform does already exist.",
                organization,
            )

        awp_resource_id = awp_resource["resourceID"]

        self.logger.info(
            "AppWorks Platform organization -> '%s' got OTDS resource ID -> %s",
            organization,
            awp_resource_id,
        )

        # Loop to wait for OTCS to create its OTDS user partition:
        otcs_partition = self._otds.get_partition(
            self._otcs.partition_name(),
            show_error=False,
        )
        while otcs_partition is None:
            self.logger.warning(
                "OTDS user partition -> '%s' for Content Server does not exist yet. Waiting...",
                self._otcs.partition_name(),
            )

            time.sleep(30)
            otcs_partition = self._otds.get_partition(
                name=self._otcs.partition_name(),
                show_error=False,
            )

        # Add the OTDS user partition for OTCS to the AppWorks Platform Access Role in OTDS.
        # This will effectvely sync all OTCS users with AppWorks Platform:
        self._otds.add_partition_to_access_role(
            access_role=access_role_name,
            partition=self._otcs.partition_name(),
        )

        # Add the OTDS admin partition to the AppWorks Platform Access Role in OTDS.
        self._otds.add_partition_to_access_role(
            access_role=access_role_name,
            partition=self._otds.admin_partition_name(),
        )

        # Set Group inclusion for Access Role for OTAWP to "True":
        self._otds.update_access_role_attributes(
            name=access_role_name,
            attribute_list=[{"name": "pushAllGroups", "values": ["True"]}],
        )

        # Add ResourceID User to 'otdsadmins' group to allow push
        self._otds.add_user_to_group(
            user=str(awp_resource_id) + "@otds.admin",
            group="otdsadmins@otds.admin",
        )

        # Allow impersonation for all users:
        self._otds.impersonate_resource(resource_name=organization)

        # Editing configmap
        config_map = self._k8s.get_config_map(config_map_name=self._otawp.config_map_name())
        if not config_map:
            self.logger.error(
                "Failed to retrieve AppWorks Kubernetes config map -> '%s'!",
                self._otawp.config_map_name(),
            )
            success = False
        else:
            self.logger.info(
                "Update Kubernetes config map for AppWorks organization -> '%s' with OTDS resource IDs...",
                organization,
            )
            solution = yaml.safe_load(
                config_map.data[organization + ".yaml"],
            )

            solution["platform"]["organizations"][organization]["otds"]["resourceId"] = awp_resource_id
            solution["platform"]["organizations"][organization]["database"]["connections"]["sysConnection"][
                "connectionString"
            ] = "jdbc:postgresql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}"

            solution["platform"]["organizations"][organization]["database"]["connections"]["sysConnection"][
                "database"
            ] = "${DATABASE_NAME}"

            solution["platform"]["organizations"][organization]["database"]["connections"]["sysConnection"][
                "password"
            ] = "${DATABASE_PASSWORD}"

            solution["platform"]["organizations"][organization]["content"]["ContentServer"]["contentServerUrl"] = (
                self._otcs.cs_url()
            )
            solution["platform"]["organizations"][organization]["content"]["ContentServer"][
                "contentServerSupportDirectoryUrl"
            ] = self._otcs.cs_support_url()
            solution["platform"]["organizations"][organization]["content"]["ContentServer"]["otdsResourceId"] = (
                self._otcs.resource_id()
            )
            solution["platform"]["organizations"][organization]["authenticators"]["OTDSAuth"]["publicLoginUrl"] = (
                self._otds.base_url() + "/otdsws/login"
            )

            config_map.data[organization + ".yaml"] = yaml.dump(solution)
            result = self._k8s.replace_config_map(
                config_map_name=self._otawp.config_map_name(),
                config_map_data=config_map.data,
            )
            if result:
                self.logger.info(
                    "Successfully updated AppWorks solution YAML for organization -> '%s'.",
                    organization,
                )
            else:
                self.logger.error(
                    "Failed to update AppWorks solution YAML for organization -> '%s'!",
                    organization,
                )
            self.logger.debug(
                "Solution YAML for Appworks organization -> '%s': %s",
                organization,
                solution,
            )
        # Add SPS license for OTAWP
        license_name = self._otawp.product_name()
        product_name = self._otawp.product_name() + "_" + organization.upper()
        product_description = self._otawp.product_name() + organization
        if os.path.isfile(self._otawp.license_file()):
            self.logger.info(
                "Found OTAWP license file -> '%s', assiging it to OTDS resource -> '%s'...",
                self._otawp.license_file(),
                organization,
            )

            otawp_license = self._otds.add_license_to_resource(
                path_to_license_file=self._otawp.license_file(),
                product_name=product_name,
                product_description=product_description,
                resource_id=awp_resource["resourceID"],
            )
            if not otawp_license:
                self.logger.error(
                    "Couldn't apply license -> '%s' for product -> '%s' to OTDS resource -> '%s'!",
                    self._otawp.license_file(),
                    product_name,
                    awp_resource["resourceID"],
                )
            else:
                self.logger.info(
                    "Successfully applied license -> '%s' for product -> '%s' to OTDS resource -> '%s'.",
                    self._otawp.license_file(),
                    product_name,
                    awp_resource["resourceID"],
                )

            # Assign AppWorks license to Content Server Members Partiton and otds.admin:
            for partition_name in ["otds.admin", self._otcs.partition_name()]:
                if self._otds.is_partition_licensed(
                    partition_name=partition_name,
                    resource_id=awp_resource["resourceID"],
                    license_feature="USERS",
                    license_name=license_name,
                ):
                    self.logger.info(
                        "Partition -> '%s' is already licensed for -> '%s' (%s).",
                        partition_name,
                        product_name,
                        "USERS",
                    )
                else:
                    assigned_license = self._otds.assign_partition_to_license(
                        partition_name=partition_name,
                        resource_id=awp_resource["resourceID"],
                        license_feature="USERS",
                        license_name=license_name,
                    )
                    if not assigned_license:
                        self.logger.error(
                            "Partition -> '%s' could not be assigned to license -> '%s' (%s)!",
                            partition_name,
                            product_name,
                            "USERS",
                        )
                        success = False
                    else:
                        self.logger.info(
                            "Partition -> '%s' successfully assigned to license -> '%s' (%s).",
                            partition_name,
                            product_name,
                            "USERS",
                        )
            # end for partition_name in ["otds.admin", self._otcs.partition_name()]:
        # end if os.path.isfile(self._otawp.license_file()):

        self.logger.info("Restart AppWorks Kubernetes stateful set -> '%s'...", self._otawp.hostname())

        self._k8s.restart_stateful_set(sts_name=self._otawp.hostname(), force=True, wait=True)

        self._otawp.set_organization(organization)
        otawp_cookie = self._otawp.authenticate(revalidate=True)
        if not otawp_cookie:
            self.logger.error(
                "Authentication at AppWorks failed! Cannot proceed with processing of AppWorks configuration -> '%s'!",
                organization,
            )
            success = False
            continue
        self._otawp.create_cws_config(
            partition=self._otcs.partition_name(), resource_name=organization, otcs_url=self._otcs.cs_url()
        )
        self._otawp.assign_role_to_user(
            organization=organization, user_name=self._otawp.username(), role_name="Developer"
        )
    # end for appworks_configuration in self._appworks_configurations:

    #
    # 2: Loop to create the AppWorks workspaces, projects, and entities:
    #
    for appworks_configuration in self._appworks_configurations:
        organization = appworks_configuration.get("organization", None)
        if not organization:
            self.logger.error("AppWorks configuration is missing the organization name! Skipping...")
            success = False
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not appworks_configuration.get("enabled", True):
            self.logger.info(
                "Payload for AppWorks configuration -> '%s' is disabled. Skipping...",
                organization,
            )
            continue

        self._log_header_callback(
            text="Process AppWorks workspaces, project, entities for '{}'".format(organization), char="-"
        )

        self._otawp.set_organization(organization)
        otawp_cookie = self._otawp.authenticate(revalidate=True)
        if not otawp_cookie:
            self.logger.error(
                "Authentication at AppWorks failed. Cannot proceed with processing of AppWorks configutation -> '%s'!",
                organization,
            )
            success = False
            continue

        if "workspaces" not in appworks_configuration:
            self.logger.warning(
                "No workspace information in AppWorks configuration -> '%s'. Skipping...", organization
            )
            continue

        for workspace in appworks_configuration["workspaces"]:
            workspace_id = workspace.get("workspace_id", None)
            workspace_name = workspace.get("name", None)
            workspace_path = workspace.get("path", None)
            if not workspace_id or not workspace_name or not workspace_path:
                self.logger.error(
                    "AppWorks workspace configuration for -> '%s'%s requires 'workspace_id', 'name', and 'path' settings! Skipping...",
                    organization,
                    " (workspace name -> {})".format(workspace_name) if workspace_name else "",
                )
                success = False
                continue

            response, created = self._otawp.create_workspace(
                workspace_name=workspace_name, workspace_id=workspace_id
            )
            if not response:
                self.logger.info("Failed to create workspace -> '%s' (%s)!", workspace_name, workspace_id)
                success = False
                continue

            if created:
                self.logger.info("Setup new workspace -> '%s' (%s)...", workspace_name, workspace_id)
                response = self._otawp.sync_workspace(
                    workspace_name=workspace_name,
                    workspace_id=workspace_id,
                )
                if not response:
                    self.logger.error("Failed to synchronize workspace -> '%s' (%s)!", workspace_name, workspace_id)
                    success = False
                    continue
                self.logger.info(
                    "Copy projects artifacts to workspace -> '%s' (%s) in AppWorks pod -> '%s'...",
                    workspace_name,
                    workspace_id,
                    "appworks-0",
                )
                self._k8s.exec_pod_command(
                    pod_name="appworks-0",
                    command=[
                        "/bin/sh",
                        "-c",
                        f'cp -r "{workspace_path}/"* "/opt/appworks/cluster/shared/cws/sync/{organization}/{workspace_name}"',
                    ],
                    timeout=600,
                )
                self.logger.info(
                    "Copying of projects artifacts to workspace -> '%s' (%s) completed.",
                    workspace_name,
                    workspace_id,
                )
            self.logger.info("Re-sync existing workspace -> '%s' (%s)...", workspace_name, workspace_id)
            response = self._otawp.sync_workspace(
                workspace_name=workspace_name,
                workspace_id=workspace_id,
            )
            if not response:
                self.logger.error("Failed to synchronize workspace -> '%s' (%s)!", workspace_name, workspace_id)
                success = False
                continue

            if "projects" in workspace:
                for project in workspace["projects"]:
                    if project.get("name") and project.get("documentId"):
                        if not self._otawp.publish_project(
                            workspace_name=workspace_name,
                            workspace_id=workspace_id,
                            project_name=project.get("name"),
                            project_id=project.get("documentId"),
                        ):
                            success = False
                            continue
                    else:
                        self.logger.error(
                            "Skipping project -> '%s' due to missing required project fields 'name' or 'documentId'!",
                            project.get("name"),
                        )
                        success = False
                        continue
                # end for project in workspace["projects"]:
            # end if "projects" in workspace
        # end for workspace in value["cws"]["workspaces"]

        # Process the entities in the payload:
        entities = appworks_configuration.get("entities", [])
        if entities:
            self._log_header_callback(
                text="Process AppWorks entities for organization -> '{}'".format(organization), char="-"
            )
            for entity in entities:
                if not self.process_appworks_entity(entity=entity):
                    success = False
            self.logger.info("Entity processing completed for organization -> '%s'.", organization)
    # end for appworks_configuration in self._appworks_configurations:

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._appworks_configurations,
    )

    return success

process_appworks_entity(entity)

Process a single AppWorks entity payload.

Parameters:

Name Type Description Default
entity dict

Entity payload. Should have a selection of the following keys: * type * name * description * status * prefix * category * priority * customer * case_type * ...

required

Returns:

Name Type Description
bool bool

True = success, False = failure.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_appworks_entity")
def process_appworks_entity(self, entity: dict) -> bool:
    """Process a single AppWorks entity payload.

    Args:
        entity (dict):
            Entity payload.
            Should have a selection of the following keys:
            * type
            * name
            * description
            * status
            * prefix
            * category
            * priority
            * customer
            * case_type
            * ...

    Returns:
        bool: True = success, False = failure.

    """

    entity_type = entity.get("type")
    if not entity_type:
        return False

    match entity_type:
        case "category":
            cat = self._otawp.get_category_by_name(name=entity.get("name"))
            if cat:
                cat_id = self._otawp.get_entity_value(entity=cat, key="id")
                self.logger.info(
                    "Category -> '%s' (%s) does already exist. Skipping...", entity.get("name"), str(cat_id)
                )
                self.logger.debug("Category -> %s", str(cat))
            else:
                response = self._otawp.create_category(
                    case_prefix=entity.get("prefix"),
                    name=entity.get("name"),
                    description=entity.get("description", ""),
                    status=entity.get("status", 1),
                )
                if not response or "Identity" not in response:
                    self.logger.error("Failed to create category -> '%s'!", entity.get("name"))
                    return False
                cat_id = response["Identity"].get("Id")
                self.logger.info(
                    "Successfully created category -> '%s' (%s).",
                    entity.get("name"),
                    cat_id,
                )
                self.logger.debug("Response -> %s", str(response))
            if "sub_entities" in entity:
                for sub_entity in entity["sub_entities"]:
                    if sub_entity["type"] != "subCategory":
                        self.logger.warning(
                            "Found a category sub-entities with wrong type -> '%s'!", sub_entity["type"]
                        )
                        continue
                    response = self._otawp.create_sub_category(
                        parent_id=cat_id,
                        name=sub_entity.get("name"),
                        description=sub_entity.get("description", ""),
                        status=sub_entity.get("status", 1),
                    )
                    if not response or "Identity" not in response:
                        self.logger.error("Failed to create sub-category -> '%s'!", sub_entity.get("name"))
                        return False
                    self.logger.info(
                        "Successfully created sub-category -> '%s' (%s) in parent category -> '%s' (%s).",
                        sub_entity.get("name"),
                        response["Identity"].get("Id"),
                        entity.get("name"),
                        cat_id,
                    )
                    self.logger.debug("Response -> %s", str(response))
            return True
        case "priority":
            priority = self._otawp.get_priority_by_name(name=entity.get("name"))
            if priority:
                priority_id = self._otawp.get_entity_value(entity=priority, key="id")
                (
                    self.logger.info(
                        "Priority -> '%s' (%s) does already exist. Skipping...",
                        entity.get("name"),
                        str(priority_id),
                    )
                )
                return True
            response = self._otawp.create_priority(
                name=entity.get("name"),
                description=entity.get("description", ""),
                status=entity.get("status", 1),
            )
            if not response or "Identity" not in response:
                self.logger.error("Failed to create priority -> '%s'!", entity.get("name"))
                return False
            self.logger.info(
                "Successfully created priority -> '%s' (%s).", entity.get("name"), response["Identity"].get("Id")
            )
            self.logger.debug("Response -> %s", str(response))
            return True
        case "caseType":
            case_type = self._otawp.get_case_type_by_name(name=entity.get("name"))
            if case_type:
                case_type_id = self._otawp.get_entity_value(entity=case_type, key="id")
                self.logger.info(
                    "Case type -> '%s' (%s) does already exist. Skipping...", entity.get("name"), str(case_type_id)
                )
                return True
            response = self._otawp.create_case_type(
                name=entity.get("name"),
                description=entity.get("description", ""),
                status=entity.get("status", 1),
            )
            if not response or "Identity" not in response:
                self.logger.error("Failed to case type -> '%s'!", entity.get("name"))
                return False
            self.logger.info(
                "Successfully created case type -> '%s' (%s).", entity.get("name"), response["Identity"].get("Id")
            )
            self.logger.debug("Response -> %s", str(response))
            return True
        case "customer":
            customer = self._otawp.get_customer_by_name(name=entity.get("name"))
            if customer:
                customer_id = self._otawp.get_entity_value(entity=customer, key="id")
                self.logger.info(
                    "Customer -> '%s' (%s) does already exist. Skipping...", entity.get("name"), str(customer_id)
                )
                return True
            response = self._otawp.create_customer(
                customer_name=entity.get("name"),
                legal_business_name=entity.get("legal_business_name", ""),
                trading_name=entity.get("trading_name", ""),
            )
            if not response or "Identity" not in response:
                self.logger.error("Failed to create customer -> '%s'!", entity.get("name"))
                return False
            self.logger.info(
                "Successfully created customer -> '%s' (%s).", entity.get("name"), response["Identity"].get("Id")
            )
            self.logger.debug("Response -> %s", str(response))
            return True
        case "case":
            if "subject" not in entity:
                self.logger.error("Cannot create a case without a subject!")
                return False

            category_name = entity.get("category")
            if category_name:
                category = self._otawp.get_category_by_name(name=category_name)
                category_id = self._otawp.get_entity_value(entity=category, key="id")
                if not category_id:
                    self.logger.error(
                        "Cannot find case category -> '%s' to create case -> '%s'!",
                        category_name,
                        entity["subject"],
                    )
                    return False
            else:
                self.logger.warning(
                    "Case entity -> '%s' does not have a category specified in its payload!", entity["subject"]
                )
                category_id = None

            sub_category_name = entity.get("sub_category")
            if category_id and sub_category_name:
                sub_category_id = self._otawp.get_sub_category_id(name=sub_category_name, parent_id=category_id)
            else:
                sub_category_id = None

            priority_name = entity.get("priority")
            if priority_name:
                priority = self._otawp.get_priority_by_name(name=priority_name)
                priority_id = self._otawp.get_entity_value(entity=priority, key="id")
                if not priority_id:
                    self.logger.error(
                        "Cannot find case priority -> '%s' to create case -> '%s'!",
                        priority_name,
                        entity["subject"],
                    )
                    return False
            else:
                self.logger.warning(
                    "Case entity -> '%s' does not have a priority specified in its payload!", entity["subject"]
                )
                priority_id = None

            case_type_name = entity.get("case_type")
            if case_type_name:
                case_type = self._otawp.get_case_type_by_name(name=case_type_name)
                case_type_id = self._otawp.get_entity_value(entity=case_type, key="id")
                if not case_type_id:
                    self.logger.error(
                        "Cannot find case type -> '%s' to create case -> '%s'!",
                        case_type_name,
                        entity["subject"],
                    )
                    return False
            else:
                self.logger.warning(
                    "Case entity -> '%s' does not have a case type specified in its payload!", entity["subject"]
                )
                case_type_id = None

            customer_name = entity.get("customer")
            if customer_name:
                customer = self._otawp.get_customer_by_name(name=customer_name)
                customer_id = self._otawp.get_entity_value(entity=customer, key="id")
                if not customer_id:
                    self.logger.error(
                        "Cannot find customer -> '%s' to create case -> '%s'!",
                        customer_name,
                        entity["subject"],
                    )
                    return False
            else:
                self.logger.warning(
                    "Case entity -> '%s' does not have a customer specified in its payload!", entity["subject"]
                )
                customer_id = None

            response = self._otawp.create_case(
                subject=entity.get("subject"),
                description=entity.get("description", ""),
                loan_amount=entity.get("loan_amount", 1),
                loan_duration_in_months=entity.get("loan_duration_in_month", 2),
                category_id=category_id,
                sub_category_id=sub_category_id,
                priority_id=priority_id,
                case_type_id=case_type_id,
                customer_id=customer_id,
            )
            if not response:
                self.logger.error("Failed to create case with subject -> '%s'!", entity.get("subject"))
                return False
            self.logger.info(
                "Successfully created case with subject -> '%s'%s.",
                entity.get("subject"),
                " for customer with ID -> '{}'".format(customer_id) if customer_id else "",
            )
            self.logger.debug("Response -> %s", str(response))
            return True
        case _:
            self.logger.error("Illegal entity type -> '%s'!", entity_type)
            return False

    return False

process_assignments(section_name='assignments')

Process assignments and assign items (such as workspaces and items with nicknames) to users or groups.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'assignments'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_assignments")
def process_assignments(self, section_name: str = "assignments") -> bool:
    """Process assignments and assign items (such as workspaces and items with nicknames) to users or groups.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise

    """

    if not self._assignments:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for assignment in self._assignments:
        # Sanity check: we need a subject - it's mandatory:
        subject = assignment.get("subject")
        if not subject:
            self.logger.error("Assignment needs a subject! Skipping assignment...")
            success = False
            continue

        # Check if assignment has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not assignment.get("enabled", True):
            self.logger.info(
                "Payload for assignment -> '%s' is disabled. Skipping...",
                subject,
            )
            continue

        # instruction is optional but we give a warning if they are missing:
        instruction = assignment.get("instruction", "")
        if not instruction:
            self.logger.warning(
                "Assignment -> '%s' should have an instruction!",
                subject,
            )

        # Sanity check: we either need users or groups (or both):
        if "groups" not in assignment and "users" not in assignment:
            self.logger.error(
                "Assignment -> '%s' needs groups or users! Skipping assignment...",
                subject,
            )
            success = False
            continue

        # Check if a workspace is specified for the assignment and check it does exist:
        if assignment.get("workspace"):
            workspace = next(
                (item for item in self._workspaces if item["id"] == assignment["workspace"]),
                None,
            )
            if not workspace:
                self.logger.error(
                    "Assignment -> '%s' has specified a not existing workspace -> %s! Skipping assignment...",
                    subject,
                    assignment["workspace"],
                )
                success = False
                continue
            node_id = self.determine_workspace_id(workspace=workspace)
            if not node_id:
                self.logger.error(
                    "Assignment -> '%s' has specified a not existing workspace -> %s! Skipping assignment...",
                    subject,
                    assignment["workspace"],
                )
                success = False
                continue
        # If we don't have a workspace then check if a nickname is specified for the assignment:
        elif "nickname" in assignment:
            response = self._otcs.get_node_from_nickname(
                nickname=assignment["nickname"],
            )
            node_id = self._otcs.get_result_value(response=response, key="id")
            if not node_id:
                # if response == None:
                self.logger.error(
                    "Assignment item with nickname -> '%s' not found",
                    assignment["nickname"],
                )
                success = False
                continue
        else:
            self.logger.error(
                "Assignment -> '%s' needs a workspace or nickname! Skipping assignment...",
                subject,
            )
            success = False
            continue

        assignees = []

        if "groups" in assignment:
            group_assignees = assignment["groups"]
            for group_assignee in group_assignees:
                # find the group in the group list
                group = next(
                    (item for item in self._groups if item["name"] == group_assignee),
                    None,
                )
                if not group:
                    self.logger.error(
                        "Assignment group -> '%s' does not exist! Skipping group...",
                        group_assignee,
                    )
                    success = False
                    continue
                if "id" not in group:
                    self.logger.error(
                        "Assignment group -> '%s' does not have an ID. Skipping group...",
                        group_assignee,
                    )
                    success = False
                    continue
                group_id = group["id"]
                # add the group ID to the assignee list:
                assignees.append(group_id)

        if "users" in assignment:
            user_assignees = assignment["users"]
            for user_assignee in user_assignees:
                # find the user in the user list
                user = next(
                    (item for item in self._users if item["name"] == user_assignee),
                    None,
                )
                if not user:
                    self.logger.error(
                        "Assignment user -> '%s' does not exist! Skipping user...",
                        user_assignee,
                    )
                    success = False
                    continue
                if "id" not in user:
                    self.logger.error(
                        "Assignment user -> '%s' does not have an ID. Skipping user...",
                        user_assignee,
                    )
                    success = False
                    continue
                user_id = user["id"]
                # add the group ID to the assignee list:
                assignees.append(user_id)

        if not assignees:
            self.logger.error(
                "Cannot add assignment -> '%s' for node ID -> %s because no assignee was found.",
                subject,
                node_id,
            )
            success = False
            continue

        response = self._otcs.assign_item_to_user_group(
            node_id=int(node_id),
            subject=subject,
            instruction=instruction,
            assignees=assignees,
        )
        if not response:
            self.logger.error(
                "Failed to add assignment -> '%s' for node ID -> %s with assignees -> %s!",
                subject,
                node_id,
                assignees,
            )
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._assignments,
    )

    return success

process_auth_handlers(section_name='authHandlers')

Process OTDS authorization handlers in payload and create them in OTDS.

An authorization handler defined the connection to an Identity Provider (IdP).

The payload section is a list of dicts with these items: { enabled (bool): True or False to enable or disable the payload item name (str): Name of the authorization handler. This is shown in the first column of the Auth Handler list in OTDS. description (str): Description of the handler. This is shown in the second column of the Auth Handler type (str): Type of the handler. Possible values are SALM, SAP, OAUTH priority (int): A numeric value to order different handlers in OTDS by priority active_by_default (bool): Whether to activate this handler for any request to the OTDS login page. If True, any login request to the OTDS login page will be redirected to the IdP. If false, the user has to select the provider on the login page. provider_name: The name of the identity provider. This should be a single word since it will be part of the metadata URL. This is what is shown as a button on the OTDS login page. auth_principal_attributes: Authentication principal attributes (list) nameid_format: Specifies which NameID format supported by the identity provider contains the desired user identifier. The value in this identifier must correspond to the value of the user attribute specified for the authentication principal attribute. saml_url: Required for SAML Authentication Handler. The URL for the IdP's federation metadata. otds_sp_endpoint: Used for SAML Authentication Handler. Specifies the service provider URL that will be used to identify OTDS to the identity provider. certificate_file: Required for SAP Authentication Handler (SAPSSOEXT). Fully qualified file name (with path) to the certificate file (URI) certificate_password: Required for SAP Authentication Handler (SAPSSOEXT). Password of the certificate file. client_id: Client ID. Required for OAUTH authentication handler. client_secret: Client Secret. Required for OAUTH authentication handler. authorization_endpoint: Required for OAUTH authentication handler. The URL to redirect the browser to for authentication. It is used to retrieve the authorization code or an OIDC id_token. token_endpoint: Used for OAUTH authentication handler. The URL from which to retrieve the access token. Not strictly required with OpenID Connect if using the implicit flow. scope_string: Used for OAUTH authentication handler. Space delimited scope values to send. Include 'openid' to use OpenID Connect. }

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'authHandlers'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_auth_handlers")
def process_auth_handlers(self, section_name: str = "authHandlers") -> bool:
    """Process OTDS authorization handlers in payload and create them in OTDS.

    An authorization handler defined the connection to an Identity Provider (IdP).

    The payload section is a list of dicts with these items:
    {
        enabled (bool):
            True or False to enable or disable the payload item
        name (str):
            Name of the authorization handler. This is shown in the first
            column of the Auth Handler list in OTDS.
        description (str):
            Description of the handler. This is shown in the second
            column of the Auth Handler
        type (str):
            Type of the handler. Possible values are SALM, SAP, OAUTH
        priority (int):
            A numeric value to order different handlers in OTDS by priority
        active_by_default (bool):
            Whether to activate this handler for any request to the
            OTDS login page. If True, any login request to the OTDS
            login page will be redirected to the IdP. If false, the
            user has to select the provider on the login page.
        provider_name:
            The name of the identity provider. This should be a single word
            since it will be part of the metadata URL. This is what is
            shown as a button on the OTDS login page.
        auth_principal_attributes:
            Authentication principal attributes (list)
        nameid_format:
            Specifies which NameID format supported by the identity provider
            contains the desired user identifier. The value in this identifier
            must correspond to the value of the user attribute specified for the
            authentication principal attribute.
        saml_url:
            Required for SAML Authentication Handler. The URL for the IdP's federation metadata.
        otds_sp_endpoint:
            Used for SAML Authentication Handler. Specifies the service provider URL that will
            be used to identify OTDS to the identity provider.
        certificate_file:
            Required for SAP Authentication Handler (SAPSSOEXT).
            Fully qualified file name (with path) to the certificate file (URI)
        certificate_password:
            Required for SAP Authentication Handler (SAPSSOEXT).
            Password of the certificate file.
        client_id:
            Client ID. Required for OAUTH authentication handler.
        client_secret:
            Client Secret. Required for OAUTH authentication handler.
        authorization_endpoint:
            Required for OAUTH authentication handler.
            The URL to redirect the browser to for authentication.
            It is used to retrieve the authorization code or an OIDC id_token.
        token_endpoint:
            Used for OAUTH authentication handler. The URL from which to retrieve the access token.
            Not strictly required with OpenID Connect if using the implicit flow.
        scope_string:
            Used for OAUTH authentication handler. Space delimited scope values to send.
            Include 'openid' to use OpenID Connect.
    }

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._auth_handlers:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for auth_handler in self._auth_handlers:
        handler_name = auth_handler.get("name")

        if not handler_name:
            self.logger.error("Auth handler does not have a name in payload. Skipping...")
            success = False
            continue

        # Check if Auth Handler does already exist (e.g. after a restart of
        # the customizer pod):
        if self._otds.get_auth_handler(name=handler_name, show_error=False):
            self.logger.info(
                "Auth handler -> '%s' does already exist. Skipping...",
                handler_name,
            )
            continue

        handler_description = auth_handler.get("description")

        # Check if auth handler has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not auth_handler.get("enabled", True):
            self.logger.info(
                "Payload for OTDS authorization handler -> '%s' is disabled. Skipping...",
                handler_name,
            )
            continue

        handler_scope = auth_handler.get("scope")
        if not handler_scope:
            # Make sure to pass None also if scope is empty string
            handler_scope = None

        handler_type = auth_handler.get("type")
        if not handler_type:
            self.logger.error(
                "OTDS authorization handler does not have a type in payload! Skipping...",
            )
            success = False
            continue

        priority = auth_handler.get("priority")
        active_by_default = auth_handler.get("active_by_default")
        if not active_by_default:
            active_by_default = False

        match handler_type:
            case "SAML":
                provider_name = auth_handler.get("provider_name")
                if not provider_name:
                    self.logger.error(
                        "SAML authorization handler needs a provider name in payload! Skipping...",
                    )
                    success = False
                    continue
                saml_url = auth_handler.get("saml_url")
                if not saml_url:
                    self.logger.error(
                        "SAML authorization handler needs a SAML URL in payload! Skipping...",
                    )
                    success = False
                    continue
                otds_sp_endpoint = auth_handler.get("otds_sp_endpoint")
                if not otds_sp_endpoint:
                    self.logger.error(
                        "SAML authorization handler needs a OTDS SP endpoint in payload! Skipping...",
                    )
                    success = False
                    continue
                auth_principal_attributes = auth_handler.get(
                    "auth_principal_attributes",
                )
                nameid_format = auth_handler.get("nameid_format")
                response = self._otds.add_auth_handler_saml(
                    name=handler_name,
                    description=handler_description,
                    scope=handler_scope,
                    provider_name=provider_name,
                    saml_url=saml_url,
                    otds_sp_endpoint=otds_sp_endpoint,
                    priority=priority,
                    active_by_default=active_by_default,
                    auth_principal_attributes=auth_principal_attributes,
                    nameid_format=nameid_format,
                )
            case "SAP":
                certificate_file = auth_handler.get("certificate_file")
                if not certificate_file:
                    self.logger.error(
                        "SAP Authorization handler -> '%s' (%s) requires a certificate file name in payload! Skipping...",
                        handler_name,
                        handler_type,
                    )
                    success = False
                    continue
                certificate_password = auth_handler.get("certificate_password")
                if not certificate_password:
                    # This is not an error - we canhave this key with empty string!
                    self.logger.info(
                        "SAP Authorization handler -> '%s' (%s) does not have a certificate password - this can be OK.",
                        handler_name,
                        handler_type,
                    )
                response = self._otds.add_auth_handler_sap(
                    name=handler_name,
                    description=handler_description,
                    scope=handler_scope,
                    certificate_file=certificate_file,
                    certificate_password=certificate_password,
                    priority=priority,
                )
            case "OAUTH":
                provider_name = auth_handler.get("provider_name")
                if not provider_name:
                    self.logger.error(
                        "OAUTH Authorization handler -> '%s' (%s) requires a provider name in payload! Skipping...",
                        handler_name,
                        handler_type,
                    )
                    success = False
                    continue
                client_id = auth_handler.get("client_id")
                if not client_id:
                    self.logger.error(
                        "OAUTH Authorization handler -> '%s' (%s) requires a client ID in payload! Skipping...",
                        handler_name,
                        handler_type,
                    )
                    success = False
                    continue
                client_secret = auth_handler.get("client_secret")
                if not client_secret:
                    self.logger.error(
                        "OAUTH Authorization handler -> '%s' (%s) requires a client secret in payload! Skipping...",
                        handler_name,
                        handler_type,
                    )
                    success = False
                    continue
                authorization_endpoint = auth_handler.get("authorization_endpoint")
                if not authorization_endpoint:
                    self.logger.error(
                        "OAUTH Authorization handler -> '%s' (%s) requires a authorization endpoint in payload! Skipping...",
                        handler_name,
                        handler_type,
                    )
                    success = False
                    continue
                token_endpoint = auth_handler.get("token_endpoint")
                if not token_endpoint:
                    self.logger.warning(
                        "OAUTH Authorization handler -> '%s' (%s) does not have a token endpoint!",
                        handler_name,
                        handler_type,
                    )
                scope_string = auth_handler.get("scope_string")
                response = self._otds.add_auth_handler_oauth(
                    name=handler_name,
                    description=handler_description,
                    scope=handler_scope,
                    provider_name=provider_name,
                    client_id=client_id,
                    client_secret=client_secret,
                    priority=priority,
                    active_by_default=active_by_default,
                    authorization_endpoint=authorization_endpoint,
                    token_endpoint=token_endpoint,
                    scope_string=scope_string,
                )
            case _:
                self.logger.error(
                    "Unsupported authorization handler type -> '%s'!",
                    handler_type,
                )
                return False

        if response:
            self.logger.info(
                "Successfully added OTDS authorization handler -> '%s' (%s).",
                handler_name,
                handler_type,
            )
        else:
            self.logger.error(
                "Failed to add OTDS authorization handler -> '%s' (%s)!",
                handler_name,
                handler_type,
            )
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._auth_handlers,
    )

    return success

process_avts_questions(section_name='avtsQuestions')

Process Aviator Search repositories.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'avtsQuestions'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_avts_questions")
def process_avts_questions(self, section_name: str = "avtsQuestions") -> bool:
    """Process Aviator Search repositories.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._avts_questions:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    if not self._avts_questions.get("enabled", True):
        self.logger.info(
            "Payload section -> '%s' is not enabled. Skipping...",
            section_name,
        )
        return True
    questions = self._avts_questions.get("questions", [])
    self.logger.info("Sample questions -> %s.", str(questions))

    token = self._avts.authenticate()
    if not token:
        self.logger.error("Cannot authenticate at Aviator Search!")
        success = False
    else:
        response = self._avts.set_questions(questions=questions)

        if response is None:
            self.logger.error("Aviator Search setting questions failed!")
            success = False
        else:
            self.logger.info("Successfully configured Aviator Search questions.")
            self.logger.debug("%s", response)

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._avts_questions,
    )

    return success

process_avts_repositories(section_name='avtsRepositories')

Process Aviator Search repositories.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'avtsRepositories'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_avts_repositories")
def process_avts_repositories(self, section_name: str = "avtsRepositories") -> bool:
    """Process Aviator Search repositories.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._avts_repositories:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    token = self._avts.authenticate()
    if not token:
        self.logger.error("Cannot authenticate at Aviator Search!")
        success = False
    else:
        for payload_repo in self._avts_repositories:
            if not payload_repo.get("enabled", True):
                continue

            repository = self._avts.get_repo_by_name(name=payload_repo["name"])

            if repository is None:
                self.logger.info(
                    "Repository -> '%s' does not exist, creating it...",
                    payload_repo["name"],
                )

                if payload_repo.get("type", "Extended ECM") == "Extended ECM":
                    repository = self._avts.create_extended_ecm_repo(
                        name=payload_repo["name"],
                        username=payload_repo["username"],
                        password=payload_repo["password"],
                        otcs_url=payload_repo["otcs_url"],
                        otcs_api_url=payload_repo["otcs_api_url"],
                        node_id=int(payload_repo["node_id"]),
                    )

                elif payload_repo["type"] == "Documentum":
                    self.logger.warning("Not yet implemented")
                elif payload_repo["type"] == "MSTeams":
                    repository = self._avts.create_msteams_repo(
                        name=payload_repo["name"],
                        client_id=payload_repo["client_id"],
                        tenant_id=payload_repo["tenant_id"],
                        certificate_file=payload_repo["certificate_file"],
                        certificate_password=payload_repo["certificate_password"],
                        index_attachments=payload_repo.get("index_attachments", True),
                        index_call_recordings=payload_repo.get(
                            "index_call_recordings",
                            True,
                        ),
                        index_message_replies=payload_repo.get(
                            "index_message_replies",
                            True,
                        ),
                        index_user_chats=payload_repo.get("index_user_chats", True),
                    )
                elif payload_repo["type"] == "SharePoint":
                    repository = self._avts.create_sharepoint_repo(
                        name=payload_repo["name"],
                        client_id=payload_repo["client_id"],
                        tenant_id=payload_repo["tenant_id"],
                        certificate_file=payload_repo["certificate_file"],
                        certificate_password=payload_repo["certificate_password"],
                        sharepoint_url=payload_repo["sharepoint_url"],
                        sharepoint_url_type=payload_repo["sharepoint_url_type"],
                        sharepoint_mysite_url=payload_repo["sharepoint_mysite_url"],
                        sharepoint_admin_url=payload_repo["sharepoint_admin_url"],
                        index_user_profiles=payload_repo.get(
                            "index_message_replies",
                            False,
                        ),
                    )
                else:
                    self.logger.error(
                        "Invalid repository type -> '%s' specified! Valid values are: Extended ECM, Documentum, MSTeams, SharePoint.",
                        payload_repo["type"],
                    )
                    success = False
                    break

                if repository is None:
                    self.logger.error(
                        "Creation of Aviator Search repository -> '%s' failed!",
                        payload_repo["name"],
                    )
                    success = False
                else:
                    self.logger.info(
                        "Successfully created Aviator Search repository -> '%s'.",
                        payload_repo["name"],
                    )
                    self.logger.debug("%s", repository)

            else:
                self.logger.info(
                    "Aviator Search repository -> '%s' already exists.",
                    payload_repo["name"],
                )

            # Start Crawling
            start_crawling = payload_repo.get("start", False)

            if repository is not None and start_crawling:
                response = self._avts.start_crawling(repo_name=payload_repo["name"])

                if response is None:
                    self.logger.error(
                        "Aviator Search start crawling on repository failed -> '%s'!",
                        payload_repo["name"],
                    )
                    success = False
                else:
                    self.logger.info(
                        "Aviator Search crawling started on repository -> '%s'",
                        payload_repo["name"],
                    )
                    self.logger.debug("%s", response)
        # end for payload_repo in self._avts_repositories:
    # end else:

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._avts_repositories,
    )

    return success

process_browser_automations(browser_automations, section_name='browserAutomations', check_status=True)

Process Playwright-based browser automations and tests.

Parameters:

Name Type Description Default
browser_automations list

A list of browser automations (need this as parameter as we have multiple lists).

required
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'browserAutomations'
check_status bool

Defines whether or not this needs to re-run for each customizer run (even if it has been successful before). If check_status is True (default) then it is only re-run if it has NOT been successfully before.

True

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_browser_automations")
def process_browser_automations(
    self,
    browser_automations: list,
    section_name: str = "browserAutomations",
    check_status: bool = True,
) -> bool:
    """Process Playwright-based browser automations and tests.

    Args:
        browser_automations (list):
            A list of browser automations (need this as parameter as we
            have multiple lists).
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.
        check_status (bool, optional):
            Defines whether or not this needs to re-run
            for each customizer run (even if it has been successful before).
            If check_status is True (default) then it is only re-run
            if it has NOT been successfully before.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not browser_automations:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if check_status and self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    automation_type = "Browser" if "browser" in section_name else "Test"

    for browser_automation in browser_automations:
        name = browser_automation.get("name")
        if not name:
            self.logger.error("%s automation is missing a unique name. Skipping...", automation_type)
            success = False
            continue

        self._log_header_callback(
            text="Process {} Automation -> '{}'".format(automation_type, name),
            char="-",
        )

        description = browser_automation.get("description", "")
        if description:
            self.logger.info(
                "%s automation description -> '%s'.",
                automation_type,
                description,
            )

        # Check if browser automation has been explicitly disabled in payload
        # (enabled = false). In this case we skip this payload element:
        if not browser_automation.get("enabled", True):
            self.logger.info(
                "Payload for %s automation -> '%s' is disabled. Skipping...",
                automation_type.lower(),
                name,
            )
            continue

        base_url = browser_automation.get("base_url")
        if not base_url:
            self.logger.error(
                "%s automation -> '%s' is missing 'base_url' parameter. Skipping...", automation_type, name
            )
            browser_automation["result"] = "failure"
            success = False
            continue

        user_name = browser_automation.get("user_name", "")
        if not user_name:
            self.logger.info("%s automation -> '%s' is not having user name.", automation_type, name)

        password = browser_automation.get("password", "")
        if not password:
            self.logger.info("%s automation -> '%s' is not having password.", automation_type, name)

        automation_steps = browser_automation.get("automations", [])
        if not automation_steps:
            self.logger.error(
                "%s automation -> '%s' is missing list of automations. Skipping...",
                automation_type,
                name,
            )
            browser_automation["result"] = "failure"
            success = False
            continue

        debug_automation: bool = browser_automation.get("debug", False)

        # Create Selenium Browser Automation:
        self.logger.info("%s automation base URL -> %s.", automation_type, base_url)
        self.logger.info("%s automation user -> '%s'.", automation_type, user_name)
        wait_until = browser_automation.get("wait_until")  # it is OK to be None
        if "wait_until" in browser_automation:
            # Only log the "wait until" value if it is specified in the payload:
            self.logger.info(
                "%s Automation page navigation strategy is to wait until -> '%s'.",
                automation_type,
                wait_until,
            )
        browser_automation_object = BrowserAutomation(
            base_url=base_url,
            user_name=user_name,
            user_password=password,
            automation_name=name,
            take_screenshots=debug_automation,
            headless=browser_automation.get("headless", self._browser_headless),
            logger=self.logger,
            wait_until=wait_until,
            browser=browser_automation.get("browser"),  # None is acceptable
        )
        if not browser_automation_object:
            self.logger.error(
                "Cannot execute browser automation -> '%s'. Initialization of browser automation object failed!",
                name,
            )
            browser_automation["result"] = "failure"
            success = False
            continue
        # Wait time is a global setting (for whole browser session)
        # This makes sure a page is fully loaded and elements are present
        # before accessing them. We set 30.0 seconds as default if not
        # otherwise specified by "wait_time" in the payload.
        wait_time = float(browser_automation.get("wait_time", 30.0))
        browser_automation_object.set_timeout(wait_time=wait_time)
        if "wait_time" in browser_automation:
            self.logger.info(
                "%s automation wait time -> '%s' configured.",
                automation_type,
                str(wait_time),
            )

        # Initialize overall result status:
        result = True
        first_step = True

        for automation_step in automation_steps:
            automation_step_type = automation_step.get("type", "")
            if not automation_step_type:
                self.logger.error(
                    "%s automation step -> %s in browser automation -> '%s' is missing 'type' parameter. Stopping automation -> '%s'.",
                    automation_type,
                    str(automation_step),
                    name,
                    name,
                )
                success = False
                break
            # Check if browser automation step has been explicitly disabled in payload
            # (enabled = false). In this case we skip this automation step:
            if not automation_step.get("enabled", True):
                self.logger.info(
                    "Automation step -> '%s' in automation -> '%s' is disabled. Skipping...",
                    automation_step_type,
                    name,
                )
                continue
            dependent = automation_step.get("dependent", True)
            if not dependent and not result:
                self.logger.warning(
                    "Ignore result of previous step as current step -> '%s' is NOT dependent on it.",
                    automation_step_type,
                )
                result = True
            elif not result:
                # In this case a proceeding automation step has failed
                # and this step is marked as dependent. Then it does not make sense
                # to continue with this automation step after the proceeding step failed.
                self.logger.warning(
                    "Step -> '%s' is dependent on a proceeding step that failed. Skipping this step...",
                    automation_step_type,
                )
                continue
            elif not first_step:
                self.logger.debug(
                    "Current step -> '%s' is %s on proceeding step.",
                    automation_step_type,
                    "dependent" if dependent else "not dependent",
                )

            match automation_step_type:
                case "login":
                    page = automation_step.get("page", "")
                    user_field = automation_step.get("user_field", "otds_username")
                    password_field = automation_step.get(
                        "password_field",
                        "otds_password",
                    )
                    login_button = automation_step.get("login_button", "loginbutton")
                    # Do we have a step-specific wait mechanism? If not, we pass None
                    # then the browser automation will take the default configured for
                    # the whole browser automation (see BrowserAutomation() constructor above):
                    wait_until = automation_step.get("wait_until", None)
                    self.logger.info(
                        "Login to -> %s as user -> '%s' (%s page navigation strategy is to wait until -> '%s')...",
                        base_url + page,
                        user_name,
                        "specific" if wait_until is not None else "default",
                        wait_until if wait_until is not None else browser_automation_object.wait_until,
                    )
                    result = browser_automation_object.run_login(
                        page=page,
                        user_field=user_field,
                        password_field=password_field,
                        login_button=login_button,
                        wait_until=wait_until,
                    )
                    if not result:
                        self.logger.error(
                            "Cannot log into -> %s. Skipping to next automation step...",
                            base_url + page,
                        )
                        automation_step["result"] = "failure"
                        success = False
                        continue
                    self.logger.info(
                        "Successfully logged into page -> %s. Page title -> '%s'.",
                        base_url + page,
                        browser_automation_object.get_title(),
                    )
                case "get_page":
                    page = automation_step.get("page", "")
                    if not page:
                        self.logger.error(
                            "Automation step type -> '%s' requires 'page' parameter. Stopping automation.",
                            automation_step_type,
                        )
                        automation_step["result"] = "failure"
                        success = False
                        break
                    volume = automation_step.get("volume", OTCS.VOLUME_TYPE_ENTERPRISE_WORKSPACE)
                    path = automation_step.get("path", [])
                    if path and volume:
                        page_node = self._otcs.get_node_by_volume_and_path(
                            volume_type=volume,
                            path=path,
                            create_path=False,
                        )
                        page_id = self._otcs.get_result_value(response=page_node, key="id")
                        if not page_id:
                            # if not parent_node:
                            self.logger.error(
                                "%s automation -> '%s' has a page path that does not exist. Skipping...",
                                automation_type,
                                name,
                            )
                            automation_step["result"] = "failure"
                            success = False
                            continue
                        self.logger.info(
                            "Resolved volume -> %s and page path -> %s to node ID -> %s.",
                            str(volume),
                            str(path),
                            str(page_id),
                        )
                    else:
                        page_id = None
                    if "{}" in page and page_id:
                        page = page.format(page_id)
                    # Do we have a step-specific wait mechanism? If not, we pass None
                    # then the browser automation will take the default configured for
                    # the whole browser automation (see BrowserAutomation() constructor called above):
                    wait_until = automation_step.get("wait_until", None)
                    self.logger.info(
                        "Load page -> %s (%s page navigation strategy is to wait until -> '%s')...",
                        base_url + page,
                        "specific" if wait_until is not None else "default",
                        wait_until if wait_until is not None else browser_automation_object.wait_until,
                    )
                    result = browser_automation_object.get_page(url=page, wait_until=wait_until)
                    if not result:
                        self.logger.error(
                            "Cannot load page -> %s! Skipping this step...",
                            page,
                        )
                        automation_step["result"] = "failure"
                        success = False
                        continue
                    self.logger.info(
                        "Successfully loaded page -> %s. Page title -> '%s'.",
                        base_url + page,
                        browser_automation_object.get_title(),
                    )
                case "click_elem":
                    # We keep the deprecated "elem" syntax supported (for now)
                    selector = automation_step.get("selector", automation_step.get("elem", ""))
                    if not selector:
                        self.logger.error(
                            "Automation step type -> '%s' requires 'selector' parameter. Stopping automation.",
                            automation_step_type,
                        )
                        automation_step["result"] = "failure"
                        success = False
                        break
                    # We keep the deprecated "find" syntax supported (for now)
                    selector_type = automation_step.get("selector_type", automation_step.get("find", "id"))
                    show_error = automation_step.get("show_error", True)
                    # Do we navigate away from the current page with the click?
                    navigation = automation_step.get("navigation", False)
                    # Do we open a new browser (popup) window with the click?
                    popup_window = automation_step.get("popup_window", False)
                    # De we close the current (popup) window with the click?
                    close_window = automation_step.get("close_window", False)
                    # Do we have a 'desired' state for clicking a checkbox?
                    checkbox_state = automation_step.get("checkbox_state", None)
                    wait_until = automation_step.get("wait_until", None)
                    wait_time = automation_step.get("wait_time", 0.0)
                    role_type = automation_step.get("role_type", None)
                    occurrence = automation_step.get("occurrence", 1)
                    scroll_to_element = automation_step.get("scroll_to_element", True)
                    exact_match = automation_step.get("exact_match", None)
                    regex = automation_step.get("regex", None)
                    hover_only = automation_step.get("hover_only", False)
                    iframe = automation_step.get("iframe", None)
                    force = automation_step.get("force", None)
                    click_button = automation_step.get("click_button", None)
                    click_count = automation_step.get("click_count", None)
                    click_modifiers = automation_step.get("click_modifiers", None)
                    repeat_reload = automation_step.get("repeat_reload", None)
                    repeat_reload_delay = automation_step.get("repeat_reload_delay", None)
                    result = browser_automation_object.find_elem_and_click(
                        selector=selector,
                        selector_type=selector_type,
                        role_type=role_type,
                        occurrence=occurrence,
                        scroll_to_element=scroll_to_element,
                        desired_checkbox_state=checkbox_state,
                        is_navigation_trigger=navigation,
                        is_popup_trigger=popup_window,
                        is_page_close_trigger=close_window,
                        wait_until=wait_until,
                        wait_time=wait_time,
                        exact_match=exact_match,
                        regex=regex,
                        hover_only=hover_only,
                        iframe=iframe,
                        force=force,
                        click_button=click_button,
                        click_count=click_count,
                        click_modifiers=click_modifiers,
                        repeat_reload=repeat_reload,
                        repeat_reload_delay=repeat_reload_delay,
                        show_error=show_error,
                    )
                    if not result:
                        message = "Cannot find clickable element with selector -> '{}' ({}) on current page. Skipping this step...".format(
                            selector, selector_type
                        )
                        if show_error:
                            self.logger.error(message)
                            automation_step["result"] = "failure"
                            success = False
                        else:
                            self.logger.warning(message)
                        continue
                    self.logger.info(
                        "Successfully %s %s element selected by -> '%s' (%s%s).",
                        "clicked" if not hover_only else "hovered over",
                        "navigational" if navigation else "non-navigational",
                        selector,
                        "selector type -> '{}'".format(selector_type),
                        ", role type -> '{}'".format(role_type) if role_type else "",
                    )
                case "set_elem":
                    # We keep the deprecated "elem" syntax supported (for now)
                    selector = automation_step.get("selector", automation_step.get("elem", ""))
                    if not selector:
                        self.logger.error(
                            "Automation step type -> '%s' requires 'selector' parameter. Stopping automation.",
                            automation_step_type,
                        )
                        automation_step["result"] = "failure"
                        success = False
                        break
                    # We keep the deprecated "find" syntax supported (for now)
                    selector_type = automation_step.get("selector_type", automation_step.get("find", "id"))
                    role_type = automation_step.get("role_type", None)
                    occurrence = automation_step.get("occurrence", 1)
                    exact_match = automation_step.get("exact_match", None)
                    regex = automation_step.get("regex", None)
                    iframe = automation_step.get("iframe", None)
                    value = automation_step.get("value", "")
                    typing = automation_step.get("typing", False)
                    press_enter = automation_step.get("press_enter", False)
                    if not value:
                        self.logger.error(
                            "Automation step type -> '%s' for element selected by -> '%s' (%s) requires 'value' parameter. Stopping automation.",
                            automation_step_type,
                            selector,
                            selector_type,
                        )
                        automation_step["result"] = "failure"
                        success = False
                        break
                    # we also support replacing placeholders that are
                    # enclosed in double % characters like %%OTCS_RESOURCE_ID%%:
                    if isinstance(value, str):
                        value = self.replace_placeholders(value)
                    show_error = automation_step.get("show_error", True)
                    result = browser_automation_object.find_elem_and_set(
                        selector=selector,
                        value=value,
                        selector_type=selector_type,
                        role_type=role_type,
                        occurrence=occurrence,
                        press_enter=press_enter,
                        exact_match=exact_match,
                        regex=regex,
                        iframe=iframe,
                        typing=typing,
                        show_error=show_error,
                    )
                    if not result:
                        message = "Cannot set element{} selected by -> '{}' ({}{}) to value -> '{}'. Skipping this step...".format(
                            " (occurrence -> {})".format(occurrence) if occurrence > 1 else "",
                            selector,
                            "selector type -> '{}'".format(selector_type),
                            ", role type -> '{}'".format(role_type) if role_type else "",
                            value,
                        )
                        if show_error:
                            self.logger.error(message)
                            automation_step["result"] = "failure"
                            success = False
                        else:
                            self.logger.warning(message)
                        continue
                    self.logger.info(
                        "Successfully set element%s selected by -> '%s' (%s%s) to value -> '%s'.",
                        " (occurrence -> {})".format(occurrence) if occurrence > 1 else "",
                        selector,
                        "selector type -> '{}'".format(selector_type),
                        ", role type -> '{}'".format(role_type) if role_type else "",
                        value,
                    )
                case "check_elem":
                    # We keep the deprecated "elem" syntax supported (for now)
                    selector = automation_step.get("selector", automation_step.get("elem", ""))
                    if not selector:
                        self.logger.error(
                            "Automation step type -> '%s' requires 'selector' parameter. Stopping automation.",
                            automation_step_type,
                        )
                        automation_step["result"] = "failure"
                        success = False
                        break
                    # We keep the deprecated "find" syntax supported (for now)
                    selector_type = automation_step.get("selector_type", automation_step.get("find", "id"))
                    role_type = automation_step.get("role_type", None)
                    value = automation_step.get("value", None)
                    attribute = automation_step.get("attribute", "")
                    substring = automation_step.get("substring", False)
                    iframe = automation_step.get("iframe", None)
                    min_count = automation_step.get("min_count", 1)
                    want_exist = automation_step.get("want_exist", True)
                    wait_time = automation_step.get("wait_time", 0.0)
                    if value:
                        # we also support replacing placeholders that are
                        # enclosed in double % characters like %%OTCS_RESOURCE_ID%%:
                        value = self.replace_placeholders(value)
                    (result, count) = browser_automation_object.check_elems_exist(
                        selector=selector,
                        selector_type=selector_type,
                        role_type=role_type,
                        value=value,
                        attribute=attribute,
                        substring=substring,
                        iframe=iframe,
                        min_count=min_count,
                        wait_time=wait_time,  # time to wait before the check is actually done
                        show_error=not want_exist,  # if element is not found that we do not want to find it is not an error
                    )
                    # Check if we didn't get what we want:
                    if (not result and want_exist) or (result and not want_exist):
                        self.logger.error(
                            "%s %s%s%s on current page. Test failed.%s",
                            "Cannot find" if not result and want_exist else "Found",
                            "{} elements with selector -> '{}' ({}{})".format(
                                min_count if want_exist else count,
                                selector,
                                "selector type -> '{}'".format(selector_type),
                                ", role type -> '{}'".format(role_type) if role_type else "",
                            )
                            if (min_count > 1 and want_exist) or (count > 1 and not want_exist)
                            else "an element with selector -> '{}' ({}{})".format(
                                selector,
                                "selector type -> '{}'".format(selector_type),
                                ", role type -> '{}'".format(role_type) if role_type else "",
                            ),
                            " with {}value -> '{}'".format("substring-" if substring else "", value)
                            if value
                            else "",
                            " in attribute -> '{}'".format(attribute) if attribute else "",
                            " Found {}{} occurences.".format(
                                count,
                                " undesirable" if not want_exist else " from a minimum of {}".format(min_count),
                            ),
                        )
                        success = False
                        continue
                        # Don't break here! We want to do all existance tests!
                    self.logger.info(
                        "Successfully passed %sexistence test for %s%s%s on current page.",
                        "non-" if not want_exist else "",
                        "{} elements with selector -> '{}' ({})".format(min_count, selector, selector_type)
                        if min_count > 1
                        else "an element with selector -> '{}' ({})".format(selector, selector_type),
                        " with {}value -> '{}'".format("substring-" if substring else "", value) if value else "",
                        " in attribute -> '{}'".format(attribute) if attribute else "",
                    )
                case _:
                    self.logger.error(
                        "Illegal automation step type -> '%s' in %s automation!",
                        automation_step_type,
                        automation_type.lower(),
                    )
                    automation_step["result"] = "failure"
                    success = False
                    break
            # end match automation_step_type:
            first_step = False
        # end for automation_step in automation_steps:

        # Cleanup session and and remove reference to the object:
        browser_automation_object.end_session()
        browser_automation_object = None

        browser_automation["result"] = (
            "failure" if any(step.get("result", "success") == "failure" for step in automation_steps) else "success"
        )
    # end for browser_automation in browser_automations:

    if check_status:
        self.write_status_file(
            success=success,
            payload_section_name=section_name,
            payload_section=browser_automations,
        )

    return success

process_bulk_categories(row, index, categories, replacements)

Replace the value placeholders of the bulk category structures.

The payload placeholders are replaced with values from the Pandas Series (row). It also processes value mappings + lookups and special treatment of list and table values.

Parameters:

Name Type Description Default
row Series

The current row in the Pandas data frame.

required
index str

The index of the Pandas data frame. Just used here for logging.

required
categories list

List of payload categories.

required
replacements list

List of replacements.

required

Returns:

Name Type Description
list list

List of categories.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_categories")
def process_bulk_categories(
    self,
    row: pd.Series,
    index: str,
    categories: list,
    replacements: list,
) -> list:
    """Replace the value placeholders of the bulk category structures.

    The payload placeholders are replaced with values from the Pandas Series (row).
    It also processes value mappings + lookups and special treatment of list and table values.

    Args:
        row (pd.Series):
            The current row in the Pandas data frame.
        index (str):
            The index of the Pandas data frame. Just used here for logging.
        categories (list):
            List of payload categories.
        replacements (list):
            List of replacements.

    Returns:
        list:
            List of categories.

    """

    # Make sure the threads are not changing data structures that
    # are shared between threads. categories is a list of dicts.
    # list and dicts are "mutable" data structures in Python!
    worker_categories = copy.deepcopy(categories)

    # In this first loop we expand table-value attributes into a new
    # list of category / attribute payload. The value of table-value attributes
    # is a list of dictionaries (in a string we evaluate into a Python
    # datastructure)
    worker_categories_expanded = []
    for category_item in worker_categories:
        if "value_type" in category_item and category_item["value_type"] == "table":
            value_field = category_item["value_field"]

            # The following method always returns a string even if the value is actually a list.
            # This is required as we want to support multiple placeholders in one string...
            value = self.replace_bulk_placeholders(
                input_string=value_field,
                row=row,
                index=None,
                replacements=replacements,
            )
            # If value is empty (probably because placeholders could not be resolved)
            # and if the payload provides an alternative value field we try this:
            if not value and "value_field_alt" in category_item:
                value = self.replace_bulk_placeholders(
                    input_string=category_item["value_field_alt"],
                    row=row,
                    index=None,
                    replacements=replacements,
                )
            if not value:
                self.logger.info(
                    "Table-type attribute -> '%s' is empty (value field -> '%s'). Cannot parse table. Skipping...",
                    category_item.get("name", ""),
                    value_field,
                )
                continue

            try:
                value_table = literal_eval(value)
            except (SyntaxError, ValueError):
                self.logger.error(
                    "Cannot parse table-type attribute -> '%s'; value field -> '%s'",
                    category_item.get("name", ""),
                    value_field,
                )
                continue

            if not isinstance(value_table, list):
                self.logger.error(
                    "Table-type value -> '%s' requires a list of dictionaries!",
                    value_field,
                )
                continue

            """
            Get the mapping of the loader generated columns in the Data Frame to the
            attribute names in the target OTCS category. If no mapping
            is in the payload, then it is assumed that the category
            attribute names are identical to the column names in the Data Frame

            Example mapping:

            attribute_mapping = {
              "Application": "u_product_model",
              "Version": "u_version_name"
            }
            """

            attribute_mapping = category_item.get("attribute_mapping", None)

            row_index = 1
            for value_row in value_table:
                if not isinstance(value_row, dict):
                    self.logger.error(
                        "Illegal data type for table row -> %s. Expected 'dict', got -> '%s' with value -> %s",
                        str(row_index),
                        type(value_row),
                        value_row,
                    )
                    continue
                for key, value in value_row.items():
                    attribute = {}
                    attribute["name"] = category_item.get("name", "")
                    attribute["set"] = category_item.get("set", "")
                    attribute["row"] = row_index
                    # check if we have a mapping for this attribute in the payload:
                    if attribute_mapping and key in attribute_mapping:
                        attribute["attribute"] = attribute_mapping[key]
                    else:
                        attribute["attribute"] = key
                    # For tables values can be None if the number of
                    # list items in the source columns are not equal.
                    # To avoid the warning below we set the value to empty string
                    # if it is None:
                    attribute["value"] = value if value is not None else ""
                    worker_categories_expanded.append(attribute)
                row_index += 1
        # end if "value_type" in category_item and category_item["value_type"] == "table"
        else:
            worker_categories_expanded.append(category_item)
    # end for category_item in worker_categories

    # this loop generates a "value" for each
    # "value_field". "value_field" may also contain lists
    # that are either delimited by [...] or by a "value_type" with value "list"
    for category_item in worker_categories_expanded:
        if "attribute" not in category_item:
            self.logger.error(
                "Category item -> %s is missing the attribute field!",
                category_item,
            )
            continue

        # per default the value is in the "value" item:
        value = category_item.get("value", None)

        # is there a value replacement for the current attribute?
        if "value_field" in category_item:
            value_field = category_item["value_field"]
            # If no row is specified we set the set index to None and not 0 to handle
            # cases where we have multi-value attributes that take a list as parameter
            set_index = int(category_item["row"]) - 1 if "row" in category_item else None

            # this method always returns a string even if the value is
            # actually a list.
            value = self.replace_bulk_placeholders(
                input_string=value_field,
                row=row,
                index=set_index,
                replacements=replacements,
            )
            # If value is empty (probably because placeholders could not be resolved)
            # and if the payload provides an alternative value field we try this:
            if not value and "value_field_alt" in category_item:
                value = self.replace_bulk_placeholders(
                    input_string=category_item["value_field_alt"],
                    row=row,
                    index=set_index,
                    replacements=replacements,
                )
        else:
            value_field = None

        # if we don't have a value now, then there's an issue:
        if value is None:
            value = ""
            self.logger.warning(
                "Category item needs either a value or value_field! Skipping attribute -> '%s'",
                category_item["attribute"],
            )

        # We have an empty string value (this is different from None!)
        if value == "":
            category_item["value"] = value
            # We can continue as any further processing (below) does not make sense for an empty string value:
            continue

        # This variable should only be true if we don't have
        # a native python string but a delimiter separated
        # value list in a string, e.g. "a, b, c" or "a | b | c" or "x;y;z":
        is_list_in_string = False

        # The data source loader may have written a real python list into the value
        # In this case the string value includes square brackets [...]
        # We only do  this if the actual type of the value is string and the
        # proposed value (value_type) is not string:
        if (
            isinstance(value, str)  # it is a string
            and value.startswith("[")  # it starts with a list indicator character
            and value.endswith("]")  # it ends with a list indicator character
            and category_item.get("value_type", "")
            != "string"  # it is NOT explicitly stated it should remain a string
        ):
            # Remove the square brackets and declare it is a list!
            try:
                value = literal_eval(value)
            except (SyntaxError, ValueError) as e:
                self.logger.warning(
                    "Cannot directly parse list-type attribute; value field -> %s; error -> %s. Try string processing...",
                    value_field,
                    str(e),
                )
                self.logger.warning(
                    "Value string -> %s has [...] - remove brackets and interpret as delimiter separated values in a string...",
                    value,
                )
                # In this failure case we try to remove the square brackets and hope the inner part
                # can be treated as a string of values delimited with a delimiter (e.g. comma or semicolon)
                value = value.strip("[]")
                is_list_in_string = True

        # Handle this special case where we get a string that actually represents a date time format and convert it.
        # We only do this if the attribute has a "value_type" of "datetime" in the payload:
        if category_item.get("value_type", "string") == "datetime":
            old_value = value
            try:
                date_obj = parse(value)
                value = datetime.strftime(date_obj, "%Y-%m-%dT%H:%M:%SZ")
            except (OverflowError, ValueError):
                self.logger.error(
                    "Cannot convert value -> '%s' into a date for attribute -> '%s'. Value will be empty.",
                    old_value,
                    category_item["attribute"],
                )
                value = ""
            else:
                self.logger.debug(
                    "Attribute -> %s is declared in payload to be a datetime (convert format). Converted it from -> %s to -> %s",
                    category_item.get("attribute"),
                    old_value,
                    value,
                )

        # Handle special case where we get a string that actually represents a list but is
        # not yet a Python list but a string. This requires that value_type == "list". The list splitter
        # can be specified via "list_splitter" in the payload:
        # Now we try to convert the string to a Python list:
        if (category_item.get("value_type", "string") == "list" or is_list_in_string) and isinstance(value, str):
            # we split the string after splitter characters:
            list_splitter = category_item.get("list_splitter", ";,")
            self.logger.info(
                "Value -> '%s' is declared in payload to be a list but it is provided as a string. Splitting it after these characters -> '%s'.",
                value,
                list_splitter,
            )

            # Escape the split characters to ensure they are treated literally in the regex pattern
            escaped_splitter = re.escape(list_splitter)

            # Construct the regex pattern dynamically
            pattern = rf"[{escaped_splitter}]\s*"

            # Split the value string at the defined splitter characters:
            elements = re.split(pattern, value)

            # Remove the quotes around each element
            elements = [element.strip("'") for element in elements]
            value = elements
            self.logger.info(
                "Found list for a multi-value category attribute -> '%s' from field -> '%s' in data row -> %s. Value -> '%s'.",
                category_item["attribute"],
                value_field,
                index,
                str(value),
            )

        # now we check if there's a data lookup configured in the payload:
        lookup_data_source = category_item.get("lookup_data_source", None)
        # Do we want to drop / clear values that fail to lookup?
        drop_value = category_item.get("lookup_data_failure_drop", False)

        if lookup_data_source:
            self.logger.info(
                "Found lookup data source -> '%s' for attribute -> '%s'. Processing...",
                lookup_data_source,
                category_item["attribute"],
            )
            if not isinstance(value, list):
                # value is a single value and not a list:
                (_, synonym) = self.process_bulk_workspaces_synonym_lookup(
                    data_source_name=lookup_data_source,
                    workspace_name_synonym=value,
                )
                if synonym:
                    self.logger.info(
                        "Found synonym -> '%s' for attribute -> '%s' and value -> '%s' in data source -> '%s'.",
                        synonym,
                        category_item["attribute"],
                        value,
                        lookup_data_source,
                    )
                    value = synonym
                elif drop_value:
                    self.logger.warning(
                        "Cannot lookup the value for attribute -> '%s' and value -> '%s' in data source -> '%s'. Clear existing value.",
                        category_item["attribute"],
                        value,
                        lookup_data_source,
                    )
                    # Clear the value:
                    value = ""
                else:
                    self.logger.warning(
                        "Cannot lookup the value for attribute -> '%s' and value -> '%s' in data source -> '%s'. Keep existing value.",
                        category_item["attribute"],
                        value,
                        lookup_data_source,
                    )
            # end if not isinstance(value, list)
            else:
                # value is a list - so we apply the lookup to each item:
                # Iterate backwards to avoid index issues while popping items:
                for i in range(len(value) - 1, -1, -1):
                    # Make sure the value does not have leading or trailing spaces:
                    value[i] = value[i].strip()
                    (_, synonym) = self.process_bulk_workspaces_synonym_lookup(
                        data_source_name=lookup_data_source,
                        workspace_name_synonym=value[i],
                        workspace_type=None,  # we don't need the workspace ID, just the workspace name
                    )
                    if synonym:
                        self.logger.info(
                            "Found synonym -> '%s' for attribute -> '%s' and value -> '%s' in data source -> '%s'.",
                            synonym,
                            category_item["attribute"],
                            value[i],
                            lookup_data_source,
                        )
                        value[i] = synonym
                    elif drop_value:
                        self.logger.warning(
                            "Cannot lookup the value -> '%s' for attribute -> '%s' in data source -> '%s'! Drop existing value from list.",
                            value[i],
                            category_item["attribute"],
                            lookup_data_source,
                        )
                        # Remove the list item we couldn't lookup as drop_value is True:
                        value.pop(i)
                    else:
                        self.logger.warning(
                            "Cannot lookup the value -> '%s' for attribute -> '%s' in data source -> '%s'! Keep existing value.",
                            value[i],
                            category_item["attribute"],
                            lookup_data_source,
                        )
        # end if lookup_data_source

        # If value is a list then we have a multi-value attribute.
        # We now want to make sure that we don't have duplicates in
        # the value list:
        if isinstance(value, list) and len(value) != len(set(value)):
            self.logger.info(
                "The multi-value attribute -> '%s' has duplicates in its values -> %s.",
                category_item["attribute"],
                value,
            )
            # Remove duplicates from the list while preserving order.
            # Uses `dict.fromkeys()` since dictionaries maintain insertion order (Python 3.7+).
            # This ensures that only the first occurrence of each element is kept.
            value = list(dict.fromkeys(value))
            self.logger.info("The attribute values got de-duplicated to -> %s.", value)

        # now we check if there's a values mapping configured in the payload.
        # This is a dictionary for keys being the original values and
        # values being the mapped values. This makes most sense for
        # values with a limited / fixed domain of values. Example
        # `value_mapping = { "TS": "Tropical Storm", "HU": "Hurricane"}`
        value_mapping = category_item.get("value_mapping", None)
        if value_mapping:
            if not isinstance(value, list):
                # value is a single value and not a list:
                if value in value_mapping:
                    self.logger.info(
                        "Found value mapping for attribute -> '%s' from value -> '%s' to value -> '%s'.",
                        category_item["attribute"],
                        value,
                        value_mapping[value],
                    )
                    value = value_mapping[value]
            else:
                # value is a list - so we apply the lookup to each item:
                # Iterate backwards to avoid index issues while popping items:
                for i in range(len(value) - 1, -1, -1):
                    # Make sure the value does not have leading or trailing spaces:
                    value[i] = value[i].strip()

                    if value[i] in value_mapping:
                        self.logger.info(
                            "Found value mapping for attribute -> '%s' from value -> '%s' to value -> '%s'",
                            category_item["attribute"],
                            value[i],
                            value_mapping[value[i]],
                        )
                        value[i] = value_mapping[value[i]]

        # If value is a list then we have a multi-value attribute.
        # If "sort_multi_values" is specified in payload and evaluate to True
        # we sort the list alphabetically (note that numbers are not necessarily sorted numerically):
        if isinstance(value, list) and len(value) > 1 and category_item.get("sort_multi_values", False):
            value.sort(key=str)
            self.logger.info(
                "Sorting the values of multi-value attribute -> '%s' to -> %s.",
                category_item["attribute"],
                value,
            )

        if value_field:
            self.logger.debug(
                "Reading category -> '%s', attribute -> '%s' from field -> '%s' in data row -> %s. Value -> '%s'.",
                category_item["name"],
                category_item["attribute"],
                value_field,
                index,
                str(value),
            )
        else:
            self.logger.debug(
                "Setting category -> '%s', attribute -> '%s' to value -> '%s'.",
                category_item["name"],
                category_item["attribute"],
                str(value),
            )
        category_item["value"] = value
    # end for category_item in worker_categories_expanded

    # Cleanup categories_payload to remove empty rows of sets:
    rows_to_remove = {}
    for attribute in worker_categories_expanded:
        if attribute.get("set") is not None and attribute.get("row") is not None:
            set_name = attribute["set"]
            row_number = attribute["row"]
            value = attribute["value"]

            # If value is empty, mark it for removal:
            if not value or value == [""]:  # Treat empty strings or None as empty
                # The following if statement is important to not mark a set row
                # as True = removable that has been set to False in the else case below!
                if (set_name, row_number) not in rows_to_remove:
                    rows_to_remove[(set_name, row_number)] = True
            else:
                # If any value in the row is not empty, mark the row as not removable.
                # This may change the marking that have been applied in the if case
                # above!
                rows_to_remove[(set_name, row_number)] = False

    # Keep only the rows marked as True (empty rows to be removed)
    rows_to_remove = {k: v for k, v in rows_to_remove.items() if v is True}

    if rows_to_remove:
        self.logger.debug("Empty rows to remove from sets -> %s.", rows_to_remove)
    else:
        self.logger.debug("No empty rows to remove from sets.")

    cleaned_categories = [
        item
        for item in worker_categories_expanded
        if "set" not in item or "row" not in item or (item["set"], item["row"]) not in rows_to_remove
    ]

    return cleaned_categories

process_bulk_classification_assignments(row, index, classifications, replacements)

Replace the value placeholders of the bulk classification structures.

The payload placeholders are replaced with values from the Pandas Series (row).

Parameters:

Name Type Description Default
row Series

The current row in the Pandas data frame.

required
index str

The index of the Pandas data frame. Just used here for logging.

required
classifications list

List of payload classifications.

required
replacements list

List of replacements.

required

Returns:

Name Type Description
list list

List of classification IDs.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_classification_assignments")
def process_bulk_classification_assignments(
    self,
    row: pd.Series,
    index: str,
    classifications: list,
    replacements: list,
) -> list:
    """Replace the value placeholders of the bulk classification structures.

    The payload placeholders are replaced with values from the Pandas Series (row).

    Args:
        row (pd.Series):
            The current row in the Pandas data frame.
        index (str):
            The index of the Pandas data frame. Just used here for logging.
        classifications (list):
            List of payload classifications.
        replacements (list):
            List of replacements.

    Returns:
        list:
            List of classification IDs.

    """

    result_list = []

    for classification in classifications:
        # Do we have a classification path?
        if isinstance(classification, list):
            # Replace placeholders in the list. As list is a mutable data type,
            # the replacement happens in-place:
            self.replace_bulk_placeholders_list(input_list=classification, row=row, replacements=replacements)
            class_node = self._otcs_frontend.get_node_by_volume_and_path(
                volume_type=self._otcs.VOLUME_TYPE_CLASSIFICATION_VOLUME,
                path=classification,
            )
        elif isinstance(classification, string):
            nickname = self.replace_bulk_placeholders(
                input_string=classification,
                row=row,
                replacements=replacements,
            )
            class_node = self._otcs_frontend.get_node_from_nickname(nickname=nickname)

        class_node_id = self._otcs.get_result_value(
            response=class_node,
            key="id",
        )
        if class_node_id:
            result_list.append(class_node_id)

    if result_list:
        self.logger.debug(
            "Found classifications for data row -> %s. Value -> %s",
            index,
            str(result_list),
        )

    return result_list

process_bulk_classifications(section_name='bulkClassifications')

Process bulk classifications in payload and bulk create them in Content Server (multi-threaded).

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'bulkClassifications'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
29319
29320
29321
29322
29323
29324
29325
29326
29327
29328
29329
29330
29331
29332
29333
29334
29335
29336
29337
29338
29339
29340
29341
29342
29343
29344
29345
29346
29347
29348
29349
29350
29351
29352
29353
29354
29355
29356
29357
29358
29359
29360
29361
29362
29363
29364
29365
29366
29367
29368
29369
29370
29371
29372
29373
29374
29375
29376
29377
29378
29379
29380
29381
29382
29383
29384
29385
29386
29387
29388
29389
29390
29391
29392
29393
29394
29395
29396
29397
29398
29399
29400
29401
29402
29403
29404
29405
29406
29407
29408
29409
29410
29411
29412
29413
29414
29415
29416
29417
29418
29419
29420
29421
29422
29423
29424
29425
29426
29427
29428
29429
29430
29431
29432
29433
29434
29435
29436
29437
29438
29439
29440
29441
29442
29443
29444
29445
29446
29447
29448
29449
29450
29451
29452
29453
29454
29455
29456
29457
29458
29459
29460
29461
29462
29463
29464
29465
29466
29467
29468
29469
29470
29471
29472
29473
29474
29475
29476
29477
29478
29479
29480
29481
29482
29483
29484
29485
29486
29487
29488
29489
29490
29491
29492
29493
29494
29495
29496
29497
29498
29499
29500
29501
29502
29503
29504
29505
29506
29507
29508
29509
29510
29511
29512
29513
29514
29515
29516
29517
29518
29519
29520
29521
29522
29523
29524
29525
29526
29527
29528
29529
29530
29531
29532
29533
29534
29535
29536
29537
29538
29539
29540
29541
29542
29543
29544
29545
29546
29547
29548
29549
29550
29551
29552
29553
29554
29555
29556
29557
29558
29559
29560
29561
29562
29563
29564
29565
29566
29567
29568
29569
29570
29571
29572
29573
29574
29575
29576
29577
29578
29579
29580
29581
29582
29583
29584
29585
29586
29587
29588
29589
29590
29591
29592
29593
29594
29595
29596
29597
29598
29599
29600
29601
29602
29603
29604
29605
29606
29607
29608
29609
29610
29611
29612
29613
29614
29615
29616
29617
29618
29619
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_classifications")
def process_bulk_classifications(self, section_name: str = "bulkClassifications") -> bool:
    """Process bulk classifications in payload and bulk create them in Content Server (multi-threaded).

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not self._bulk_classifications:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    # For efficient idem-potent operation we may want to see which
    # bulk classifications have already been processed before:
    if self.check_status_file(
        payload_section_name=section_name,
        payload_specific=True,
        prefix="failure_",
    ):
        # Read payload from where we left it last time
        self._bulk_classifications = self.get_status_file(
            payload_section_name=section_name,
            prefix="failure_",
        )
        if not self._bulk_classifications:
            self.logger.error(
                "Cannot load existing bulkClassifications failure file. Bailing out!",
            )
            return False

    success: bool = True

    for bulk_classification in self._bulk_classifications:
        # Check if element has been disabled in payload (enabled = false).
        # In this case we skip the element:
        enabled = bulk_classification.get("enabled", True)
        if not enabled:
            self.logger.info("Payload for bulk classification is disabled. Skipping...")
            continue

        # The payload element must have a "data_source" key:
        data_source_name = bulk_classification.get("data_source", None)
        if not data_source_name:
            self.logger.error("Missing data source name in bulk classification!")
            success = False
            continue

        self._log_header_callback(
            text="Process bulk classifications using data source -> '{}'".format(
                data_source_name,
            ),
            char="-",
        )

        copy_data_source = bulk_classification.get("copy_data_source", False)
        force_reload = bulk_classification.get("force_reload", True)

        # Load and prepare the data source for the bulk processing:
        if copy_data_source:
            self.logger.info(
                "Take a copy of data source -> '%s' to avoid side-effects for repeative usage of the data source...",
                data_source_name,
            )
            data = Data(
                self.process_bulk_datasource(
                    data_source_name=data_source_name,
                    force_reload=force_reload,
                ),
                logger=self.logger,
            )
        else:
            self.logger.info(
                "Use original data source -> '%s' and not do a copy.",
                bulk_classification["data_source"],
            )
            data = self.process_bulk_datasource(
                data_source_name=data_source_name,
                force_reload=force_reload,
            )
        if data is None:  # important to use "is None" here!
            self.logger.error("Failed to load data source -> '%s' for bulk classification!", data_source_name)
            success = False
            continue
        if data.get_data_frame() is None:  # important to use "is None" here!
            self.logger.error("Data source -> '%s' for bulk classification is empty!", data_source_name)
            continue

        # Check if fields with list substructures should be exploded.
        # We may want to do this outside the bulkDatasource to only
        # explode for bulkClassifications and not other bulk elements
        # like bulkDocuments or bulkWorkspaces:
        explosions = bulk_classification.get("explosions", [])
        for explosion in explosions:
            # explode field can be a string or a list
            # exploding multiple fields at once avoids
            # combinatorial explosions - this is VERY
            # different from exploding columns one after the other!
            if "explode_field" not in explosion and "explode_fields" not in explosion:
                self.logger.error("Missing explosion field(s)!")
                continue
            # we want to be backwards compatible...
            explode_fields = (
                explosion["explode_field"] if "explode_field" in explosion else explosion["explode_fields"]
            )
            flatten_fields = explosion.get("flatten_fields", [])
            split_string_to_list = explosion.get("split_string_to_list", False)
            list_splitter = explosion.get(
                "list_splitter",
                ",",
            )  # don't have None as default!
            self.logger.info(
                "Starting explosion of bulk classifications by field(s) -> %s (type -> %s). Size of data frame before explosion -> %s.",
                str(explode_fields),
                type(explode_fields),
                str(len(data)),
            )
            data.explode_and_flatten(
                explode_fields=explode_fields,
                flatten_fields=flatten_fields,
                make_unique=False,
                split_string_to_list=split_string_to_list,
                separator=list_splitter,
                reset_index=True,
            )
            self.logger.info(
                "Size of data frame after explosion -> %s.",
                str(len(data)),
            )

        # Keep only selected rows if filters are specified in bulkClassifications.
        # We have this _after_ "explosions" to allow access to subfields as well.
        # We have this _before_ "sorting" and "deduplication" as we may keep the wrong
        # rows otherwise (unique / deduplication always keeps the first matching row).
        # We may want to do this outside the bulkDatasource to only
        # filter for bulkClassifications and not for bulkWorkspaces or
        # bulkWorkspaceRelationships:
        filters = bulk_classification.get("filters", [])
        if filters:
            data.filter(conditions=filters)

        # Sort the data frame if "sort" specified in payload. We may want to do this to have a
        # higher chance that rows with classifications names that may collapse into
        # one name are put into the same partition. This can avoid race conditions
        # between different Python threads.
        sort_fields = bulk_classification.get("sort", [])
        if sort_fields:
            self.logger.info(
                "Start sorting of data frame for bulk classifications based on fields (columns) -> %s...",
                str(sort_fields),
            )
            data.sort(sort_fields=sort_fields, inplace=True)
            self.logger.info(
                "Sorting of data frame for bulk classifications based on fields (columns) -> %s completed.",
                str(sort_fields),
            )

        # Check if duplicate rows for given fields should be removed. It is
        # important to do this after sorting as Pandas always keep the first occurance,
        # so ordering plays an important role in deduplication!
        unique_fields = bulk_classification.get("unique", [])
        if unique_fields:
            self.logger.info(
                "Starting deduplication of data frame for bulk classifications with unique fields -> %s. Size of data frame before deduplication -> %s.",
                str(unique_fields),
                str(len(data)),
            )
            data.deduplicate(unique_fields=unique_fields, inplace=True)
            self.logger.info(
                "Size of data frame after deduplication -> %s.",
                str(len(data)),
            )

        # Read the optional categories payload:
        categories = bulk_classification.get("categories", None)
        if not categories:
            self.logger.info(
                "Bulk classification payload has no category data! Will leave category attributes empty...",
            )

        # Get the operations to carry out during bulk processing.
        # Just doing a create is the default. Other options are
        # "update", "delete", "recreate":
        operations = bulk_classification.get("operations", ["create"])

        self.logger.info(
            "Bulk create classifications. Operations -> %s.",
            str(operations),
        )

        # See if bulkClassification definition has a specific thread number
        # otherwise it is read from a global environment variable:
        bulk_thread_number = int(
            bulk_classification.get("thread_number", BULK_THREAD_NUMBER),
        )

        partitions = data.partitionate(bulk_thread_number)

        # Create a list to hold the threads
        threads = []
        results = []

        # Create and start a thread for each partition
        for index, partition in enumerate(partitions, start=1):
            thread = threading.Thread(
                name=f"{section_name}_{index:02}",
                target=self.thread_wrapper,
                args=(
                    self.process_bulk_classifications_worker,
                    bulk_classification,
                    partition,
                    categories,
                    operations,
                    results,
                ),
            )
            # start a thread executing the process_bulk_classifications_worker() method below:
            self.logger.info("Starting thread -> '%s'...", str(thread.name))
            thread.start()
            threads.append(thread)
            # Avoid that all threads start at the exact same time with
            # potentially expired cookies that cases race conditions:
            time.sleep(1)
        # end for index, partition in enumerate(partitions, start=1)

        # Wait for all threads to complete
        for thread in threads:
            self.logger.info(
                "Waiting for thread -> '%s' to complete...",
                str(thread.name),
            )
            thread.join()
            self.logger.info("Thread -> '%s' has completed.", str(thread.name))

        if "classifications" not in bulk_classification:
            bulk_classification["classifications"] = {}

        # Print statistics for each thread. In addition,
        # check if all threads have completed without error / failure.
        # If there's a single failure in on of the thread results we
        # set 'success' variable to False.
        results.sort(key=lambda x: x["thread_name"])
        for result in results:
            self.logger.info(
                "Thread -> '%s' completed with overall status '%s'.",
                str(result["thread_name"]),
                "successful" if result["success"] else "failed",
            )
            self.logger.info(
                "Thread -> '%s' processed %s data rows with %s successful, %s failed, and %s skipped.",
                str(result["thread_name"]),
                str(
                    result["success_counter"] + result["failure_counter"] + result["skipped_counter"],
                ),
                str(result["success_counter"]),
                str(result["failure_counter"]),
                str(result["skipped_counter"]),
            )
            self.logger.info(
                "Thread -> '%s' created %s classifications, updated %s classifications, and deleted %s classifications.",
                str(result["thread_name"]),
                str(result["create_counter"]),
                str(result["update_counter"]),
                str(result["delete_counter"]),
            )

            if not result["success"]:
                success = False
            # Record all generated classifications. This should allow us
            # to restart in case of failures and avoid trying to
            # create classifications that have been created before.
            bulk_classification["classifications"].update(result["classifications"])
        self._log_header_callback(
            text="Completed processing of bulk classifications using data source -> '{}'".format(data_source_name),
            char="-",
        )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._bulk_classifications,
    )

    return success

process_bulk_classifications_worker(bulk_classification, partition, categories=None, operations=None, results=None)

Thread worker to create classificatins in bulk.

Each worker thread gets a partition of the rows that include the data required for the classification creation.

Parameters:

Name Type Description Default
bulk_classification dict

The payload of the bulkClassification.

required
partition DataFrame

Data partition with rows to process.

required
categories list

List of category dictionaries.

None
operations list

Defines which operations should be applyed on classifications. Possible values are "create", "update", "delete", "recreate".

None
results list

A mutable list of thread results.

None
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
29623
29624
29625
29626
29627
29628
29629
29630
29631
29632
29633
29634
29635
29636
29637
29638
29639
29640
29641
29642
29643
29644
29645
29646
29647
29648
29649
29650
29651
29652
29653
29654
29655
29656
29657
29658
29659
29660
29661
29662
29663
29664
29665
29666
29667
29668
29669
29670
29671
29672
29673
29674
29675
29676
29677
29678
29679
29680
29681
29682
29683
29684
29685
29686
29687
29688
29689
29690
29691
29692
29693
29694
29695
29696
29697
29698
29699
29700
29701
29702
29703
29704
29705
29706
29707
29708
29709
29710
29711
29712
29713
29714
29715
29716
29717
29718
29719
29720
29721
29722
29723
29724
29725
29726
29727
29728
29729
29730
29731
29732
29733
29734
29735
29736
29737
29738
29739
29740
29741
29742
29743
29744
29745
29746
29747
29748
29749
29750
29751
29752
29753
29754
29755
29756
29757
29758
29759
29760
29761
29762
29763
29764
29765
29766
29767
29768
29769
29770
29771
29772
29773
29774
29775
29776
29777
29778
29779
29780
29781
29782
29783
29784
29785
29786
29787
29788
29789
29790
29791
29792
29793
29794
29795
29796
29797
29798
29799
29800
29801
29802
29803
29804
29805
29806
29807
29808
29809
29810
29811
29812
29813
29814
29815
29816
29817
29818
29819
29820
29821
29822
29823
29824
29825
29826
29827
29828
29829
29830
29831
29832
29833
29834
29835
29836
29837
29838
29839
29840
29841
29842
29843
29844
29845
29846
29847
29848
29849
29850
29851
29852
29853
29854
29855
29856
29857
29858
29859
29860
29861
29862
29863
29864
29865
29866
29867
29868
29869
29870
29871
29872
29873
29874
29875
29876
29877
29878
29879
29880
29881
29882
29883
29884
29885
29886
29887
29888
29889
29890
29891
29892
29893
29894
29895
29896
29897
29898
29899
29900
29901
29902
29903
29904
29905
29906
29907
29908
29909
29910
29911
29912
29913
29914
29915
29916
29917
29918
29919
29920
29921
29922
29923
29924
29925
29926
29927
29928
29929
29930
29931
29932
29933
29934
29935
29936
29937
29938
29939
29940
29941
29942
29943
29944
29945
29946
29947
29948
29949
29950
29951
29952
29953
29954
29955
29956
29957
29958
29959
29960
29961
29962
29963
29964
29965
29966
29967
29968
29969
29970
29971
29972
29973
29974
29975
29976
29977
29978
29979
29980
29981
29982
29983
29984
29985
29986
29987
29988
29989
29990
29991
29992
29993
29994
29995
29996
29997
29998
29999
30000
30001
30002
30003
30004
30005
30006
30007
30008
30009
30010
30011
30012
30013
30014
30015
30016
30017
30018
30019
30020
30021
30022
30023
30024
30025
30026
30027
30028
30029
30030
30031
30032
30033
30034
30035
30036
30037
30038
30039
30040
30041
30042
30043
30044
30045
30046
30047
30048
30049
30050
30051
30052
30053
30054
30055
30056
30057
30058
30059
30060
30061
30062
30063
30064
30065
30066
30067
30068
30069
30070
30071
30072
30073
30074
30075
30076
30077
30078
30079
30080
30081
30082
30083
30084
30085
30086
30087
30088
30089
30090
30091
30092
30093
30094
30095
30096
30097
30098
30099
30100
30101
30102
30103
30104
30105
30106
30107
30108
30109
30110
30111
30112
30113
30114
30115
30116
30117
30118
30119
30120
30121
30122
30123
30124
30125
30126
30127
30128
30129
30130
30131
30132
30133
30134
30135
30136
30137
30138
30139
30140
30141
30142
30143
30144
30145
30146
30147
30148
30149
30150
30151
30152
30153
30154
30155
30156
30157
30158
30159
30160
30161
30162
30163
30164
30165
30166
30167
30168
30169
30170
30171
30172
30173
30174
30175
30176
30177
30178
30179
30180
30181
30182
30183
30184
30185
30186
30187
30188
30189
30190
30191
30192
30193
30194
30195
30196
30197
30198
30199
30200
30201
30202
30203
30204
30205
30206
30207
30208
30209
30210
30211
30212
30213
30214
30215
30216
30217
30218
30219
30220
30221
30222
30223
30224
30225
30226
30227
30228
30229
30230
30231
30232
30233
30234
30235
30236
30237
30238
30239
30240
30241
30242
30243
30244
30245
30246
30247
30248
30249
30250
30251
30252
30253
30254
30255
30256
30257
30258
30259
30260
30261
30262
30263
30264
30265
30266
30267
30268
30269
30270
30271
30272
30273
30274
30275
30276
30277
30278
30279
30280
30281
30282
30283
30284
30285
30286
30287
30288
30289
30290
30291
30292
30293
30294
30295
30296
30297
30298
30299
30300
30301
30302
30303
30304
30305
30306
30307
30308
30309
30310
30311
30312
30313
30314
30315
30316
30317
30318
30319
30320
30321
30322
30323
30324
30325
30326
30327
30328
30329
30330
30331
30332
30333
30334
30335
30336
30337
30338
30339
30340
30341
30342
30343
30344
30345
30346
30347
30348
30349
30350
30351
30352
30353
30354
30355
30356
30357
30358
30359
30360
30361
30362
30363
30364
30365
30366
30367
30368
30369
30370
30371
30372
30373
30374
30375
30376
30377
30378
30379
30380
30381
30382
30383
30384
30385
30386
30387
30388
30389
30390
30391
30392
30393
30394
30395
30396
30397
30398
30399
30400
30401
30402
30403
30404
30405
30406
30407
30408
30409
30410
30411
30412
30413
30414
30415
30416
30417
30418
30419
30420
30421
30422
30423
30424
30425
30426
30427
30428
30429
30430
30431
30432
30433
30434
30435
30436
30437
30438
30439
30440
30441
30442
30443
30444
30445
30446
30447
30448
30449
30450
30451
30452
30453
30454
30455
30456
30457
30458
30459
30460
30461
30462
30463
30464
30465
30466
30467
30468
30469
30470
30471
30472
30473
30474
30475
30476
30477
30478
30479
30480
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_classifications_worker")
def process_bulk_classifications_worker(
    self,
    bulk_classification: dict,
    partition: pd.DataFrame,
    categories: list | None = None,
    operations: list | None = None,
    results: list | None = None,
) -> None:
    """Thread worker to create classificatins in bulk.

    Each worker thread gets a partition of the rows that include
    the data required for the classification creation.

    Args:
        bulk_classification (dict):
            The payload of the bulkClassification.
        partition (pd.DataFrame):
            Data partition with rows to process.
        categories (list):
            List of category dictionaries.
        operations (list):
            Defines which operations should be applyed on classifications.
            Possible values are "create", "update", "delete", "recreate".
        results (list):
            A mutable list of thread results.

    """

    thread_id = threading.get_ident()
    thread_name = threading.current_thread().name

    self.logger.info(
        "Start working on data frame partition of size -> %s to bulk create classifications...",
        str(len(partition)),
    )

    # Avoid linter warnings - so make parameter default None while we
    # actually want ["create"] to be the default:
    if operations is None:
        operations = ["create"]

    result = {}
    result["thread_id"] = thread_id
    result["thread_name"] = thread_name
    result["success_counter"] = 0
    result["failure_counter"] = 0
    result["skipped_counter"] = 0
    result["create_counter"] = 0
    result["update_counter"] = 0
    result["delete_counter"] = 0
    result["classifications"] = {}
    result["success"] = True

    # Check if classifications have been processed before, e.i. testing
    # if a "classifications" key exists and if it is pointing to a non-empty list.
    # Additionally we check that classification updates are not enforced:
    if (
        bulk_classification.get("classifications")
        and "update" not in operations
        and "delete" not in operations
        and "recreate" not in operations
    ):
        existing_classifications = bulk_classification["classifications"]
        self.logger.info(
            "Found %s already processed classifications. Try to complete the job...",
            str(len(existing_classifications)),
        )
    else:
        existing_classifications = {}

    # See if external creation and modification dates are in the payload data:
    external_modify_date_field = bulk_classification.get("external_modify_date")
    external_create_date_field = bulk_classification.get("external_create_date")

    # See if we have a key field to uniquely identify an existing document:
    key_field = bulk_classification.get("key")

    # Get dictionary of replacements for bulk classification creations
    # this we will be used of all places data is read from the
    # data frame. Each dictionary item has the field name as the
    # dictionary key and a list of regular expressions as dictionary value
    replacements = bulk_classification.get("replacements")

    # In case the name cannot be resolved we allow to
    # specify an alternative name field in the payload.
    name_field = bulk_classification.get("name")

    # In case the name cannot be resolved we allow to
    # specify an alternative name field in the payload.
    name_field_alt = bulk_classification.get("name_alt")

    # In case the description cannot be resolved we allow to
    # specify an alternative description field in the payload.
    description_field = bulk_classification.get("description")

    # In case the description cannot be resolved we allow to
    # specify an alternative description field in the payload.
    description_field_alt = bulk_classification.get("description_alt")

    # Fetch the nickname field from the payload (if it is specified):
    nickname_field = bulk_classification.get("nickname")

    # In case the nickname cannot be resolved we allow to
    # specify an alternative nickname field in the payload.
    nickname_field_alt = bulk_classification.get("nickname_alt")

    # Nicknames are very limited in terms of allowed characters.
    # For nicknames we need additional regexp as we need to
    # replace all non-alphanumeric, non-space characters with ""
    # We also preserve hyphens in the first step to replace
    # them below with underscores. This is important to avoid
    # that different spellings of names produce different nicknames.
    # We want spellings with spaces match spellings with hyphens.
    # For this the classification names have a regexp "-| " in the payload.
    nickname_additional_regex_list = [r"[^\w\s-]"]

    total_rows = len(partition)

    # Process all rows in the partition that was given to this thread:
    for index, row in partition.iterrows():
        # Calculate percentage of completion:
        percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

        self.logger.info(
            "Processing data row -> %s for bulk classification creation (%.2f %% complete)...",
            str(index),
            percent_complete,
        )

        # Clear variables to esure clean state for each row:
        classification_id = None

        # Determine the classification name:
        classification_name = self.replace_bulk_placeholders(
            input_string=name_field,
            row=row,
            replacements=replacements,
        )
        # If we cannot get the classification name from the
        # name_field we try the alternative name field
        # (if specified in payload via "name_alt"):
        if not classification_name and name_field_alt:
            self.logger.debug(
                "Row -> %s does not have the data to resolve the placeholders in classification name -> %s! Trying alternative name field -> %s...",
                str(index),
                name_field,
                name_field_alt,
            )
            classification_name = self.replace_bulk_placeholders(
                input_string=name_field_alt,
                row=row,
                replacements=replacements,
            )

        if not classification_name:
            self.logger.warning(
                "Row -> %s does not have the required data to resolve -> %s for the classification name!%s",
                str(index),
                name_field,
                " There's no alternative field name given in the payload (via 'name_alt')."
                if not name_field_alt
                else " The alternative field -> '{}' could not be resolved either!".format(
                    name_field_alt,
                ),
            )
            result["skipped_counter"] += 1
            continue

        # Cleanse the classification name (allowed characters, maximum length):
        classification_name = OTCS.cleanse_item_name(classification_name)

        # Check if classification has been created before (either in this run
        # or in a former run of the customizer):
        if (
            classification_name in existing_classifications  # processed in former run?
            or classification_name in result["classifications"]  # processed in current run?
        ):
            self.logger.info(
                "Classification -> '%s' does already exist. Skipping...",
                classification_name,
            )
            result["skipped_counter"] += 1
            continue

        # Determine the description field:
        if description_field:
            description = self.replace_bulk_placeholders(
                input_string=description_field,
                row=row,
            )
            # If we cannot get the description from the
            # description_field we try the alternative description field
            # (if specified in payload via "description_alt"):
            if not description and description_field_alt:
                self.logger.debug(
                    "Row -> %s does not have the data to resolve the placeholders in classification description -> %s! Trying alternative description field -> %s...",
                    str(index),
                    name_field,
                    description_field_alt,
                )
                description = self.replace_bulk_placeholders(
                    input_string=description_field_alt,
                    row=row,
                )
        else:
            description = ""

        # Create a copy of the mutable operations list as we may
        # want to modify it per row:
        row_operations = list(operations)

        # Check if all data conditions to create the classification are met
        conditions = bulk_classification.get("conditions")
        if conditions:
            evaluated_condition = self.evaluate_conditions(
                conditions=conditions,
                row=row,
                replacements=replacements,
            )
            if not evaluated_condition:
                self.logger.info(
                    "Condition for bulk classification row -> %s not met. Skipping row for classification creation...",
                    str(index),
                )
                result["skipped_counter"] += 1
                continue

        # Check if all data conditions to create or recreate the classification are met:
        if "create" in row_operations or "recreate" in row_operations:
            conditions_create = bulk_classification.get("conditions_create")
            if conditions_create:
                evaluated_conditions_create = self.evaluate_conditions(
                    conditions=conditions_create,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_conditions_create:
                    self.logger.info(
                        "Create condition for bulk classification row -> %s not met. Excluding create operation...",
                        str(index),
                    )
                    if "create" in row_operations:
                        row_operations.remove("create")
                    if "recreate" in row_operations:
                        row_operations.remove("recreate")
            elif "recreate" in row_operations:
                # we still create and recreate without conditions_create.
                # But give a warning for 'recreate' without condition.
                self.logger.warning(
                    "No create condition provided but 'recreate' operation requested. Recreating all existing classifications!",
                )

        # Check if all data conditions to delete the classification are met:
        if "delete" in row_operations:
            conditions_delete = bulk_classification.get("conditions_delete")
            if conditions_delete:
                evaluated_conditions_delete = self.evaluate_conditions(
                    conditions=conditions_delete,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_conditions_delete:
                    self.logger.info(
                        "Delete condition for bulk classification row -> %s not met. Excluding delete operation...",
                        str(index),
                    )
                    row_operations.remove("delete")
            else:  # without delete_conditions we don't delete!!
                self.logger.warning(
                    "Delete operation requested for bulk classifications but conditions for deletion are missing! (specify 'conditions_delete')!",
                )
                row_operations.remove("delete")

        # Check if all data conditions to delete the classification are met:
        if "update" in row_operations:
            conditions_update = bulk_classification.get("conditions_update")
            if conditions_update:
                evaluated_conditions_update = self.evaluate_conditions(
                    conditions=conditions_update,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_conditions_update:
                    self.logger.info(
                        "Update condition for bulk classification row -> %s not met. Excluding update operation...",
                        str(index),
                    )
                    row_operations.remove("update")

        # Determine the external modification date (if any):
        if external_modify_date_field:
            external_modify_date = self.replace_bulk_placeholders(
                input_string=external_modify_date_field,
                row=row,
            )
        else:
            external_modify_date = None

        # Determine the external creation date (if any):
        if external_create_date_field:
            external_create_date = self.replace_bulk_placeholders(
                input_string=external_create_date_field,
                row=row,
            )
        else:
            external_create_date = None

        # Determine the key field (if any):
        key = self.replace_bulk_placeholders(input_string=key_field, row=row) if key_field else None

        # check if classification with this nickname does already exist.
        # we also store the nickname to assign it to the new classification:
        if nickname_field:
            nickname = self.replace_bulk_placeholders(
                input_string=nickname_field,
                row=row,
                replacements=replacements,
                additional_regex_list=nickname_additional_regex_list,
            )
            # If we cannot get the nickname from the
            # nickname_field we try the alternative nickname field
            # (if specified in payload via "nickname_alt"):
            if not nickname and nickname_field_alt:
                nickname = self.replace_bulk_placeholders(
                    input_string=nickname_field_alt,
                    row=row,
                    replacements=replacements,
                    additional_regex_list=nickname_additional_regex_list,
                )

            # Nicknames for sure should not have leading or trailing spaces:
            nickname = nickname.strip()
            # Nicknames for sure are not allowed to include spaces:
            nickname = nickname.replace(" ", "_")
            # We also want to replace hyphens with underscores
            # to make sure that classification name spellings with
            # spaces and with hyphens are mapped to the same
            # classification nicknames (aligned with the classification names
            # that have a regexp rule for this in the payload):
            nickname = nickname.replace("-", "_")
            nickname = nickname.replace("___", "_")
            nickname = nickname.lower()
            response = self._otcs_frontend.get_node_from_nickname(nickname=nickname)
            if response:
                found_classification_name = self._otcs_frontend.get_result_value(
                    response=response,
                    key="name",
                )
                found_classification_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if found_classification_name != classification_name:
                    self.logger.warning(
                        "Clash of nicknames for classification -> '%s' and classification -> '%s' (%s)!",
                        classification_name,
                        found_classification_name,
                        found_classification_id,
                    )
                # Only skip, if classification 'update' or 'delete' is NOT requested:
                elif "update" not in row_operations and "delete" not in row_operations:
                    self.logger.info(
                        "Classification -> '%s' with nickname -> '%s' does already exist (found -> '%s'). No update or delete operations requested or allowed. Skipping...",
                        classification_name,
                        nickname,
                        found_classification_name,
                    )
                    result["skipped_counter"] += 1
                    continue
        # end if nickname_field
        else:
            nickname = None

        # Based on the evaluation of conditions_create, conditions_delete,
        # and conditions_update we could end up with an empty operations list.
        # In such cases we skip the further processing of this row:
        if not row_operations:
            self.logger.info(
                "Skip classification -> '%s' as row operations is empty (no create, update, delete or recreate). This may be because conditions_create, conditions_delete, and conditions_update have been evaluated to false for this row.",
                classification_name,
            )
            result["skipped_counter"] += 1
            continue

        self.logger.info(
            "Bulk process classification -> '%s' using effective operations -> %s...",
            classification_name,
            str(row_operations),
        )

        self.logger.info(
            "Check if classification -> '%s' does already exist...",
            classification_name,
        )

        # Initialize variables - this is important!
        classification_old_name = None
        classification_id = None
        handle_name_clash = False
        classification_modify_date = None
        parent_id = None

        # We create a copy list to not modify original payload
        classification_path = list(bulk_classification.get("path", []))
        result_placeholders = self.replace_bulk_placeholders_list(
            input_list=classification_path,
            row=row,
            replacements=replacements,
        )
        if not result_placeholders:
            classification_path = None

        # We have 4 cases to differentiate:

        # 1. key given + key found = name irrelevant, item exist
        # 2. key given + key not found = if name exist it is a name clash
        # 3. no key given + name found = item exist
        # 4. no key given + name not found = item does not exist

        # We are NOT trying to compensate for a failed key lookup with a name lookup!
        # Names are only relevant if no key is in payload!

        if key:
            # if we have a key we need to also know which category attribute is
            # storing the value for the key:
            key_attribute = next(
                (cat_attr for cat_attr in categories if cat_attr.get("is_key", False) is True),
                None,
            )
            if not key_attribute:
                self.logger.error(
                    "Bulk Classification has a key -> '%s' defined but none of the category attributes is marked as key ('is_key' is missing)!",
                    key,
                )
                result["success"] = False
                result["failure_counter"] += 1
                continue
            cat_name = key_attribute.get("name", None)
            att_name = key_attribute.get("attribute", None)
            set_name = key_attribute.get("set", None)

            # Get the parent classification element:
            response = self._otcs_frontend.get_node_by_volume_and_path(
                volume_type=self._otcs_frontend.VOLUME_TYPE_CLASSIFICATION_VOLUME,
                path=classification_path,
                create_path=True,
            )
            parent_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
            if parent_id:
                # Try to find the node that has the given key attribute value:
                response = self._otcs_frontend.lookup_nodes(
                    parent_node_id=parent_id,
                    category=cat_name,
                    attribute=att_name,
                    attribute_set=set_name,
                    value=key,
                )
                classification_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
            else:
                # if we couldn't determine the parent ID this means there are
                # now classification instances for this classification type. Then we set
                # classification_id = None and let the code go into the else case below:
                classification_id = None

            if classification_id:
                # Case 1: key given + key found = name irrelevant, item exist
                classification_old_name = self._otcs_frontend.get_result_value(
                    response=response,
                    key="name",
                )
                self.logger.info(
                    "Found existing classification -> %s (%s) in classification with ID -> %s using key value -> '%s', category -> '%s', and attribute -> '%s'.",
                    classification_old_name,
                    classification_id,
                    parent_id,
                    key,
                    cat_name,
                    att_name,
                )
                if classification_old_name != classification_name:
                    self.logger.info(
                        "Existing classification has the old name -> '%s' which is different from the new name -> '%s.'",
                        classification_old_name,
                        classification_name,
                    )
                else:
                    classification_old_name = None
                # We get the modify date of the existing classification.
                classification_modify_date = self._otcs_frontend.get_result_value(
                    response=response,
                    key="modify_date",
                )
            else:
                # Case 2: key given + key not found = if name exist it is a name clash
                self.logger.info(
                    "Couldn't find existing classification with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                    key,
                    cat_name,
                    att_name,
                    parent_id,
                )
                handle_name_clash = True
            # end if key_attribute
        # end if key
        else:
            # Get the parent classification element:
            response = self._otcs_frontend.get_node_by_volume_and_path(
                volume_type=self._otcs_frontend.VOLUME_TYPE_CLASSIFICATION_VOLUME,
                path=classification_path,
            )
            parent_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
            # If we haven't a key we try by parent + name
            response = self._otcs_frontend.get_node_by_parent_and_name(
                parent_id=parent_id,
                name=classification_name,
            )
            classification_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
            if classification_id:
                # Case 3: no key given + name found = item exist
                self.logger.info(
                    "Found existing classification -> '%s' (%s).",
                    classification_name,
                    classification_id,
                )
                # We get the modify date of the existing classification.
                classification_modify_date = self._otcs_frontend.get_result_value(
                    response=response,
                    key="modify_date",
                )
            else:
                # Case 4: no key given + name not found = item does not exist
                self.logger.info(
                    "No existing classification with name -> '%s' in path -> %s.",
                    classification_name,
                    classification_path,
                )

        # Check if we want to recreate an existing classification
        # then we handle the "delete" part of "recreate" first:
        if classification_id and "recreate" in row_operations:
            response = self._otcs_frontend.delete_node(
                node_id=classification_id,
                purge=True,
            )
            if not response:
                self.logger.error(
                    "Failed to recreate existing classification -> '%s' (%s)! Delete failed.",
                    (classification_name if classification_old_name is None else classification_old_name),
                    classification_id,
                )
                result["success"] = False
                result["failure_counter"] += 1
                continue
            result["delete_counter"] += 1
            self.logger.info(
                "Successfully deleted existing classification -> '%s' (%s) as part of the recreate operation.",
                (classification_name if classification_old_name is None else classification_old_name),
                classification_id,
            )
            classification_id = None

        # Check if classification does not exists - then we create a new classification
        # if this is requested ("create" or "recreate" value in operations list in payload)
        if not classification_id and ("create" in row_operations or "recreate" in row_operations):
            # for Case 2 (we looked up the classification by key but could have name collisions)
            # we need to see if we have a name collision:
            if handle_name_clash and parent_id:
                response = self._otcs_frontend.check_node_name(
                    parent_id=parent_id,
                    node_name=classification_name,
                )
                # result is a list of names that clash - if it is empty we have no clash
                if response and response["results"]:
                    # We add the suffix with the key which should be unique:
                    self.logger.warning(
                        "Classification -> '%s' does already exist in folder with ID -> %s and we need to handle the name clash by using name -> '%s'",
                        classification_name,
                        parent_id,
                        classification_name + " (" + key + ")",
                    )
                    classification_name = classification_name + " (" + key + ")"

            # If category data is in payload we substitute
            # the values with data from the current data row:
            if categories:
                # Make sure the threads are not changing data structures that
                # are shared between threads. categories is a list of dicts.
                # list and dicts are "mutable" data structures in Python!
                worker_categories = self.process_bulk_categories(
                    row=row,
                    index=index,
                    categories=categories,
                    replacements=replacements,
                )
                # classification_category_data = self.prepare_item_create_form(
                #     parent_id=parent_id,
                #     categories=worker_categories,
                #     subtype=self._otcs_frontend.ITEM_TYPE_CLASSIFICATION,
                # )
                classification_category_data = self.prepare_category_data(
                    categories_payload=worker_categories,
                    source_node_id=parent_id,
                )
                if not classification_category_data:
                    self.logger.error(
                        "Failed to prepare the category data for new classification -> '%s'!",
                        classification_name,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue  # for index, row in partition.iterrows()
            else:
                classification_category_data = {}

            self.logger.info(
                "Bulk create classification -> '%s'...",
                classification_name,
            )

            # Create the classification with all provided information:
            response = self._otcs_frontend.create_item(
                parent_id=parent_id,
                item_type=self._otcs_frontend.ITEM_TYPE_CLASSIFICATION,
                item_name=classification_name,
                item_description=description,
                category_data=classification_category_data,
                external_create_date=external_create_date,
                external_modify_date=external_modify_date,
                show_error=False,
            )
            if response is None:
                # Potential race condition: see if the classification has been created by a concurrent thread.
                # So we better check if the classification is there even if the create_item() call delivered
                # a 'None' response:
                response = self._otcs_frontend.get_node_by_parent_and_name(
                    parent_id=parent_id,
                    name=classification_name,
                )
            classification_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
            if not classification_id:
                self.logger.error(
                    "Failed to bulk create classification -> '%s' under parent with ID -> %s!",
                    classification_name,
                    parent_id,
                )
                result["success"] = False
                result["failure_counter"] += 1
                continue

            # Set the categories separately:
            # if classification_category_data:
            #     response = self._otcs_frontend.update_item(
            #         node_id=classification_id,
            #         category_data=classification_category_data,
            #     )
            self.logger.info(
                "Created classification -> '%s' with ID -> %s!",
                classification_name,
                classification_id,
            )
            result["create_counter"] += 1

        # end if not classification_id and "create" or "recreate" in row_operations

        # If "updates" are an requested row operation we update the existing classification with
        # fresh metadata from the payload. Additionally we check the external
        # modify date to support incremental load for content that has really
        # changed.
        # In addition we check that "delete" is not requested as otherwise it will
        # never go in elif "delete" ... below (and it does not make sense to update a classification
        # that is deleted in the next step...)
        elif (
            classification_id
            and "update" in row_operations
            and "delete" not in row_operations  # note the NOT !
            and OTCS.date_is_newer(
                date_old=classification_modify_date,
                date_new=external_modify_date,
            )
        ):
            # get the specific update operations given in the payload
            # if not specified we do all 4 update operations (name, description, categories and nickname)
            update_operations = bulk_classification.get(
                "update_operations",
                ["name", "description", "categories", "nickname"],
            )

            # If category data is in payload we substitute
            # the values with data from the current data row.
            # This is only done if "categories" update is not
            # excluded from the update_operations:
            if categories and "categories" in update_operations:
                # Make sure the threads are not changing data structures that
                # are shared between threads. categories is a list of dicts.
                # list and dicts are "mutable" data structures in Python!
                worker_categories = self.process_bulk_categories(
                    row=row,
                    index=index,
                    categories=categories,
                    replacements=replacements,
                )
                # classification_category_data = self.prepare_item_create_form(
                #     parent_id=parent_id,
                #     categories=worker_categories,
                #     subtype=self._otcs_frontend.ITEM_TYPE_CLASSIFICATION,
                # )
                # Transform the payload structure into the format
                # the OTCS REST API requires:
                classification_category_data = self.prepare_category_data(
                    categories_payload=worker_categories,
                    source_node_id=classification_id,
                )
                if not classification_category_data:
                    self.logger.error(
                        "Failed to prepare the updated category data for classification -> '%s'!",
                        classification_name,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue  # for index, row in partition.iterrows()
            # end if categories
            else:
                classification_category_data = {}

            self.logger.info(
                "Update existing classification -> '%s' (%s) with operations -> %s...",
                classification_old_name if classification_old_name else classification_name,
                str(classification_id),
                str(update_operations),
            )
            # Update the classification with all provided information:
            response = self._otcs_frontend.update_item(
                classification_id=classification_id,
                item_name=(classification_name if "name" in update_operations else None),
                item_description=(description if "description" in update_operations else None),
                category_data=(classification_category_data if "categories" in update_operations else None),
                external_create_date=external_create_date,
                external_modify_date=external_modify_date,
                show_error=True,
            )
            if not response:
                self.logger.error(
                    "Failed to update existing classification -> '%s' (%s)!",
                    (classification_old_name if classification_old_name else classification_name),
                    classification_id,
                )
                result["success"] = False
                result["failure_counter"] += 1
                continue
            self.logger.info(
                "Updated existing classification -> '%s' (%s).",
                classification_name
                if "name" in update_operations or not classification_old_name
                else classification_old_name,
                classification_id,
            )
            result["update_counter"] += 1

        # end elif "update" in row_operations...
        elif classification_id and "delete" in row_operations:
            # We delete with immediate purging to keep recycle bin clean
            # and to not run into issues with nicknames used in deleted items:
            response = self._otcs_frontend.delete_node(
                node_id=classification_id,
                purge=True,
            )
            if not response:
                self.logger.error(
                    "Failed to delete existing classification -> '%s' (%s)!",
                    (classification_old_name if classification_old_name else classification_name),
                    classification_id,
                )
                result["success"] = False
                result["failure_counter"] += 1
                continue
            self.logger.info(
                "Successfully deleted existing classification -> '%s' (%s).",
                classification_old_name if classification_old_name else classification_name,
                classification_id,
            )
            result["delete_counter"] += 1
            classification_id = None
        # end elif classification_id and "delete" in row_operations

        # this is the plain old "it does exist and we just skip it case":
        elif classification_id:
            result["skipped_counter"] += 1
            self.logger.info(
                "Skipped existing classification -> '%s' (%s).",
                classification_old_name if classification_old_name else classification_name,
                classification_id,
            )
        # this is the case where we just want to operate on existing classifications (update or delete)
        # but they do not exist:
        elif not classification_id and ("update" in row_operations or "delete" in row_operations):
            result["skipped_counter"] += 1
            self.logger.info(
                "Skipped update/delete of non-existing classification -> '%s'.",
                classification_old_name if classification_old_name else classification_name,
            )

        # The following code is executed for all operations
        # (create, recreate, update, delete):

        # Check if we want to set / update the nickname of the classification.
        # Precondition is we have determined a nickname, we have the classification ID
        # and the update of the nickname is either required (create, recreate)
        # or requested (update_operations include "nickname"):
        if (
            nickname
            and classification_id
            and (
                "create" in row_operations
                or "recreate" in row_operations
                or ("update" in row_operations and "nickname" in update_operations)
            )
        ):
            response = self._otcs_frontend.set_node_nickname(
                node_id=classification_id,
                nickname=nickname,
                show_error=True,
            )
            if not response:
                self.logger.error(
                    "Failed to assign nickname -> '%s' to classification -> '%s'!",
                    nickname,
                    classification_name,
                )

        # Depending on the bulk operations (create, update, delete)
        # and the related conditions it may well be that classification_id is None.
        # Only if classification ID is not none we want to count this row as success:
        if classification_id is not None:
            result["success_counter"] += 1
            # Record the classification name and ID to allow to read it from failure file
            # and speedup the process.
            result["classifications"][classification_name] = classification_id
    # end for index...

    self.logger.info("End working...")

    results.append(result)

process_bulk_datasource(data_source_name, force_reload=True)

Process a data source that is given by a payload element.

Parse its properties and deliver a 'Data' object which is a wrapper for a Pandas data frame.

Parameters:

Name Type Description Default
data_source_name str

The data source name.

required
force_reload bool

Force a reload of the data source if True.

True

Returns:

Type Description
Data | None

Data | None: Data source object of type Data. None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource")
def process_bulk_datasource(
    self,
    data_source_name: str,
    force_reload: bool = True,
) -> Data | None:
    """Process a data source that is given by a payload element.

    Parse its properties and deliver a 'Data' object which is a wrapper for
    a Pandas data frame.

    Args:
        data_source_name (str):
            The data source name.
        force_reload (bool):
            Force a reload of the data source if True.

    Returns:
        Data | None:
            Data source object of type Data. None in case of an error.

    """

    if not data_source_name:
        self.logger.error("Missing data source name!")
        return None

    self._log_header_callback(
        text="Process Bulk Data Source -> '{}'".format(data_source_name),
        char="-",
    )

    self.logger.info(
        "Found specified data source name -> '%s'. Lookup the data source payload...",
        data_source_name,
    )
    data_source = next(
        (item for item in self._bulk_datasources if item["name"] == data_source_name),
        None,
    )
    if not data_source:  # does this data source not exist?
        self.logger.error(
            "Cannot find specified data source -> '%s' in payload!",
            data_source_name,
        )
        return None

    # Check if data has already been loaded for the data source:
    if "data" in data_source and not force_reload:
        self.logger.info(
            "Data for data source -> '%s' is already loaded and reload is not enforced. Return existing data...",
            data_source_name,
        )
        return data_source["data"]
    elif force_reload:
        self.logger.info(
            "Reload of data from data source -> '%s' is enforced. Building data...",
            data_source_name,
        )
    else:
        self.logger.info(
            "Data for data source -> '%s' is not yet available. Building data...",
            data_source_name,
        )
        # Try to load the data source in the Admin Personal Workspace for investigations:
        if data_source.get("load_data_source", False):
            self.logger.info(
                "Payload allows to reload existing data source. Check if the data source -> '%s' can be reloaded...",
                data_source_name,
            )
            if self.read_data_source_file(data_source=data_source):
                return data_source["data"]
            else:
                self.logger.warning(
                    "Couldn't load the data source -> '%s' from file. Fall back to reloading it from the source...",
                    data_source_name,
                )

    data_source_type = data_source.get("type", None)
    if not data_source_type:
        self.logger.error(
            "Data source needs a type parameter. This is new - you may need to add it to your bulkDataSource payload definition file! Cannot load data.",
        )
        return None

    match data_source_type:
        case "excel":
            data = self.process_bulk_datasource_excel(data_source=data_source)
            if data is None:
                self.logger.error("Failure during load of Excel file!")
                return None
        case "servicenow":
            data = self.process_bulk_datasource_servicenow(data_source=data_source)
            if data is None:
                self.logger.error("Failure during load of ServiceNow articles!")
                return None
        case "otmm":
            data = self.process_bulk_datasource_otmm(data_source=data_source)
            if data is None:
                self.logger.error(
                    "Failure during load of OpenText Media Management assets!",
                )
                return None
        case "otcs":
            data = self.process_bulk_datasource_otcs(data_source=data_source)
            if data is None:
                self.logger.error(
                    "Failure during load of OpenText Content Server items!",
                )
                return None
        case "pht":
            data = self.process_bulk_datasource_pht(data_source=data_source)
            if data is None:
                self.logger.error(
                    "Failure during load of OpenText Product Hierarchy (PHT)!",
                )
                return None
        case "nhc":
            data = self.process_bulk_datasource_nhc(data_source=data_source)
            if data is None:
                self.logger.error(
                    "Failure during load of National Hurricane Center data (NHC)!",
                )
                return None
        case "json":
            data = self.process_bulk_datasource_json(data_source=data_source)
            if data is None:
                self.logger.error("Failure during load of JSON data source!")
                return None
        case "xml":
            data = self.process_bulk_datasource_xml(data_source=data_source)
            if data is None:
                self.logger.error("Failure during load of XML data source!")
                return None
        case "csv":
            data = self.process_bulk_datasource_csv(data_source=data_source)
            if data is None:
                self.logger.error("Failure during load of CSV data source!")
                return None
        case "web":
            data = self.process_bulk_datasource_web(data_source=data_source)
            if data is None:
                self.logger.error("Failure during load of Web data source!")
                return None
        case "filesystem":
            data = self.process_bulk_datasource_filesystem(data_source=data_source)
            if data is None:
                self.logger.error("Failure during load of Web data source!")
                return None
        case _:
            self.logger.error(
                "Illegal data source type. Types supported: 'excel', 'servicenow', 'otmm', 'otcs', 'pht', 'json', 'csv', 'xml', 'web', or 'filesystem'",
            )
            return None

    if data.get_data_frame() is None or data.get_data_frame().empty:
        self.logger.warning("Data source is empty - nothing loaded.")
        return None

    self.logger.info(
        "Data Frame for source -> '%s' has %s row(s) and %s column(s) after data loading.",
        data_source_name,
        data.get_data_frame().shape[0],
        data.get_data_frame().shape[1],
    )

    cleansings = data_source.get("cleansings", {})
    columns_to_drop = data_source.get("columns_to_drop", [])
    columns_to_keep = data_source.get("columns_to_keep", [])
    columns_to_add = data_source.get("columns_to_add", [])
    columns_to_add_list = data_source.get("columns_to_add_list", [])
    columns_to_add_concat = data_source.get("columns_to_add_concat", [])
    columns_to_add_table = data_source.get("columns_to_add_table", [])
    filters = data_source.get("filters", [])
    if not filters:
        # Backward compatibility. This used to be called "conditions" before
        # but we don't want to mix this with the conditions on row level
        # we have for bulkWorkspaces and bulkDocuments:
        filters = data_source.get("conditions", [])
    explosions = data_source.get("explosions", [])

    # Cleanup data if specified in data_source. We do this first!
    if cleansings:
        self.logger.info(
            "Start cleansing for data source -> '%s'...",
            data_source_name,
        )
        data.cleanse(cleansings=cleansings)
        self.logger.info(
            "Cleansing for data source -> '%s' completed...",
            data_source_name,
        )

    # Add columns if specified in data_source:
    for add_column in columns_to_add:
        if "source_column" not in add_column or "name" not in add_column:
            self.logger.error(
                "Add columns is missing name or source column. Column will not be added.",
            )
            continue
        data.add_column(
            source_column=add_column["source_column"],
            new_column=add_column["name"],
            reg_exp=add_column.get("regex", add_column.get("reg_exp", None)),
            prefix=add_column.get("prefix", ""),
            suffix=add_column.get("suffix", ""),
            length=add_column.get("length", None),
            group_chars=add_column.get("group_chars", None),
            group_separator=add_column.get("group_separator", "."),
        )

    # Add columns with list values from a list of other columns
    # if specified in data_source:
    for add_column in columns_to_add_list:
        if "source_columns" not in add_column or "name" not in add_column:
            self.logger.error(
                "Add list columns is missing name or source columns. Column will not be added.",
            )
            continue
        data.add_column_list(
            source_columns=add_column["source_columns"],
            new_column=add_column["name"],
        )

    # Add columns with list values from a list of other columns
    # if specified in data_source:
    for add_column in columns_to_add_concat:
        if "source_columns" not in add_column or "name" not in add_column:
            self.logger.error(
                "Add concatenation columns is missing name or source columns. Column will not be added.",
            )
            continue
        data.add_column_concat(
            source_columns=add_column["source_columns"],
            new_column=add_column["name"],
            concat_char=add_column.get("concat_chars", ""),
            lower=add_column.get("lower", False),
            upper=add_column.get("upper", False),
            capitalize=add_column.get("capitalize", False),
            title=add_column.get("title", False),
        )

    # Add columns with list values from a list of other columns
    # if specified in data_source:
    for add_column in columns_to_add_table:
        if "source_columns" not in add_column or "name" not in add_column:
            self.logger.error(
                "Add table columns is missing name or source columns. Column will not be added.",
            )
            continue
        data.add_column_table(
            source_columns=add_column["source_columns"],
            new_column=add_column["name"],
            delimiter=add_column.get("list_splitter", ","),
        )

    # Drop columns if specified in data_source:
    if columns_to_drop:
        data.drop_columns(columns_to_drop)

    # Keep only selected columns if specified in data_source:
    if columns_to_keep:
        data.keep_columns(columns_to_keep)

    # Check if fields with list substructures should be exploded:
    for explosion in explosions:
        # The explode field parameter can be a string or a list.
        # Exploding multiple fields at once avoids combinatorial explosions -
        # this is VERY different from exploding columns one after the other!
        if "explode_field" not in explosion and "explode_fields" not in explosion:
            self.logger.error("Missing explosion field(s)!")
            continue
        # We support both syntax (singular + plural):
        explode_fields = (
            explosion["explode_field"] if "explode_fields" not in explosion else explosion["explode_fields"]
        )

        flatten_fields = explosion.get("flatten_fields", [])
        split_string_to_list = explosion.get("split_string_to_list", False)
        list_splitter = explosion.get("list_splitter", None)
        self.logger.info(
            "Starting explosion of data source -> '%s' by field(s) -> '%s' (type -> '%s'). Size of data frame before explosion -> %s.",
            data_source_name,
            str(explode_fields),
            type(explode_fields),
            str(len(data)),
        )
        data.explode_and_flatten(
            explode_fields=explode_fields,
            flatten_fields=flatten_fields,
            make_unique=False,
            split_string_to_list=split_string_to_list,
            separator=list_splitter,
            reset_index=True,
        )
        self.logger.info("Size of data frame after explosion -> %s.", str(len(data)))

    # Keep only selected rows if filters are specified in data_source
    # We have this after "explosions" to allow access to subfields as well:
    if filters:
        data.filter(conditions=filters)

    # Keep the Data Frame for later lookups:
    data_source["data"] = data

    self._log_header_callback(
        text="Completed Bulk Data Source -> '{}'".format(data_source_name),
        char="-",
    )

    # Save the data source in the Admin Personal Workspace for investigations:
    if data_source.get("save_data_source", False):
        self.write_data_source_file(data_source=data_source)

    return data

process_bulk_datasource_csv(data_source)

Load data from CSV files (comma-separated values) into the data frame of the Data class.

See helper/data.py

Parameters:

Name Type Description Default
data_source dict

Payload dict element for the data source.

required

Returns:

Type Description
Data | None

Data | None: Data class that includes a Pandas data frame. None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_csv")
def process_bulk_datasource_csv(self, data_source: dict) -> Data | None:
    """Load data from CSV files (comma-separated values) into the data frame of the Data class.

    See helper/data.py

    Args:
        data_source (dict):
            Payload dict element for the data source.

    Returns:
        Data | None:
            Data class that includes a Pandas data frame. None in case of an error.

    """

    # 1. Read and validate values from the data source payload:
    csv_files = data_source.get("csv_files", [])
    if not csv_files:
        self.logger.error(
            "CSV files not specified in payload of bulk data source. Cannot load data!",
        )
        return None
    csv_delimiter = data_source.get("csv_delimiter", ",")
    # The header index is an integer. The first row / line has the index 0.
    # But 0 is not the default! The default is the CSV does not have a header line
    # at all (None):
    csv_header_index = data_source.get("csv_header_index")
    csv_column_names = data_source.get("csv_column_names")
    csv_use_columns = data_source.get("csv_use_columns")

    # If no headers is specified it means the CSV does not have column
    # names in a row (typically row 0 = first row). If we also don't
    # have the names for the columns we will end with having coumn names
    # that a index values (1, 2, 3, ...). This may not be what the payload
    # author wants - so we issue a warning:
    if not csv_column_names and csv_header_index is None:  # "is None" is important here as the index can be 0
        self.logger.warning(
            "CSV loader parameters are missing both, header line index (no 'csv_header_index') and column names (no 'csv_column_names'). This could lead to numeric column names.",
        )

    # 2. Initialize Data object:
    data = Data(logger=self.logger)

    # 3. Iterate CSV files and load data into Data object:
    for csv_file in csv_files:
        if not data.load_csv_data(
            csv_path=csv_file,
            delimiter=csv_delimiter,
            names=csv_column_names,
            header=csv_header_index,
            usecols=csv_use_columns,
        ):
            self.logger.error("failed to load CSV file -> '%s'!", csv_file)
            return None

    return data

process_bulk_datasource_excel(data_source)

Load data from Excel files into the data frame of the Data class.

See helper/data.py

Parameters:

Name Type Description Default
data_source dict

Payload dict element for the data source.

required

Returns:

Type Description
Data | None

Data | None: Data class that includes a Pandas data frame. None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_excel")
def process_bulk_datasource_excel(self, data_source: dict) -> Data | None:
    """Load data from Excel files into the data frame of the Data class.

    See helper/data.py

    Args:
        data_source (dict):
            Payload dict element for the data source.

    Returns:
        Data | None:
            Data class that includes a Pandas data frame. None in case of an error.

    """

    # 1. Read and validate values from the data source payload:
    xlsx_files = data_source.get("xlsx_files", [])
    if not xlsx_files:
        self.logger.error(
            "Excel files not specified in payload of bulk data source. Cannot load data!",
        )
        return None
    xlsx_sheets = data_source.get("xlsx_sheets", 0)  # use 0 not None!
    xlsx_columns = data_source.get("xlsx_columns")
    xlsx_skip_rows = data_source.get("xlsx_skip_rows", 0)
    xlsx_na_values = data_source.get("xlsx_na_values")

    # 2. Initialize Data object:
    data = Data(logger=self.logger)

    # 3. Iterate of Excel files and load them into the Data object:
    for xlsx_file in xlsx_files:
        if not data.load_excel_data(
            xlsx_path=xlsx_file,
            sheet_names=xlsx_sheets,
            usecols=xlsx_columns,
            skip_rows=xlsx_skip_rows,
            na_values=xlsx_na_values,
        ):
            self.logger.error("Failed to load Excel file -> '%s'!", xlsx_file)
            return None

    return data

process_bulk_datasource_filesystem(data_source)

Load data from filesystem into the data frame of the Data class.

See helper/data.py

Parameters:

Name Type Description Default
data_source dict

Payload dict element for the data source.

required

Returns:

Type Description
Data | None

Data | None: Data class that includes a Pandas data frame. None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_filesystem")
def process_bulk_datasource_filesystem(self, data_source: dict) -> Data | None:
    """Load data from filesystem into the data frame of the Data class.

    See helper/data.py

    Args:
        data_source (dict):
            Payload dict element for the data source.

    Returns:
        Data | None:
            Data class that includes a Pandas data frame. None in case of an error.

    """

    # 1. Read and validate values from the data source payload:
    root_folders = data_source.get("root_folders", [])
    if not root_folders:
        self.logger.error(
            "Root folders not specified in payload of bulk data source. Cannot load data from filesystem!",
        )
        return None

    if not isinstance(root_folders, list):
        self.logger.warning(
            "The payload for root folders of the 'filesytem' data source should be a list! You should adjust the 'root_folders' setting.",
        )
        root_folders: list = [root_folders]

    # 2. Initialize Data object:
    data = Data(logger=self.logger)

    # 3. Iterate root folders and load data into Data object:
    for folder in root_folders:
        if not data.load_directory(path_to_root=folder):
            self.logger.error(
                "Failed to load data from root folder -> '%s'!",
                folder,
            )
            return None

    return data

process_bulk_datasource_json(data_source)

Load data from JSON files into the data frame of the Data class.

See helper/data.py

Parameters:

Name Type Description Default
data_source dict

Payload dict element for the data source.

required

Returns:

Type Description
Data | None

Data | None: Data class that includes a Pandas data frame. None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_json")
def process_bulk_datasource_json(self, data_source: dict) -> Data | None:
    """Load data from JSON files into the data frame of the Data class.

    See helper/data.py

    Args:
        data_source (dict):
            Payload dict element for the data source.

    Returns:
        Data | None:
            Data class that includes a Pandas data frame. None in case of an error.

    """

    # 1. Read and validate values from the data source payload:
    json_files = data_source.get("json_files", [])
    if not json_files:
        self.logger.error(
            "JSON files not specified in payload of bulk data source. Cannot load data!",
        )
        return None

    # 2. Initialize Data object:
    data = Data(logger=self.logger)

    # 3. Iterate JSON files and load data into Data object:
    for json_file in json_files:
        json_file = self.prepare_datasource_file(
            data_source=data_source,
            filename=json_file,
        )

        if not data.load_json_data(json_path=json_file):
            self.logger.error(
                "Invalid JSON file -> '%s'. Cannot load it!",
                json_file,
            )
            return None

    return data

process_bulk_datasource_nhc(data_source)

Load data from National Hurricane Center data source into the data frame of the Data class.

See helper/data.py

Parameters:

Name Type Description Default
data_source dict

Payload dict element for the data source

required

Returns:

Type Description
Data | None

Data | None: Data class that includes a Pandas data frame. None in cause of an error.

Side Effects

self._nhc is set to the NHC object created by this method.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_nhc")
def process_bulk_datasource_nhc(self, data_source: dict) -> Data | None:
    """Load data from National Hurricane Center data source into the data frame of the Data class.

    See helper/data.py

    Args:
        data_source (dict):
            Payload dict element for the data source

    Returns:
        Data | None:
            Data class that includes a Pandas data frame. None in cause of an error.

    Side Effects:
        self._nhc is set to the NHC object created by this method.

    """
    try:
        from pyxecm_internal.nhc import (
            NHC,
        )
    except ModuleNotFoundError:
        self.logger.info(
            "Python module 'pyxecm_internal' not installed. Cannot process NHC data source!",
        )
        return None

    # 1. Read and validate values from the data source payload:
    nhc_year_start = data_source.get("nhc_year_start", "2023")
    nhc_year_end = data_source.get("nhc_year_end", "2024")
    nhc_basin = data_source.get("nhc_basin", "north_atlantic")
    nhc_track_images = data_source.get("nhc_track_images", ["png"])
    nhc_track_data = data_source.get("nhc_track_data", [])
    nhc_rain_images = data_source.get("nhc_rain_images", ["png"])
    nhc_rain_data = data_source.get("nhc_rain_data", [])
    nhc_skip_existing_files = data_source.get("nhc_skip_existing_files", True)
    nhc_async = data_source.get("nhc_async", True)
    nhc_async_processes = data_source.get("nhc_async_processes", 5)
    nhc_async_processes = data_source.get("nhc_async_processes", 5)
    nhc_storm_plot_exclusions = data_source.get("nhc_storm_plot_exclusions", [])
    nhc_download_dir = data_source.get("nhc_download_dir", "/data/nhc")
    # We either get the download dir for images from the payload
    # directly or we construct it from the general download dir:
    nhc_download_dir_images = data_source.get(
        "nhc_download_dir_images",
        os.path.join(nhc_download_dir, "images"),
    )  # don't use "/images"
    # We either get the download dir for data files (JSON) from the
    # payload directly or we construct it from the general download dir:
    nhc_download_dir_data = data_source.get(
        "nhc_download_dir_images",
        os.path.join(nhc_download_dir, "json"),
    )  # don't use "/data"

    # 2. Creating the NHC object:
    self._nhc = NHC(
        basin=nhc_basin,
        storm_plot_exclusions=nhc_storm_plot_exclusions,
        download_dir_images=nhc_download_dir_images,
        download_dir_data=nhc_download_dir_data,
        logger=self.logger,
    )
    if not self._nhc:
        self.logger.error("Failed to initialize NHC object!")
        return None

    # 3. Load the NHC storms into the Data object (Pandas DataFrame):
    if not self._nhc.load_storms(
        year_start=nhc_year_start,
        year_end=nhc_year_end,
        save_track_images=nhc_track_images,
        save_track_data=nhc_track_data,
        save_rain_images=nhc_rain_images,
        save_rain_data=nhc_rain_data,
        skip_existing_files=nhc_skip_existing_files,
        load_async=nhc_async,
        async_processes=nhc_async_processes,
    ):
        self.logger.error("Failure during load of NHC storms!")
        return None

    data = self._nhc.get_data()
    if data is None:
        self.logger.error("Failure during load of NHC storm data!")
        return None

    return data

process_bulk_datasource_otcs(data_source)

Load data from Content Server (OTCS) data source into the data frame.

See helper/data.py

Parameters:

Name Type Description Default
data_source dict

Payload dict element for the data source.

required

Returns:

Name Type Description
Data Data

Data class that includes a Pandas DataFrame

Side Effects

self._otcs_source is set to the OTCS object created by this method.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_otcs")
def process_bulk_datasource_otcs(self, data_source: dict) -> Data:
    """Load data from Content Server (OTCS) data source into the data frame.

    See helper/data.py

    Args:
        data_source (dict):
            Payload dict element for the data source.

    Returns:
        Data:
            Data class that includes a Pandas DataFrame

    Side Effects:
        self._otcs_source is set to the OTCS object created by this method.

    """

    # 1. Read and validate values from the data source payload:
    otcs_hostname = data_source.get("otcs_hostname", "")
    if not otcs_hostname:
        self.logger.error(
            "Content Server hostname (otcs_hostname) is not specified in payload of bulk data source. Cannot load data!",
        )
        return None
    otcs_protocol = data_source.get("otcs_protocol", "https")
    otcs_port = data_source.get("otcs_port", "443")
    otcs_basepath = data_source.get("otcs_basepath", "/cs/cs")
    otcs_username = data_source.get("otcs_username", "")
    otcs_password = data_source.get("otcs_password", "")
    if not otcs_username or not otcs_password:
        self.logger.error(
            "Content Server user name (otcs_username) or password (otcs_password) are missing in payload of bulk data source. Cannot load data!",
        )
        return None
    otcs_thread_number = data_source.get("otcs_thread_number", BULK_THREAD_NUMBER)
    otcs_download_dir = data_source.get("otcs_download_dir", "/data/contentserver")
    # Root nodes for the traversal of the data loader (can be a list or single ID)
    # we keep two spellings to remain backwards compatible with the payload syntax:
    otcs_root_node_ids = data_source.get(
        "otcs_root_node_ids",
        data_source.get("otcs_root_node_id"),
    )

    if not otcs_root_node_ids:
        self.logger.error(
            "Content Server root node ID(s) for traversal are missing in payload of bulk data source. Cannot load data!",
        )
        return None

    otcs_include_workspaces = data_source.get("otcs_include_workspaces", True)
    otcs_include_items = data_source.get("otcs_include_items", True)
    otcs_include_workspace_metadata = data_source.get("otcs_include_workspace_metadata", True)
    otcs_include_item_metadata = data_source.get("otcs_include_item_metadata", True)

    # Filter workspace by depth under the given root (only consider items as workspace if they have the right depth in the hierarchy):
    otcs_filter_workspace_depth = data_source.get("otcs_filter_workspace_depth", 0)
    # Filter workspace by subtype (only consider items as workspace if they have the right technical subtype):
    # This is NOT the workspace type but the technical subtype (like 848 for workspaces and 0 for folder)
    otcs_filter_workspace_subtypes = data_source.get(
        "otcs_filter_workspace_subtypes",
    )
    # Filter workspace by category name (only consider items as workspace if they have the category):
    otcs_filter_workspace_category = data_source.get(
        "otcs_filter_workspace_category",
    )
    # Filter workspace by attribute values (only consider items as workspace if they have the attributes with the defined values):
    otcs_filter_workspace_attributes = data_source.get(
        "otcs_filter_workspace_attributes",
    )

    # Filter item by depth under the given root:
    otcs_filter_item_depth = data_source.get("otcs_filter_item_depth")
    # Filter item by subtype (only consider items if they have the right technical subtype):
    # This is the technical subtype (like 0 for folder and 144 for documents)
    otcs_filter_item_subtypes = data_source.get(
        "otcs_filter_item_subtypes",
    )
    # Filter item by category name (only consider items as workspace if they have the category):
    otcs_filter_item_category = data_source.get("otcs_filter_item_category")
    # Filter item by attribute values (only consider items if they have the attributes with the defined values):
    otcs_filter_item_attributes = data_source.get("otcs_filter_item_attributes")
    # Filter item also if the are in workspaces (default is True):
    otcs_filter_item_in_workspace = data_source.get(
        "otcs_filter_item_in_workspace",
        True,
    )
    # List of node IDs to exclude in traversing the folders in the OTCS data source:
    otcs_exclude_node_ids = data_source.get("otcs_exclude_node_ids")

    # Document handling parameters:
    otcs_download_documents = data_source.get("otcs_download_documents", True)
    otcs_skip_existing_downloads = data_source.get("otcs_skip_existing_downloads", True)
    otcs_extract_zip = data_source.get("extract_zip", False)
    # The following parameter controls how column names are constructed. If it is true, then
    # attribute columns for workspaces and items will use the category ID in the column name.
    # Wokspace attributes always start with "workspace_cat_". Item attributes start with item_cat_".
    # If the value of 'otcs_use_numeric_category_identifier' is False then the category name
    # is converted to lower-case and spaces and non-alphanumeric characters are replaced with "_".
    # Example with otcs_use_numeric_category_identifier = True: workspace_cat_47110815_10
    # Example with otcs_use_numeric_category_identifier = False: workspace_cat_customer_use_case_10
    otcs_use_numeric_category_identifier = data_source.get("otcs_use_numeric_category_identifier", True)

    # Ensure Root_node_id is a list of integers
    if not isinstance(otcs_root_node_ids, list):
        otcs_root_node_ids = [otcs_root_node_ids]
    otcs_root_node_ids = [int(item) for item in otcs_root_node_ids]

    # 2. Creating the OTCS object:
    self._otcs_source = OTCS(
        protocol=otcs_protocol,
        hostname=otcs_hostname,
        port=otcs_port,
        base_path=otcs_basepath,
        username=otcs_username,
        password=otcs_password,
        thread_number=otcs_thread_number,
        download_dir=otcs_download_dir,
        use_numeric_category_identifier=otcs_use_numeric_category_identifier,
        logger=self.logger,
    )

    # 3. Authenticate at OTCS
    auth_data = self._otcs_source.authenticate()
    if not auth_data:
        self.logger.error(
            "Failed to authenticate at Content Server -> %s!",
            otcs_protocol + "://" + otcs_hostname + otcs_basepath,
        )
        return None
    else:
        self.logger.info(
            "Successfully authenticated at Content Server -> %s.",
            otcs_protocol + "://" + otcs_hostname + otcs_basepath,
        )

    # 4. Load the Content Server data into the Data object (Pandas DataFrame):

    self.logger.info(
        "Loading data (folder, workspaces, items) from Content Server -> %s using root IDs -> %s...",
        otcs_protocol + "://" + otcs_hostname + otcs_basepath,
        otcs_root_node_ids,
    )

    for otcs_root_node_id in otcs_root_node_ids:
        if not self._otcs_source.load_items(
            node_id=otcs_root_node_id,
            workspaces=otcs_include_workspaces,
            items=otcs_include_items,
            filter_workspace_depth=otcs_filter_workspace_depth,
            filter_workspace_subtypes=otcs_filter_workspace_subtypes,
            filter_workspace_category=otcs_filter_workspace_category,
            filter_workspace_attributes=otcs_filter_workspace_attributes,
            filter_item_depth=otcs_filter_item_depth,
            filter_item_subtypes=otcs_filter_item_subtypes,
            filter_item_category=otcs_filter_item_category,
            filter_item_attributes=otcs_filter_item_attributes,
            filter_item_in_workspace=otcs_filter_item_in_workspace,
            exclude_node_ids=otcs_exclude_node_ids,
            workspace_metadata=otcs_include_workspace_metadata,
            item_metadata=otcs_include_item_metadata,
            download_documents=otcs_download_documents,
            skip_existing_downloads=otcs_skip_existing_downloads,
            extract_zip=otcs_extract_zip,
            workers=otcs_thread_number,
        ):
            self.logger.error(
                "Failure during load of Content Server items from root node ID -> %s!",
                str(otcs_root_node_id),
            )
            return None

    data = self._otcs_source.get_data()
    if data is None:
        self.logger.error(
            "Failure during load of Content Server items! No data loaded!",
        )
        return None

    return data

process_bulk_datasource_otmm(data_source)

Load data from OTMM data source into the data frame of the Data class.

See helper/data.py

Parameters:

Name Type Description Default
data_source dict

Payload dict element for the data source.

required

Returns:

Type Description
Data | None

Data | None: Data class that includes a Pandas data frame. None in case of an error.

Side Effects

self._otmm is set to the OTMM object created by this method

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_otmm")
def process_bulk_datasource_otmm(self, data_source: dict) -> Data | None:
    """Load data from OTMM data source into the data frame of the Data class.

    See helper/data.py

    Args:
        data_source (dict):
            Payload dict element for the data source.

    Returns:
        Data | None:
            Data class that includes a Pandas data frame. None in case of an error.

    Side Effects:
        self._otmm is set to the OTMM object created by this method

    """

    # 1. Read and validate values from the data source payload:
    otmm_base_url = data_source.get("otmm_base_url", "")
    if not otmm_base_url:
        self.logger.error(
            "OTMM base URL (otmm_base_url) is not specified in payload of bulk data source. Cannot load data!",
        )
        return None
    otmm_username = data_source.get("otmm_username", "")
    otmm_password = data_source.get("otmm_password", "")
    otmm_client_id = data_source.get("otmm_client_id")
    otmm_client_secret = data_source.get("otmm_client_secret")
    otmm_thread_number = data_source.get("otmm_thread_number", BULK_THREAD_NUMBER)
    otmm_download_dir = data_source.get("otmm_download_dir", "/data/mediaassets")
    otmm_business_unit_exclusions = data_source.get(
        "otmm_business_unit_exclusions",
        [],
    )
    otmm_business_unit_inclusions = data_source.get("otmm_business_unit_inclusions")
    otmm_product_exclusions = data_source.get("otmm_product_exclusions", [])
    otmm_product_inclusions = data_source.get("otmm_product_inclusions")
    otmm_asset_exclusions = data_source.get("otmm_asset_exclusions", [])
    otmm_asset_inclusions = data_source.get("otmm_asset_inclusions", [])

    self.logger.info(
        "Loading data from OpenText Media Management -> %s (Marketing Assets)...",
        otmm_base_url,
    )

    # 2. Creating the OTMM object:
    self._otmm = OTMM(
        base_url=otmm_base_url,
        client_id=otmm_client_id,
        client_secret=otmm_client_secret,
        username=otmm_username,
        password=otmm_password,
        thread_number=otmm_thread_number,
        download_dir=otmm_download_dir,
        business_unit_exclusions=otmm_business_unit_exclusions,
        business_unit_inclusions=otmm_business_unit_inclusions,
        product_exclusions=otmm_product_exclusions,
        product_inclusions=otmm_product_inclusions,
        asset_exclusions=otmm_asset_exclusions,
        asset_inclusions=otmm_asset_inclusions,
        logger=self.logger,
    )
    if not self._otmm:
        self.logger.error("Failed to initialize OTMM object!")
        return None

    # 3. Authenticate at OTMM
    token = self._otmm.authenticate()
    if not token:
        self.logger.error(
            "Failed to authenticate at OpenText Media Management -> %s!",
            otmm_base_url,
        )
        return None
    else:
        self.logger.info(
            "Successfully authenticated at OpenText Media Management -> %s.",
            otmm_base_url,
        )

    # 4. Load the OTMM assets into the Data object (Pandas DataFrame):
    if not self._otmm.load_assets():
        self.logger.error(
            "Failure during load of OpenText Media Management assets!",
        )
        return None

    data = self._otmm.get_data()
    if data is None:
        self.logger.error(
            "Failure during load of OpenText Media Management assets! No assets loaded!",
        )
        return None

    return data

process_bulk_datasource_pht(data_source)

Load data from OpenText PHT data source into the data frame of the Data class.

See helper/data.py

Parameters:

Name Type Description Default
data_source dict

Payload dict element for the data source.

required

Returns:

Type Description
Data | None

Data | None: Data class that includes a Pandas data frame. None in case of an error.

Side Effects

self._pht is set to the PHT object created by this method.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_pht")
def process_bulk_datasource_pht(self, data_source: dict) -> Data | None:
    """Load data from OpenText PHT data source into the data frame of the Data class.

    See helper/data.py

    Args:
        data_source (dict):
            Payload dict element for the data source.

    Returns:
        Data | None:
            Data class that includes a Pandas data frame. None in case of an error.

    Side Effects:
        self._pht is set to the PHT object created by this method.

    """

    try:
        from pyxecm_internal.pht import PHT
    except ModuleNotFoundError:
        self.logger.info(
            "Python module 'pyxecm_internal' not installed. Cannot process PHT data source!",
        )
        return None

    # 1. Read and validate values from the data source payload:
    pht_base_url = data_source.get("pht_base_url", "")
    if not pht_base_url:
        self.logger.error(
            "PHT base URL (pht_base_url) is not specified in payload of bulk data source. Cannot load data!",
        )
        return None
    pht_username = data_source.get("pht_username", "")
    if not pht_username:
        self.logger.error(
            "PHT username (pht_username) is not specified in payload of bulk data source. Cannot load data!",
        )
        return None
    pht_password = data_source.get("pht_password", "")
    if not pht_password:
        self.logger.error(
            "PHT password (pht_password) is not specified in payload of bulk data source. Cannot load data!",
        )
        return None

    pht_business_unit_exclusions = data_source.get(
        "pht_business_unit_exclusions",
        [],
    )
    pht_business_unit_inclusions = data_source.get(
        "pht_business_unit_inclusions",
        [],
    )
    pht_product_exclusions = data_source.get("pht_product_exclusions", [])
    pht_product_inclusions = data_source.get("pht_product_inclusions", [])
    pht_product_category_exclusions = data_source.get(
        "pht_product_category_exclusions",
        [],
    )
    pht_product_category_inclusions = data_source.get(
        "pht_product_category_inclusions",
        [],
    )
    pht_product_status_exclusions = data_source.get(
        "pht_product_status_exclusions",
        [],
    )
    pht_product_status_inclusions = data_source.get(
        "pht_product_status_inclusions",
        [],
    )
    pht_product_attributes = data_source.get(
        "pht_product_attributes",
        [],
    )

    self.logger.info(
        "Loading data from OpenText PHT -> %s (Product Hierarchy)...",
        pht_base_url,
    )

    # 2. Creating the PHT object:
    self._pht = PHT(
        base_url=pht_base_url,
        username=pht_username,
        password=pht_password,
        business_unit_exclusions=pht_business_unit_exclusions,
        business_unit_inclusions=pht_business_unit_inclusions,
        product_exclusions=pht_product_exclusions,
        product_inclusions=pht_product_inclusions,
        product_category_exclusions=pht_product_category_exclusions,
        product_category_inclusions=pht_product_category_inclusions,
        product_status_exclusions=pht_product_status_exclusions,
        product_status_inclusions=pht_product_status_inclusions,
        logger=self.logger,
    )
    if not self._pht:
        self.logger.error("Failed to initialize PHT object!")
        return None

    # 3. Authenticate at PHT
    token = self._pht.authenticate()
    if not token:
        self.logger.error(
            "Failed to authenticate at OpenText PHT -> %s!",
            pht_base_url,
        )
        return None
    else:
        self.logger.info(
            "Successfully authenticated at OpenText PHT -> %s.",
            pht_base_url,
        )

    # 4. Load the PHT product information into the Data object (Pandas DataFrame):
    if not self._pht.load_products(attributes_to_extract=pht_product_attributes):
        self.logger.error("Failure during load of OpenText PHT products!")
        return None

    data = self._pht.get_data()
    if data is None:
        self.logger.error("Failure during load of OpenText PHT product data!")
        return None

    return data

process_bulk_datasource_servicenow(data_source)

Load data from ServiceNow data source into the data frame of the Data class.

See helper/data.py

Parameters:

Name Type Description Default
data_source dict

Payload dict element for the data source.

required

Returns:

Type Description
Data | None

Data | None: Data class that includes a Pandas data frame. None in case of an error.

Side Effects

self._servicenow is set to the ServiceNow object created by this method

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_servicenow")
def process_bulk_datasource_servicenow(self, data_source: dict) -> Data | None:
    """Load data from ServiceNow data source into the data frame of the Data class.

    See helper/data.py

    Args:
        data_source (dict):
            Payload dict element for the data source.

    Returns:
        Data | None:
            Data class that includes a Pandas data frame. None in case of an error.

    Side Effects:
        self._servicenow is set to the ServiceNow object created by this method

    """

    # 1. Read and validate values from the data source payload:
    sn_base_url = data_source.get("sn_base_url", "")
    if not sn_base_url:
        self.logger.error(
            "ServiceNow base URL (sn_base_url) is not specified in payload of bulk data source. Cannot load data!",
        )
        return None
    sn_auth_type = data_source.get("sn_auth_type", "basic")
    sn_username = data_source.get("sn_username", "")
    sn_password = data_source.get("sn_password", "")
    sn_client_id = data_source.get("sn_client_id")
    sn_client_secret = data_source.get("sn_client_secret")
    sn_table_name = data_source.get(
        "sn_table_name",
        "u_kb_template_technical_article_public",
    )
    sn_queries = data_source.get("sn_queries", [])
    sn_query = data_source.get("sn_query")
    if sn_query is not None:
        sn_queries.append({"table": sn_table_name, "query": sn_query})

    sn_thread_number = data_source.get("sn_thread_number", BULK_THREAD_NUMBER)
    sn_download_dir = data_source.get("sn_download_dir", "/data/knowledgebase")
    sn_skip_existing_downloads = data_source.get("sn_skip_existing_downloads", True)
    sn_product_exclusions = data_source.get("sn_product_exclusions", [])

    if sn_product_exclusions:
        self.logger.info(
            "Excluding products -> %s from ServiceNow data loading.",
            sn_product_exclusions,
        )

    if sn_base_url and (sn_auth_type == "basic") and (not sn_username or not sn_password):
        self.logger.error(
            "ServiceNow basic authentication needs username and password in payload!",
        )
        return None
    if sn_base_url and (sn_auth_type == "oauth") and (not sn_client_id or not sn_client_secret):
        self.logger.error(
            "ServiceNow OAuth authentication needs client ID and client secret in payload!",
        )
        return None

    # 2. Creating the ServiceNow object:
    self._servicenow = ServiceNow(
        base_url=sn_base_url,
        auth_type=sn_auth_type,
        client_id=sn_client_id,
        client_secret=sn_client_secret,
        username=sn_username,
        password=sn_password,
        thread_number=sn_thread_number,
        download_dir=sn_download_dir,
        product_exclusions=sn_product_exclusions,
        logger=self.logger,
    )

    # 3. Authenticate at ServiceNow
    auth_data = self._servicenow.authenticate(auth_type=sn_auth_type)
    if not auth_data:
        self.logger.error("Failed to authenticate at ServiceNow -> %s!", sn_base_url)
        return None
    else:
        self.logger.info(
            "Successfully authenticated at ServiceNow -> %s.",
            sn_base_url,
        )

    # 4. Load the ServiceNow data into the Data object (Pandas DataFrame):
    for item in sn_queries:
        sn_table_name = item["sn_table_name"]
        sn_query = item["sn_query"]

        self.logger.info(
            "Loading data from ServiceNow table -> '%s' with query -> '%s'...",
            sn_table_name,
            sn_query,
        )

        if not self._servicenow.load_articles(
            table_name=sn_table_name,
            query=sn_query,
            skip_existing_downloads=sn_skip_existing_downloads,
        ):
            self.logger.error(
                "Failure during load of ServiceNow articles from table -> '%s' using query -> '%s' !",
                sn_table_name,
                sn_query,
            )
            continue

    data = self._servicenow.get_data()
    if data is None:
        self.logger.error(
            "Failure during load of ServiceNow articles! No articles loaded!",
        )
        return None

    return data

process_bulk_datasource_web(data_source)

Load data from Web page into the data frame of the Data class.

See helper/data.py

Parameters:

Name Type Description Default
data_source dict

Payload dict element for the data source

required

Returns:

Type Description
Data | None

Data | None: Data class that includes a Pandas DataFrame. None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_web")
def process_bulk_datasource_web(self, data_source: dict) -> Data | None:
    """Load data from Web page into the data frame of the Data class.

    See helper/data.py

    Args:
        data_source (dict):
            Payload dict element for the data source

    Returns:
        Data | None:
            Data class that includes a Pandas DataFrame. None in case of an error.

    """

    # 1. Read and validate values from the data source payload:
    web_url_templates = data_source.get("web_url_templates")
    if not web_url_templates:
        self.logger.error(
            "Web URLs (templates) not specified in payload of bulk data source. Cannot load data!",
        )
        return None
    web_start_value = data_source.get("web_start_value")
    web_end_value = data_source.get("web_end_value")
    web_value_name = data_source.get(
        "web_value_name",
    )  # which column name to use for the value
    web_special_url_templates = data_source.get("web_special_url_templates", [])
    # Pattern to filter file links on the page
    web_file_url_pattern = data_source.get("web_file_url_pattern")

    web_values = list(range(web_start_value, web_end_value + 1)) if web_start_value and web_end_value else None
    web_special_values = data_source.get("web_special_values", [])

    # 2. Initialize Data object:
    data = Data(logger=self.logger)

    # 3. Iterate over Web pages and load data into Data object:
    if not data.load_web(
        values=web_values,
        value_name=web_value_name,
        url_templates=web_url_templates,
        special_values=web_special_values,
        special_url_templates=web_special_url_templates,
        pattern=web_file_url_pattern,
    ):
        self.logger.error(
            "Failed to load Web data from URLs -> %s!",
            str(web_url_templates),
        )
        return None

    return data

process_bulk_datasource_xml(data_source)

Load data from XML files or directories or zip files into the data frame of the Data class.

See helper/data.py

Parameters:

Name Type Description Default
data_source dict

Payload dict element for the data source.

required

Returns:

Type Description
Data | None

Data | None: Data class that includes a Pandas data frame. None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_datasource_xml")
def process_bulk_datasource_xml(self, data_source: dict) -> Data | None:
    """Load data from XML files or directories or zip files into the data frame of the Data class.

    See helper/data.py

    Args:
        data_source (dict):
            Payload dict element for the data source.

    Returns:
        Data | None:
            Data class that includes a Pandas data frame. None in case of an error.

    """

    # 1. Read and validate values from the data source payload:
    xml_files = data_source.get("xml_files", [])
    xml_directories = data_source.get(
        "xml_directories",
        [],
    )  # can also be zip files
    xml_xpath = data_source.get("xml_xpath")

    # 2. Initialize Data object:
    data = Data(logger=self.logger)

    # 3. If no XML directories are specified we interpret the "xml_files"
    if not xml_directories:
        for xml_file in xml_files:
            self.logger.info("Loading XML file -> '%s'...", xml_file)
            if not data.load_xml_data(xml_path=xml_file, xpath=xml_xpath):
                self.logger.error("Failed to load XML file -> '%s'!", xml_file)
                return None

    # 4. If XML directories or zip files of XML files are given we traverse them instead:
    else:
        # we now produce a list of dictionaries:
        xml_data = XML.load_xml_files_from_directories(
            directories=xml_directories,
            filenames=xml_files,
            xpath=xml_xpath,
            logger=self.logger.getChild("xml"),
        )
        if xml_data is None:  # test on error
            self.logger.error(
                "Failed to load XML files from directories or ZIP files -> %s!",
                xml_directories,
            )
            return None
        if not xml_data:  # test on empty result
            self.logger.warning(
                "Found no XML files in directory or ZIP files -> %s!",
                xml_directories,
            )
        else:
            data.append(add_data=xml_data)

    return data

process_bulk_documents(section_name='bulkDocuments')

Process bulkDocuments in payload and bulk create them in Content Server (multi-threaded).

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'bulkDocuments'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
25061
25062
25063
25064
25065
25066
25067
25068
25069
25070
25071
25072
25073
25074
25075
25076
25077
25078
25079
25080
25081
25082
25083
25084
25085
25086
25087
25088
25089
25090
25091
25092
25093
25094
25095
25096
25097
25098
25099
25100
25101
25102
25103
25104
25105
25106
25107
25108
25109
25110
25111
25112
25113
25114
25115
25116
25117
25118
25119
25120
25121
25122
25123
25124
25125
25126
25127
25128
25129
25130
25131
25132
25133
25134
25135
25136
25137
25138
25139
25140
25141
25142
25143
25144
25145
25146
25147
25148
25149
25150
25151
25152
25153
25154
25155
25156
25157
25158
25159
25160
25161
25162
25163
25164
25165
25166
25167
25168
25169
25170
25171
25172
25173
25174
25175
25176
25177
25178
25179
25180
25181
25182
25183
25184
25185
25186
25187
25188
25189
25190
25191
25192
25193
25194
25195
25196
25197
25198
25199
25200
25201
25202
25203
25204
25205
25206
25207
25208
25209
25210
25211
25212
25213
25214
25215
25216
25217
25218
25219
25220
25221
25222
25223
25224
25225
25226
25227
25228
25229
25230
25231
25232
25233
25234
25235
25236
25237
25238
25239
25240
25241
25242
25243
25244
25245
25246
25247
25248
25249
25250
25251
25252
25253
25254
25255
25256
25257
25258
25259
25260
25261
25262
25263
25264
25265
25266
25267
25268
25269
25270
25271
25272
25273
25274
25275
25276
25277
25278
25279
25280
25281
25282
25283
25284
25285
25286
25287
25288
25289
25290
25291
25292
25293
25294
25295
25296
25297
25298
25299
25300
25301
25302
25303
25304
25305
25306
25307
25308
25309
25310
25311
25312
25313
25314
25315
25316
25317
25318
25319
25320
25321
25322
25323
25324
25325
25326
25327
25328
25329
25330
25331
25332
25333
25334
25335
25336
25337
25338
25339
25340
25341
25342
25343
25344
25345
25346
25347
25348
25349
25350
25351
25352
25353
25354
25355
25356
25357
25358
25359
25360
25361
25362
25363
25364
25365
25366
25367
25368
25369
25370
25371
25372
25373
25374
25375
25376
25377
25378
25379
25380
25381
25382
25383
25384
25385
25386
25387
25388
25389
25390
25391
25392
25393
25394
25395
25396
25397
25398
25399
25400
25401
25402
25403
25404
25405
25406
25407
25408
25409
25410
25411
25412
25413
25414
25415
25416
25417
25418
25419
25420
25421
25422
25423
25424
25425
25426
25427
25428
25429
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_documents")
def process_bulk_documents(self, section_name: str = "bulkDocuments") -> bool:
    """Process bulkDocuments in payload and bulk create them in Content Server (multi-threaded).

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not self._bulk_documents:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    # For efficient idem-potent operation we may want to see which workspaces
    # have already been processed successfully before:
    if self.check_status_file(
        payload_section_name=section_name,
        payload_specific=True,
        prefix="failure_",
    ):
        self.logger.info(
            "Found failure file. Trying to reprocess just the failed ones...",
        )
        # Read payload from where we left it last time
        self._bulk_documents = self.get_status_file(
            payload_section_name=section_name,
            prefix="failure_",
        )
        if not self._bulk_documents:
            self.logger.error(
                "Cannot load existing bulkDocuments failure file. Bailing out!",
            )
            return False

    success: bool = True

    for bulk_document in self._bulk_documents:
        # Check if element has been disabled in payload (enabled = false).
        # In this case we skip the element:
        enabled = bulk_document.get("enabled", True)
        if not enabled:
            self.logger.info("Payload for Bulk Document is disabled. Skipping...")
            continue

        # The payload element must have a "data_source" key:
        data_source_name = bulk_document.get("data_source", None)
        if not data_source_name:
            self.logger.error("Missing data source name in Bulk Document!")
            success = False
            continue

        copy_data_source = bulk_document.get("copy_data_source", False)
        force_reload = bulk_document.get("force_reload", True)

        # Load and prepare the data source for the bulk processing:
        if copy_data_source:
            self.logger.info(
                "Take a copy of data source -> %s to avoid side-effects for repeative usage of the data source...",
                data_source_name,
            )
            data = Data(
                self.process_bulk_datasource(
                    data_source_name=data_source_name,
                    force_reload=force_reload,
                ),
                logger=self.logger,
            )
        else:
            self.logger.info(
                "Use original data source -> '%s' and not do a copy.",
                bulk_document["data_source"],
            )
            data = self.process_bulk_datasource(
                data_source_name=data_source_name,
                force_reload=force_reload,
            )
        if (
            data is None
            or data.get_data_frame() is None  # the 2nd check is important for the "copy_data_source" case!
        ):  # important to use "is None" here!
            self.logger.error(
                "Failed to load data source -> '%s' for bulk documents!",
                data_source_name,
            )
            success = False
            continue

        # Check if fields with list substructures should be exploded.
        # We may want to do this outside the bulkDatasource to only
        # explode for bulkDocuments and not for bulkWorkspaces or
        # bulkWorkspaceRelationships:
        explosions = bulk_document.get("explosions", [])
        for explosion in explosions:
            # explode field can be a string or a list
            # exploding multiple fields at once avoids
            # combinatorial explosions - this is VERY
            # different from exploding columns one after the other!
            if "explode_field" not in explosion and "explode_fields" not in explosion:
                self.logger.error("Missing explosion field(s)!")
                continue
            # we want to be backwards compatible...
            explode_fields = (
                explosion["explode_field"] if "explode_field" in explosion else explosion["explode_fields"]
            )
            flatten_fields = explosion.get("flatten_fields", [])
            split_string_to_list = explosion.get("split_string_to_list", False)
            list_splitter = explosion.get("list_splitter", None)
            self.logger.info(
                "Starting explosion of bulk documents by field(s) -> %s (type -> %s). Size of data frame before explosion -> %s.",
                explode_fields,
                str(type(explode_fields)),
                str(len(data)),
            )
            data.explode_and_flatten(
                explode_fields=explode_fields,
                flatten_fields=flatten_fields,
                make_unique=False,
                split_string_to_list=split_string_to_list,
                separator=list_splitter,
                reset_index=True,
            )
            self.logger.info(
                "Size of data frame after explosion -> %s.",
                str(len(data)),
            )
        # end for explosion in explosions

        # Keep only selected rows if filters are specified in bulkDocuments.
        # We have this _after_ "explosions" to allow access to subfields as well.
        # We have this _before_ "sorting" and "deduplication" as we may keep the wrong
        # rows otherwise (unique / deduplication always keeps the first matching row).
        # We may want to do this outside the bulkDatasource to only
        # filter for bulkDocuments and not for bulkWorkspaces or
        # bulkWorkspaceRelationships:
        filters = bulk_document.get("filters", [])
        if filters:
            data.filter(conditions=filters)

        # Sort the data frame if "sort" specified in payload. We may want to do this to have a
        # higher chance that rows with workspace names that may collapse into
        # one name are put into the same partition. This can avoid race conditions
        # between different Python threads.
        # We do this before deduplication (unique) below as sorting has an influence
        # on which duplicates are kept.
        sort_fields = bulk_document.get("sort", [])
        if sort_fields:
            self.logger.info(
                "Start sorting of bulk document data frame based on fields (columns) -> %s...",
                str(sort_fields),
            )
            data.sort(sort_fields=sort_fields, inplace=True)
            self.logger.info(
                "Sorting of bulk document data frame based on fields (columns) -> %s completed!",
                str(sort_fields),
            )

        # Check if duplicate rows for given fields should be removed. It is
        # important to do this after sorting as Pandas always keep the first occurance,
        # so ordering plays an important role in deduplication!
        unique_fields = bulk_document.get("unique", [])
        if unique_fields:
            self.logger.info(
                "Starting deduplication of data frame for bulk documents with unique fields -> %s. Size of data frame before deduplication -> %s.",
                str(unique_fields),
                str(len(data)),
            )
            data.deduplicate(unique_fields=unique_fields, inplace=True)
            self.logger.info(
                "Size of data frame after deduplication -> %s.",
                str(len(data)),
            )

        # Read name field from payload:
        name_field = bulk_document.get("name", None)
        if not name_field:
            self.logger.error(
                "Bulk document needs a name field! Skipping to next bulk document...",
            )
            success = False
            continue

        self._log_header_callback(
            text="Process bulk documents -> '{}' from data source -> '{}'".format(
                name_field,
                data_source_name,
            ),
            char="-",
        )

        # Read optional description field from payload:
        description_field = bulk_document.get("description", None)

        # Read the optional categories payload:
        categories = bulk_document.get("categories", None)
        if not categories:
            self.logger.info(
                "Bulk document payload has no category data! Will leave category attributes empty...",
            )

        # Should existing documents be updated? False (= no) is the default.
        operations = bulk_document.get("operations", ["create"])

        self.logger.info(
            "Bulk create documents (name field -> '%s', operations -> %s)...",
            name_field,
            str(operations),
        )

        # See if bulkWorkspace definition has a specific thread number
        # otherwise it is read from a global environment variable
        bulk_thread_number = int(
            bulk_document.get("thread_number", BULK_THREAD_NUMBER),
        )

        partitions = data.partitionate(bulk_thread_number)

        # Create a list to hold the threads
        threads = []
        results = []

        # Define source OTCS object and authenticate once and pass it to all workers if needed
        if bulk_document.get("source_type", "URL").lower() == "contentserver":
            if bulk_document.get("cs_hostname") is None:
                source_otcs = None
                self.logger.error(
                    "Required information for source type ContentServer is not configured -> cs_hostname",
                )
                success = False
                continue

            if bulk_document.get("cs_username") is None:
                source_otcs = None
                self.logger.error(
                    "Required information for source type ContentServer is not configured -> cs_username",
                )
                success = False
                continue

            if bulk_document.get("cs_password") is None:
                source_otcs = None
                self.logger.error(
                    "Required information for source type ContentServer is not configured -> cs_password",
                )
                success = False
                continue

            self.logger.info(
                "Generating reusable OTCS instance for bulk processing...",
            )
            source_otcs = OTCS(
                protocol=bulk_document.get("cs_protocol", "https"),
                hostname=bulk_document.get("cs_hostname"),
                port=bulk_document.get("cs_port", "443"),
                base_path=bulk_document.get("cs_basepath", "/cs/cs"),
                username=bulk_document.get("cs_username"),
                password=bulk_document.get("cs_password"),
                logger=self.logger,
            )
            source_otcs.authenticate()
        # end if bulk_document.get("source_type", "URL").lower() == "contentserver"
        else:
            source_otcs = None

        # Create and start a thread for each partition
        for index, partition in enumerate(partitions, start=1):
            # start a thread executing the process_bulk_documents_worker() method below:
            thread = threading.Thread(
                name=f"{section_name}_{index:02}",
                target=self.thread_wrapper,
                args=(
                    self.process_bulk_documents_worker,
                    bulk_document,
                    partition,
                    name_field,
                    description_field,
                    categories,
                    operations,
                    results,
                    source_otcs,
                ),
            )
            self.logger.info("Starting thread -> '%s'...", str(thread.name))
            thread.start()
            threads.append(thread)
            # Avoid that all threads start at the exact same time with
            # potentially expired cookies that cases race conditions:
            time.sleep(1)
        # end for index, partition in enumerate(partitions, start=1)

        # Wait for all threads to complete
        for thread in threads:
            self.logger.info(
                "Waiting for thread -> '%s' to complete...",
                str(thread.name),
            )
            thread.join()
            self.logger.info("Thread -> '%s' has completed.", str(thread.name))

        if "documents" not in bulk_document:
            bulk_document["documents"] = {}

        # Print statistics for each thread. In addition,
        # check if all threads have completed without error / failure.
        # If there's a single failure in on of the thread results we
        # set 'success' variable to False.
        results.sort(key=lambda x: x["thread_name"])
        for result in results:
            self.logger.info(
                "Thread -> '%s' completed with overall status '%s'.",
                str(result["thread_name"]),
                "successful" if result["success"] else "failed",
            )
            self.logger.info(
                "Thread -> '%s' processed %s data rows with %s successful, %s failed, and %s skipped.",
                str(result["thread_name"]),
                str(
                    result["success_counter"] + result["failure_counter"] + result["skipped_counter"],
                ),
                str(result["success_counter"]),
                str(result["failure_counter"]),
                str(result["skipped_counter"]),
            )
            self.logger.info(
                "Thread -> '%s' created %s documents, updated %s documents, and deleted %s documents.",
                str(result["thread_name"]),
                str(result["create_counter"]),
                str(result["update_counter"]),
                str(result["delete_counter"]),
            )

            if not result["success"]:
                success = False
            # Record all generated documents. This should allow us
            # to restart in case of failures and avoid trying to
            # uploading that have been successfully uploaded before.
            bulk_document["documents"].update(result["documents"])
        # end for result in results
        self._log_header_callback(
            text="Completed processing of bulk documents -> '{}' using data source -> '{}'".format(
                name_field,
                data_source_name,
            ),
            char="-",
        )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._bulk_documents,
    )

    return success

process_bulk_documents_worker(bulk_document, partition, name_field, description_field, categories=None, operations=None, results=None, source_otcs=None)

Thread worker to download + create documents in bulk.

Each worker thread gets a partition of the rows that include the data required for the document creation.

Parameters:

Name Type Description Default
bulk_document dict

The bulkDocument payload element.

required
partition DataFrame

Data partition with rows to process by this thread.

required
name_field str

The payload field where the document name is stored.

required
description_field str

The payload field where the document description is stored.

required
categories list

A list of category dictionaries.

None
operations list

A list of operations that should be applyed on workspaces. Possible values: "create", "update", "delete", "recreate".

None
results list

A mutable list of thread results.

None
source_otcs OTCS | None

Provide the OTCS object if a direct download from Content Server is done.

None
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
26405
26406
26407
26408
26409
26410
26411
26412
26413
26414
26415
26416
26417
26418
26419
26420
26421
26422
26423
26424
26425
26426
26427
26428
26429
26430
26431
26432
26433
26434
26435
26436
26437
26438
26439
26440
26441
26442
26443
26444
26445
26446
26447
26448
26449
26450
26451
26452
26453
26454
26455
26456
26457
26458
26459
26460
26461
26462
26463
26464
26465
26466
26467
26468
26469
26470
26471
26472
26473
26474
26475
26476
26477
26478
26479
26480
26481
26482
26483
26484
26485
26486
26487
26488
26489
26490
26491
26492
26493
26494
26495
26496
26497
26498
26499
26500
26501
26502
26503
26504
26505
26506
26507
26508
26509
26510
26511
26512
26513
26514
26515
26516
26517
26518
26519
26520
26521
26522
26523
26524
26525
26526
26527
26528
26529
26530
26531
26532
26533
26534
26535
26536
26537
26538
26539
26540
26541
26542
26543
26544
26545
26546
26547
26548
26549
26550
26551
26552
26553
26554
26555
26556
26557
26558
26559
26560
26561
26562
26563
26564
26565
26566
26567
26568
26569
26570
26571
26572
26573
26574
26575
26576
26577
26578
26579
26580
26581
26582
26583
26584
26585
26586
26587
26588
26589
26590
26591
26592
26593
26594
26595
26596
26597
26598
26599
26600
26601
26602
26603
26604
26605
26606
26607
26608
26609
26610
26611
26612
26613
26614
26615
26616
26617
26618
26619
26620
26621
26622
26623
26624
26625
26626
26627
26628
26629
26630
26631
26632
26633
26634
26635
26636
26637
26638
26639
26640
26641
26642
26643
26644
26645
26646
26647
26648
26649
26650
26651
26652
26653
26654
26655
26656
26657
26658
26659
26660
26661
26662
26663
26664
26665
26666
26667
26668
26669
26670
26671
26672
26673
26674
26675
26676
26677
26678
26679
26680
26681
26682
26683
26684
26685
26686
26687
26688
26689
26690
26691
26692
26693
26694
26695
26696
26697
26698
26699
26700
26701
26702
26703
26704
26705
26706
26707
26708
26709
26710
26711
26712
26713
26714
26715
26716
26717
26718
26719
26720
26721
26722
26723
26724
26725
26726
26727
26728
26729
26730
26731
26732
26733
26734
26735
26736
26737
26738
26739
26740
26741
26742
26743
26744
26745
26746
26747
26748
26749
26750
26751
26752
26753
26754
26755
26756
26757
26758
26759
26760
26761
26762
26763
26764
26765
26766
26767
26768
26769
26770
26771
26772
26773
26774
26775
26776
26777
26778
26779
26780
26781
26782
26783
26784
26785
26786
26787
26788
26789
26790
26791
26792
26793
26794
26795
26796
26797
26798
26799
26800
26801
26802
26803
26804
26805
26806
26807
26808
26809
26810
26811
26812
26813
26814
26815
26816
26817
26818
26819
26820
26821
26822
26823
26824
26825
26826
26827
26828
26829
26830
26831
26832
26833
26834
26835
26836
26837
26838
26839
26840
26841
26842
26843
26844
26845
26846
26847
26848
26849
26850
26851
26852
26853
26854
26855
26856
26857
26858
26859
26860
26861
26862
26863
26864
26865
26866
26867
26868
26869
26870
26871
26872
26873
26874
26875
26876
26877
26878
26879
26880
26881
26882
26883
26884
26885
26886
26887
26888
26889
26890
26891
26892
26893
26894
26895
26896
26897
26898
26899
26900
26901
26902
26903
26904
26905
26906
26907
26908
26909
26910
26911
26912
26913
26914
26915
26916
26917
26918
26919
26920
26921
26922
26923
26924
26925
26926
26927
26928
26929
26930
26931
26932
26933
26934
26935
26936
26937
26938
26939
26940
26941
26942
26943
26944
26945
26946
26947
26948
26949
26950
26951
26952
26953
26954
26955
26956
26957
26958
26959
26960
26961
26962
26963
26964
26965
26966
26967
26968
26969
26970
26971
26972
26973
26974
26975
26976
26977
26978
26979
26980
26981
26982
26983
26984
26985
26986
26987
26988
26989
26990
26991
26992
26993
26994
26995
26996
26997
26998
26999
27000
27001
27002
27003
27004
27005
27006
27007
27008
27009
27010
27011
27012
27013
27014
27015
27016
27017
27018
27019
27020
27021
27022
27023
27024
27025
27026
27027
27028
27029
27030
27031
27032
27033
27034
27035
27036
27037
27038
27039
27040
27041
27042
27043
27044
27045
27046
27047
27048
27049
27050
27051
27052
27053
27054
27055
27056
27057
27058
27059
27060
27061
27062
27063
27064
27065
27066
27067
27068
27069
27070
27071
27072
27073
27074
27075
27076
27077
27078
27079
27080
27081
27082
27083
27084
27085
27086
27087
27088
27089
27090
27091
27092
27093
27094
27095
27096
27097
27098
27099
27100
27101
27102
27103
27104
27105
27106
27107
27108
27109
27110
27111
27112
27113
27114
27115
27116
27117
27118
27119
27120
27121
27122
27123
27124
27125
27126
27127
27128
27129
27130
27131
27132
27133
27134
27135
27136
27137
27138
27139
27140
27141
27142
27143
27144
27145
27146
27147
27148
27149
27150
27151
27152
27153
27154
27155
27156
27157
27158
27159
27160
27161
27162
27163
27164
27165
27166
27167
27168
27169
27170
27171
27172
27173
27174
27175
27176
27177
27178
27179
27180
27181
27182
27183
27184
27185
27186
27187
27188
27189
27190
27191
27192
27193
27194
27195
27196
27197
27198
27199
27200
27201
27202
27203
27204
27205
27206
27207
27208
27209
27210
27211
27212
27213
27214
27215
27216
27217
27218
27219
27220
27221
27222
27223
27224
27225
27226
27227
27228
27229
27230
27231
27232
27233
27234
27235
27236
27237
27238
27239
27240
27241
27242
27243
27244
27245
27246
27247
27248
27249
27250
27251
27252
27253
27254
27255
27256
27257
27258
27259
27260
27261
27262
27263
27264
27265
27266
27267
27268
27269
27270
27271
27272
27273
27274
27275
27276
27277
27278
27279
27280
27281
27282
27283
27284
27285
27286
27287
27288
27289
27290
27291
27292
27293
27294
27295
27296
27297
27298
27299
27300
27301
27302
27303
27304
27305
27306
27307
27308
27309
27310
27311
27312
27313
27314
27315
27316
27317
27318
27319
27320
27321
27322
27323
27324
27325
27326
27327
27328
27329
27330
27331
27332
27333
27334
27335
27336
27337
27338
27339
27340
27341
27342
27343
27344
27345
27346
27347
27348
27349
27350
27351
27352
27353
27354
27355
27356
27357
27358
27359
27360
27361
27362
27363
27364
27365
27366
27367
27368
27369
27370
27371
27372
27373
27374
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_documents_worker")
def process_bulk_documents_worker(
    self,
    bulk_document: dict,
    partition: pd.DataFrame,
    name_field: str,
    description_field: str,
    categories: list | None = None,
    operations: list | None = None,
    results: list | None = None,
    source_otcs: OTCS | None = None,
) -> None:
    """Thread worker to download + create documents in bulk.

    Each worker thread gets a partition of the rows that include
    the data required for the document creation.

    Args:
        bulk_document (dict):
            The bulkDocument payload element.
        partition (pd.DataFrame):
            Data partition with rows to process by this thread.
        name_field (str):
            The payload field where the document name is stored.
        description_field (str):
            The payload field where the document description is stored.
        categories (list, optional):
            A list of category dictionaries.
        operations (list, optional):
            A list of operations that should be applyed on workspaces.
            Possible values: "create", "update", "delete", "recreate".
        results (list, optional):
            A mutable list of thread results.
        source_otcs (OTCS | None, optional):
            Provide the OTCS object if a direct download from Content Server is done.

    """

    thread_id = threading.get_ident()
    thread_name = threading.current_thread().name

    self.logger.info(
        "Start working on data frame partition of size -> %s to bulk create documents...",
        str(len(partition)),
    )

    # Avoid linter warnings - so make parameter default None while we
    # actually want ["create"] to be the default:
    if operations is None:
        operations = ["create"]

    # get the specific update operations given in the payload
    # if not specified we do the following update operations.
    # The 'purge' operation needs to be specified explicitly.
    update_operations = bulk_document.get(
        "update_operations",
        ["name", "description", "categories", "nickname", "version"],
    )

    result = {}
    result["thread_id"] = thread_id
    result["thread_name"] = thread_name
    result["success_counter"] = 0
    result["failure_counter"] = 0
    result["skipped_counter"] = 0
    result["create_counter"] = 0
    result["update_counter"] = 0
    result["delete_counter"] = 0
    result["documents"] = {}
    result["success"] = True

    # Check if documents have been processed before, e.i. testing
    # if a "documents" key exists and if it is pointing to a non-empty list:
    # Additionally we check that workspace updates are not enforced:
    if (
        bulk_document.get("documents")
        and "update" not in operations
        and "delete" not in operations
        and "recreate" not in operations
    ):
        existing_documents = bulk_document["documents"]
        self.logger.info(
            "Found %s already processed documents. Try to complete the job...",
            str(len(existing_documents)),
        )
    else:
        existing_documents = {}

    # See if external creation and modification dates are in the payload data:
    external_modify_date_field = bulk_document.get("external_modify_date")
    external_create_date_field = bulk_document.get("external_create_date")

    # See if we have a key field to uniquely identify an existing document:
    key_field = bulk_document.get("key")

    # Get dictionary of replacements for bulk document creations
    # this we will be used of all places data is read from the
    # data frame. Each dictionary item has the field name as the
    # dictionary key and a list of regular expressions as dictionary value
    replacements = bulk_document.get("replacements")

    # In case the name cannot be resolved we allow to
    # specify an alternative name field in the payload.
    name_field_alt = bulk_document.get("name_alt")

    # If download_name field is not in payload we use name_field instead.
    # It can still be that download_name is "" as name_field is only
    # used if the entry for "download_name" is not in payload at all.
    download_name_field = bulk_document.get("download_name", name_field)

    # Determine if we want to automatically extract ZIP files and upload
    # them as folders / documents:
    extract_zip = bulk_document.get("extract_zip", False)

    # Document names are limited in terms of allowed characters.
    # E.g. if you don't want any path elements and "/" dividers
    # in the document name you could use name_regex = r".*/" in the payload.
    document_name_additional_regex_list = [bulk_document.get("name_regex", r"")]

    # Fetch the nickname field from the payload (if it is specified):
    nickname_field = bulk_document.get("nickname")

    # Nicknames are very limited in terms of allowed characters.
    # For nicknames we need additional regexp as we need to
    # replace all non-alphanumeric, non-space characters with ""
    # We also preserve hyphens in the first step to replace
    # them below with underscores. This is important to avoid
    # that different spellings of names produce different nicknames.
    # We want spellings with spaces match spellings with hyphens.
    # For this the workspace names have a regexp "-| " in the payload.
    nickname_additional_regex_list = [r"[^\w\s-]"]

    total_rows = len(partition)

    # Process all rows in the partition that was given to this thread:
    for index, row in partition.iterrows():
        # Calculate percentage of completion:
        percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

        self.logger.info(
            "Processing data row -> %s for bulk document creation (%.2f %% complete)...",
            str(index),
            percent_complete,
        )

        # Clear variables to ensure clean state for each row:
        parent_id = None
        document_id = None

        # Create a copy of the mutable operations list as we may
        # want to modify it per row:
        row_operations = list(operations)

        # Check if all data conditions to create the document are met
        conditions = bulk_document.get("conditions")
        if conditions:
            evaluated_condition = self.evaluate_conditions(
                conditions=conditions,
                row=row,
            )
            if not evaluated_condition:
                self.logger.info(
                    "Condition for bulk document row -> %s not met. Skipping row for document creation...",
                    str(index),
                )
                result["skipped_counter"] += 1
                continue

        # Check if all data conditions to create the document are met:
        if "create" in row_operations or "recreate" in row_operations:
            conditions_create = bulk_document.get("conditions_create")
            if conditions_create:
                evaluated_conditions_create = self.evaluate_conditions(
                    conditions=conditions_create,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_conditions_create:
                    self.logger.info(
                        "Create condition for bulk document row -> %s not met. Excluding create operation for current row...",
                        str(index),
                    )
                    if "create" in row_operations:
                        row_operations.remove("create")
                    if "recreate" in row_operations:
                        row_operations.remove("recreate")
            elif (
                "recreate" in row_operations
            ):  # we still create and recreate without conditions_create. But give a warning for 'recreate' without condition.
                self.logger.warning(
                    "No create condition provided but 'recreate' operation requested. This will recreate all existing documents!",
                )

        # Check if all data conditions to delete the document are met:
        if "delete" in row_operations:
            conditions_delete = bulk_document.get("conditions_delete")
            if conditions_delete:
                evaluated_conditions_delete = self.evaluate_conditions(
                    conditions=conditions_delete,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_conditions_delete:
                    self.logger.info(
                        "Delete condition for bulk document row -> %s not met. Excluding delete operation for current row...",
                        str(index),
                    )
                    row_operations.remove("delete")
            else:  # without delete_conditions we don't delete!!
                self.logger.warning(
                    "Delete operation requested for bulk documents but conditions for deletion are missing! (specify 'conditions_delete'!)",
                )
                row_operations.remove("delete")

        # Check if all data conditions to update the document are met:
        if "update" in row_operations:
            conditions_update = bulk_document.get("conditions_update")
            if conditions_update:
                evaluated_conditions_update = self.evaluate_conditions(
                    conditions=conditions_update,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_conditions_update:
                    self.logger.info(
                        "Update condition for bulk document row -> %s not met. Excluding update operation for current row...",
                        str(index),
                    )
                    row_operations.remove("update")

        # Determine the document name:
        document_name = self.replace_bulk_placeholders(
            input_string=name_field,
            row=row,
            replacements=replacements,
            additional_regex_list=document_name_additional_regex_list,
        )
        # If we cannot get the document_name from the
        # name_field we try the alternative name field
        # (if specified in payload via "name_alt"):
        if not document_name and name_field_alt:
            self.logger.debug(
                "Row -> %s does not have the data to resolve the placeholders in document name -> %s! Trying alternative name field -> %s...",
                str(index),
                name_field,
                name_field_alt,
            )
            # The additional regex list will make sure
            # that any path or URL element is stripped from
            # the value in the row element:
            document_name = self.replace_bulk_placeholders(
                input_string=name_field_alt,
                row=row,
                replacements=replacements,
                additional_regex_list=document_name_additional_regex_list,
            )
        if not document_name:
            self.logger.error(
                "Row -> %s does not have the data to resolve the placeholders in document name -> %s%s!",
                str(index),
                name_field,
                (" nor in alternative name field -> " + name_field_alt if name_field_alt else ""),
            )
            result["skipped_counter"] += 1
            continue

        # Cleanse the document name (allowed characters, maximum length):
        document_name = OTCS.cleanse_item_name(document_name)

        download_name = ""
        if download_name_field:
            # The additional regex list will make sure
            # that any path or URL element is stripped from
            # the value in the row element:
            download_name = self.replace_bulk_placeholders(
                input_string=download_name_field,
                row=row,
                replacements=replacements,
                additional_regex_list=document_name_additional_regex_list,
            )
        if not download_name:
            self.logger.warning(
                "Download name is empty or row -> %s does not have the data to resolve the placeholders in document download name -> '%s'. Using -> '%s' instead!",
                str(index),
                download_name_field,
                document_name,
            )
            # in this case we use the document_name also as the download_name:
            download_name = document_name

        # This is an optimization. We check if the document was created
        # in a former run. This helps if the customizer is re-run:
        if document_name and document_name in existing_documents:
            self.logger.info(
                "Document -> '%s' does already exist and has ID -> %s. Skipping...",
                document_name,
                existing_documents[document_name],
            )
            result["skipped_counter"] += 1
            continue

        # Determine the description field:
        description = (
            self.replace_bulk_placeholders(
                input_string=description_field,
                row=row,
            )
            if description_field
            else ""
        )

        # Determine the external creation date (if any):
        if external_create_date_field:
            external_create_date = self.replace_bulk_placeholders(
                input_string=external_create_date_field,
                row=row,
            )
        else:
            external_create_date = None

        # Determine the external modification date (if any):
        if external_modify_date_field:
            external_modify_date = self.replace_bulk_placeholders(
                input_string=external_modify_date_field,
                row=row,
            )
        else:
            external_modify_date = None

        # Determine the key field (if any):
        key = self.replace_bulk_placeholders(input_string=key_field, row=row) if key_field else None

        # check if workspace with this nickname does already exist.
        # we also store the nickname to assign it to the new workspace:
        if nickname_field:
            nickname = self.replace_bulk_placeholders(
                input_string=nickname_field,
                row=row,
                replacements=replacements,
                additional_regex_list=nickname_additional_regex_list,
            )
        else:
            nickname = None

        # Based on the evaluation of conditions_create, conditions_delete,
        # and conditions_update we could end up with an empty operations list.
        # In such cases we skip the further processing of this row:
        if not row_operations:
            self.logger.info(
                "Skip document -> '%s' as row operations is empty (no create, update, delete or recreate). This may be because conditions_create, conditions_delete, and conditions_update have been evaluated to false for this row.",
                document_name,
            )
            result["skipped_counter"] += 1
            continue

        self.logger.info(
            "Bulk process document -> '%s' using effective operations -> %s...",
            document_name,
            str(row_operations),
        )

        # We only need to download the files if we want more than just
        # the "delete" operation:
        if any(operation in ["create", "update", "recreate"] for operation in row_operations):
            file_name, mime_type = self.download_bulk_document(
                bulk_document=bulk_document,
                download_name=download_name,
                row=row,
                source_otcs=source_otcs,
                result=result,
            )

            if file_name is None:
                result["success"] = False
                result["failure_counter"] += 1
                continue

            self.logger.debug("Download name -> '%s'", download_name)
            self.logger.debug("Document name -> '%s'", document_name)
            self.logger.debug("File name -> '%s'", file_name)
            self.logger.debug("Mime type -> '%s'", mime_type)
        else:
            # if we only do a delete operation then there's no need for
            # processing local files for upload:
            file_name = None
            mime_type = None

        # Now we traverse a list of (multiple) workspaces
        # the document should be uploaded to:
        success = True
        workspaces = bulk_document.get("workspaces", [])
        for workspace in workspaces:
            # success will only be false if a config problem (failure)
            # and not just a data problem (skipped) has occured:
            parent_id, success = self.get_bulk_document_location(
                workspace=workspace,
                row=row,
                index=index,
                replacements=replacements,
            )

            if parent_id is None:
                continue

            #
            # Create the document in the target folder specified by parent_id:
            #

            # Check if the document does already exist.
            # First we try to look up the document by a unique
            # key that does not change over time.
            # For this we expect a "key" value to be defined for the
            # bulkDocument and one of the category / attribute item
            # to be marked with "is_key" = True. If we don't find the
            # document via key we try via parent and name.
            self.logger.info(
                "Check if document -> '%s' is already in target folder with ID -> %s%s...",
                document_name,
                parent_id,
                " (using key -> {})".format(key) if key is not None else "",
            )
            # Initialize variables - this is important!
            document_old_name = None
            document_id = None
            handle_name_clash = False
            document_modify_date = None

            # We have 4 cases to differentiate:

            # 1. key given + key found = name irrelevant, item exist
            # 2. key given + key not found = if name exist it is a name clash
            # 3. no key given + name found = item exist
            # 4. no key given + name not found = item does not exist

            # We are NOT trying to compensate for a failed key lookup with a name lookup!
            # Names are only relevant if no key is in payload!

            if key:
                key_attribute = next(
                    (cat_attr for cat_attr in categories if cat_attr.get("is_key", False) is True),
                    None,
                )
                if key_attribute:
                    cat_name = key_attribute.get("name", None)
                    att_name = key_attribute.get("attribute", None)
                    set_name = key_attribute.get("set", None)
                    # Try to find the node that has the given key attribute value:
                    response = self._otcs_frontend.lookup_nodes(
                        parent_node_id=parent_id,
                        category=cat_name,
                        attribute=att_name,
                        attribute_set=set_name,
                        value=key,
                    )
                    document_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                    if document_id:
                        # Case 1: key given + key found = name irrelevant, item exist
                        self.logger.info(
                            "Found existing document with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                            key,
                            cat_name,
                            att_name,
                            parent_id,
                        )
                        document_old_name = self._otcs_frontend.get_result_value(
                            response=response,
                            key="name",
                        )
                        if (
                            document_old_name != document_name
                            and document_old_name != document_name + " (" + key + ")"
                        ):
                            self.logger.info(
                                "Existing document with ID -> %s has the old name -> '%s' which is different from the new name -> '%s.'",
                                document_id,
                                document_old_name,
                                document_name,
                            )
                        else:
                            # if there was a name clash before and got resolved
                            # then we need to stick to the resolved name also for updates:
                            if document_old_name == document_name + " (" + key + ")":
                                self.logger.info(
                                    "Document name conflict was resolved before. Changing document name to -> '%s' to match former resolution.",
                                    document_old_name,
                                )
                                document_name = document_old_name
                            document_old_name = None
                        # We get the modify date of the existing document.
                        document_modify_date = self._otcs_frontend.get_result_value(
                            response=response,
                            key="modify_date",
                        )
                    else:
                        # Case 2: key given + key not found = if name exist it is a name clash
                        self.logger.info(
                            "Couldn't find existing document with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                            key,
                            cat_name,
                            att_name,
                            parent_id,
                        )
                        # Keep track that we need to handle a name clash as based on the key
                        # the document should not exist.
                        handle_name_clash = True
                else:
                    self.logger.error(
                        "Document has a key -> '%s' defined but none of the category attributes is marked as a key attribute ('is_key' is missing)!",
                        key,
                    )
                    success = False
                    continue
            # end if key
            else:
                # If we haven't a key we try by parent + name
                response = self._otcs_frontend.get_node_by_parent_and_name(
                    name=document_name,
                    parent_id=parent_id,
                )
                document_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if document_id:
                    # Case 3: no key given + name found = item exist
                    self.logger.info(
                        "Found existing document -> '%s' (%s) in parent with ID -> %s.",
                        document_name,
                        document_id,
                        parent_id,
                    )
                    # We get the modify date of the existing document.
                    document_modify_date = self._otcs_frontend.get_result_value(
                        response=response,
                        key="modify_date",
                    )
                elif not extract_zip or mime_type != "application/zip":
                    # Case 4: no key given + name not found = item does not exist
                    self.logger.info(
                        "Cannot find document -> '%s' in parent with ID -> %s.",
                        document_name,
                        parent_id,
                    )
                else:
                    # Edge case: no key given + name not found but it is a zip file that got extracted.
                    # So we don't really know if it exists yet.
                    self.logger.info(
                        "File -> '%s' is a zip file that potentially has been extracted into parent with ID -> %s before. Existence of extracted files will be determined later.",
                        document_name,
                        parent_id,
                    )

            # Check if we want to recreate an existing document
            # then we handle the "delete" part of "recreate" first:
            if document_id and "recreate" in row_operations:
                response = self._otcs_frontend.delete_node(
                    node_id=document_id,
                    purge=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to recreate existing document -> '%s' (%s) under parent ID -> %s! Delete failed.",
                        (document_name if document_old_name is None else document_old_name),
                        document_id,
                        parent_id,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Deleted existing document -> '%s' (%s) as part of the recreate operation.",
                    (document_name if document_old_name is None else document_old_name),
                    document_id,
                )
                result["delete_counter"] += 1
                document_id = None

            # Check if document does not exists - then we create a new document
            # if this is requested ("create" value in operations list in payload)
            if not document_id and ("create" in row_operations or "recreate" in row_operations):
                # The document does not exist in Content Server - so we
                # upload it now...

                # for Case 2 (we looked up the document by key but could have name collisions)
                # we need to see if we have name collisions:
                if handle_name_clash:
                    response = self._otcs_frontend.check_node_name(
                        parent_id=parent_id,
                        node_name=document_name,
                    )
                    # result is a list of names that clash - if it is empty we have no clash
                    if response.get("results"):
                        # We need to handle a race condition here: it could be that
                        # the document has been created by another thread. This is because
                        # the key could be in multiple rows of the data frame depending how the data loader works.
                        # We can also not assume that this can be resolved by making the key unique
                        # in the data source as it could be that a data set with the same key should
                        # go to multiple workspaces that then are folded into one by synonym resolution.
                        conflicting_node_id = response["results"][0].get("id")
                        conflicting_key = self._otcs_frontend.get_category_value_by_name(
                            node_id=conflicting_node_id,
                            category_name=cat_name,
                            attribute_name=att_name,
                            set_name=set_name,
                        )
                        if key == conflicting_key:
                            # We have a race condition as the two documents don't really clash but are identical.
                            # Just skip uploading the same document once more.
                            self.logger.warning(
                                "Detected a race condition in name clash handling! Skipping document upload!",
                            )
                            continue

                        # We add the suffix with the key which should be unique:
                        self.logger.warning(
                            "Document -> '%s' does already exist in workspace folder with ID -> %s and we need to handle the name clash and use name -> '%s'",
                            document_name,
                            parent_id,
                            document_name + " (" + key + ")",
                        )
                        document_name = document_name + " (" + key + ")"

                # If category data is in payload we substitute
                # the values with data from the current data row:
                if categories:
                    # Make sure the threads are not changing data structures that
                    # are shared between threads. categories is a list of dicts.
                    # list and dicts are "mutable" data structures in Python!
                    worker_categories = self.process_bulk_categories(
                        row=row,
                        index=index,
                        categories=categories,
                        replacements=replacements,
                    )
                    document_category_data = self.prepare_category_data(
                        categories_payload=worker_categories,
                        source_node_id=parent_id,
                    )
                # end if categories
                else:
                    document_category_data = {}

                response = self._otcs_frontend.upload_file_to_parent(
                    file_url=file_name,
                    file_name=document_name,
                    mime_type=mime_type,
                    parent_id=int(parent_id),
                    category_data=document_category_data,
                    description=description,
                    external_create_date=external_create_date,
                    external_modify_date=external_modify_date,
                    extract_zip=extract_zip,
                    replace_existing="update" in row_operations and "version" in update_operations,
                    show_error=False,
                )
                document_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if not document_id:
                    # We may have a race condition here. Double check the document does not exist
                    # before issuing an error:
                    response = self._otcs_frontend.get_node_by_parent_and_name(
                        parent_id=int(parent_id),
                        name=document_name,
                    )
                    document_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                if not document_id:
                    self.logger.error(
                        "Cannot create document -> '%s' (download name -> '%s') in folder/workspace with ID -> %s!",
                        document_name,
                        download_name,
                        parent_id,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Created document -> '%s' (ID -> %s, file -> '%s', mime type -> '%s'%s%s) in parent with ID -> %s.",
                    document_name,
                    document_id,
                    file_name,
                    mime_type,
                    ", description -> {}".format(description) if description else "",
                    ", size -> {}".format(os.path.getsize(file_name))
                    if os.path.exists(file_name)
                    else "",  # this can happen for files in zip files that have been cleaned up already
                    parent_id,
                )
                result["create_counter"] += 1

                # Check if metadata or images embeddings need to be updated for the document:
                aviator_metadata = bulk_document.get("aviator_metadata", False)
                aviator_images = bulk_document.get("aviator_images", False)
                aviator_remove_existing = bulk_document.get("aviator_remove_existing", False)
                if aviator_metadata or aviator_images or aviator_remove_existing:
                    self.logger.info(
                        "%s Content Aviator %s%s%s embedding via FEME for document or image -> '%s' (%s)...",
                        "Trigger" if not aviator_remove_existing else "Remove",
                        "metadata" if aviator_metadata else "",
                        " and " if aviator_metadata and aviator_images else "",
                        "image" if aviator_images else "",
                        document_name,
                        document_id,
                    )

                    self._otcs_frontend.aviator_embed_metadata(
                        node_id=document_id,
                        workspace_metadata=False,
                        document_metadata=aviator_metadata,
                        images=aviator_images,
                        wait_for_completion=False,
                        remove_existing=aviator_remove_existing,
                    )

            # end if not workspace_id and "create" in row_operations

            # If updates are requested we update the existing document with
            # a new document version and with fresh metadata from the payload.
            # Additionally we check the external modify date to support
            # incremental load for content that has really changed.
            # In addition we check that "delete" is not requested as otherwise it will
            # never go in elif "delete" ... below (and it does not make sense to update a document
            # that is deleted in the next step...)

            elif (
                document_id
                and "update" in row_operations
                and "delete" not in row_operations  # note the NOT !
                and OTCS.date_is_newer(
                    date_old=document_modify_date,
                    date_new=external_modify_date,
                )
            ):
                # If category data is in payload we substitute
                # the values with data from the current data row.
                # This is only done if "categories" update is not
                # excluded from the update_operations:
                if categories and "categories" in update_operations:
                    # Make sure the threads are not changing data structures that
                    # are shared between threads. categories is a list of dicts.
                    # list and dicts are "mutable" data structures in Python!
                    worker_categories = self.process_bulk_categories(
                        row=row,
                        index=index,
                        categories=categories,
                        replacements=replacements,
                    )
                    # document_category_data = self.prepare_item_create_form(
                    #     parent_id=parent_id,
                    #     categories=worker_categories,
                    #     subtype=self._otcs_frontend.ITEM_TYPE_DOCUMENT,
                    # )
                    # Transform the payload structure into the format
                    # the OTCS REST API requires:
                    document_category_data = self.prepare_category_data(
                        categories_payload=worker_categories,
                        source_node_id=document_id,
                        source_is_document=True,
                    )
                    if not document_category_data:
                        self.logger.error(
                            "Failed to prepare the updated category data for document -> '%s' (%s)!",
                            document_name,
                            str(document_id),
                        )
                        success = False
                        continue  # for index, row in partition.iterrows()
                # end if categories
                else:
                    document_category_data = {}

                self.logger.info(
                    "Update existing document -> '%s' (%s) with operations -> %s...",
                    document_old_name if document_old_name else document_name,
                    document_id,
                    str(update_operations),
                )
                # check if adding a version is requested:
                if "version" in update_operations:
                    response = self._otcs_frontend.add_document_version(
                        node_id=document_id,
                        file_url=file_name,
                        file_name=document_name,
                        mime_type=mime_type,
                        description=description,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to add new version to existing document -> '%s' (%s)!",
                            (document_old_name if document_old_name else document_name),
                            document_id,
                        )
                        success = False
                        continue
                if "purge" in update_operations:
                    max_versions = bulk_document.get("max_versions", 1)
                    response = self._otcs_frontend.purge_document_versions(
                        node_id=document_id, versions_to_keep=max_versions
                    )
                    if not response:
                        self.logger.error(
                            "Failed to purge versions of document -> '%s' (%s) to %d version%s!",
                            (document_old_name if document_old_name else document_name),
                            document_id,
                            max_versions,
                            "s" if max_versions > 1 else "",
                        )
                        success = False
                        continue
                response = self._otcs_frontend.update_item(
                    node_id=document_id,
                    parent_id=None,  # None = do not move item
                    item_name=(document_name if "name" in update_operations else None),
                    item_description=(description if "description" in update_operations else None),
                    category_data=(document_category_data if "categories" in update_operations else None),
                    external_create_date=external_create_date,
                    external_modify_date=external_modify_date,
                )
                if not response:
                    self.logger.error(
                        "Failed to update metadata of existing document -> '%s' (%s) with metadata -> %s!",
                        (document_old_name if document_old_name else document_name),
                        document_id,
                        str(document_category_data),
                    )
                    success = False
                    continue
                self.logger.info(
                    "Updated existing document -> '%s' (ID -> %s, file -> '%s', mime type -> '%s', description -> '%s'%s).",
                    document_name,
                    document_id,
                    file_name,
                    mime_type,
                    description,
                    ", size -> {}".format(os.path.getsize(file_name))
                    if os.path.exists(file_name)
                    else "",  # this can happen for files in zip files that have been cleaned up already
                )
                result["update_counter"] += 1

                # Check if metadata or image embeddings need to be updated for the document:
                aviator_metadata = bulk_document.get("aviator_metadata", False)
                aviator_images = bulk_document.get("aviator_images", False)
                aviator_remove_existing = bulk_document.get("aviator_remove_existing", False)
                if aviator_metadata or aviator_images or aviator_remove_existing:
                    self.logger.info(
                        "%s Content Aviator %s%s%s embedding via FEME for document or image -> '%s' (%s)...",
                        "Update" if not aviator_remove_existing else "Remove",
                        "metadata" if aviator_metadata else "",
                        " and " if aviator_metadata and aviator_images else "",
                        "image" if aviator_images else "",
                        document_name,
                        document_id,
                    )

                    self._otcs_frontend.aviator_embed_metadata(
                        node_id=document_id,
                        workspace_metadata=False,
                        document_metadata=aviator_metadata,
                        images=aviator_images,
                        wait_for_completion=False,
                        remove_existing=aviator_remove_existing,
                    )
            # end if workspace_id and "update" in row_operations
            elif document_id and "delete" in row_operations:
                # We delete with immediate purging to keep recycle bin clean
                # and to not run into issues with nicknames used in deleted items:
                response = self._otcs_frontend.delete_node(
                    node_id=document_id,
                    purge=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to bulk delete existing document -> '%s' (%s)!",
                        (document_old_name if document_old_name else document_name),
                        document_id,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Successfully deleted existing document -> '%s' (%s).",
                    document_old_name if document_old_name else document_name,
                    document_id,
                )
                result["delete_counter"] += 1
                document_id = None
            # end elif document_id and "delete" in row_operations

            # this is the plain old "if it does exist we just skip it" case:
            elif document_id:
                self.logger.info(
                    "Skipped existing document -> '%s' (%s).",
                    document_old_name if document_old_name else document_name,
                    document_id,
                )
            # this is the case where we just want to operate on existing documents (update or delete)
            # but they do not exist:
            elif not document_id and ("update" in row_operations or "delete" in row_operations):
                result["skipped_counter"] += 1
                self.logger.info(
                    "Skipped update/delete of non-existing document -> '%s'.",
                    document_old_name if document_old_name else document_name,
                )

            # The following code is executed for all operations
            # (create, recreate, update, delete):

            # Check if we want to set / update the nickname of the document.
            # Precondition is we have determined a nickname, we have the document ID
            # and the update of the nickname is either required (create, recreate)
            # or requested (update_operations include "nickname"):
            if (
                nickname
                and document_id
                and (
                    "create" in row_operations
                    or "recreate" in row_operations
                    or ("update" in row_operations and "nickname" in update_operations)
                )
            ):
                response = self._otcs_frontend.set_node_nickname(
                    node_id=document_id,
                    nickname=nickname,
                    show_error=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to assign nickname -> '%s' to document -> '%s'!",
                        nickname,
                        document_name,
                    )
            if document_id is not None:
                # Record the document name and ID to allow to read it from failure file
                # and speedup the process.
                result["documents"][document_name] = document_id

        # end for workspaces

        # We want the success, failure and skip counter
        # to consider only complete data frame rows. In
        # case of bulk documents we can potentially create
        # update or delete more than 1 document per row.
        # So we use the "success" variable to accumulate
        # this for all documents per row:
        if not success:
            result["success"] = False
            result["failure_counter"] += 1
        elif document_name not in result["documents"]:
            self.logger.info(
                "Bulk document -> '%s' was not uploaded to any workspace.",
                document_name,
            )
            result["skipped_counter"] += 1
        else:
            result["success_counter"] += 1

        # Make sure no temp documents are piling up except
        # we want it (e.g. if using cloud document storage):
        if file_name and os.path.exists(file_name) and bulk_document.get("delete_download", True):
            os.remove(file_name)
    # end for index, row in partition.iterrows()

    self.logger.info("End working...")

    results.append(result)

process_bulk_items(section_name='bulkItems')

Process bulkItems in payload and bulk create them in Content Server (multi-threaded).

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'bulkItems'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
27378
27379
27380
27381
27382
27383
27384
27385
27386
27387
27388
27389
27390
27391
27392
27393
27394
27395
27396
27397
27398
27399
27400
27401
27402
27403
27404
27405
27406
27407
27408
27409
27410
27411
27412
27413
27414
27415
27416
27417
27418
27419
27420
27421
27422
27423
27424
27425
27426
27427
27428
27429
27430
27431
27432
27433
27434
27435
27436
27437
27438
27439
27440
27441
27442
27443
27444
27445
27446
27447
27448
27449
27450
27451
27452
27453
27454
27455
27456
27457
27458
27459
27460
27461
27462
27463
27464
27465
27466
27467
27468
27469
27470
27471
27472
27473
27474
27475
27476
27477
27478
27479
27480
27481
27482
27483
27484
27485
27486
27487
27488
27489
27490
27491
27492
27493
27494
27495
27496
27497
27498
27499
27500
27501
27502
27503
27504
27505
27506
27507
27508
27509
27510
27511
27512
27513
27514
27515
27516
27517
27518
27519
27520
27521
27522
27523
27524
27525
27526
27527
27528
27529
27530
27531
27532
27533
27534
27535
27536
27537
27538
27539
27540
27541
27542
27543
27544
27545
27546
27547
27548
27549
27550
27551
27552
27553
27554
27555
27556
27557
27558
27559
27560
27561
27562
27563
27564
27565
27566
27567
27568
27569
27570
27571
27572
27573
27574
27575
27576
27577
27578
27579
27580
27581
27582
27583
27584
27585
27586
27587
27588
27589
27590
27591
27592
27593
27594
27595
27596
27597
27598
27599
27600
27601
27602
27603
27604
27605
27606
27607
27608
27609
27610
27611
27612
27613
27614
27615
27616
27617
27618
27619
27620
27621
27622
27623
27624
27625
27626
27627
27628
27629
27630
27631
27632
27633
27634
27635
27636
27637
27638
27639
27640
27641
27642
27643
27644
27645
27646
27647
27648
27649
27650
27651
27652
27653
27654
27655
27656
27657
27658
27659
27660
27661
27662
27663
27664
27665
27666
27667
27668
27669
27670
27671
27672
27673
27674
27675
27676
27677
27678
27679
27680
27681
27682
27683
27684
27685
27686
27687
27688
27689
27690
27691
27692
27693
27694
27695
27696
27697
27698
27699
27700
27701
27702
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_items")
def process_bulk_items(self, section_name: str = "bulkItems") -> bool:
    """Process bulkItems in payload and bulk create them in Content Server (multi-threaded).

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not self._bulk_items:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    # For efficient idem-potent operation we may want to see which workspaces
    # have already been processed before:
    if self.check_status_file(
        payload_section_name=section_name,
        payload_specific=True,
        prefix="failure_",
    ):
        self.logger.info(
            "Found failure file. Trying to reprocess just the failed ones...",
        )
        # Read payload from where we left it last time
        self._bulk_items = self.get_status_file(
            payload_section_name=section_name,
            prefix="failure_",
        )
        if not self._bulk_items:
            self.logger.error(
                "Cannot load existing bulk items failure file. Bailing out!",
            )
            return False

    success: bool = True

    for bulk_item in self._bulk_items:
        # Check if element has been disabled in payload (enabled = false).
        # In this case we skip the element:
        enabled = bulk_item.get("enabled", True)
        if not enabled:
            self.logger.info("Payload for bulk items is disabled. Skipping...")
            continue

        # The payload element must have a "data_source" key:
        data_source_name = bulk_item.get("data_source", None)
        if not data_source_name:
            self.logger.error("Missing data source name in bulk items!")
            success = False
            continue

        copy_data_source = bulk_item.get("copy_data_source", False)
        force_reload = bulk_item.get("force_reload", True)

        # Load and prepare the data source for the bulk processing:
        if copy_data_source:
            self.logger.info(
                "Take a copy of data source -> %s to avoid side-effects for repeative usage of the data source...",
                data_source_name,
            )
            data = Data(
                self.process_bulk_datasource(
                    data_source_name=data_source_name,
                    force_reload=force_reload,
                ),
                logger=self.logger,
            )
        else:
            self.logger.info(
                "Use original data source -> '%s' and not do a copy.",
                bulk_item["data_source"],
            )
            data = self.process_bulk_datasource(
                data_source_name=data_source_name,
                force_reload=force_reload,
            )
        if (
            data is None
            or data.get_data_frame() is None  # the 2nd check is important for the "copy_data_source" case!
        ):  # important to use "is None" here!
            self.logger.error(
                "Failed to load data source -> '%s' for bulk items!",
                data_source_name,
            )
            success = False
            continue

        # Check if fields with list substructures should be exploded.
        # We may want to do this outside the bulkDatasource to only
        # explode for bulkItems and not for bulkWorkspaces or
        # bulkWorkspaceRelationships:
        explosions = bulk_item.get("explosions", [])
        for explosion in explosions:
            # explode field can be a string or a list
            # exploding multiple fields at once avoids
            # combinatorial explosions - this is VERY
            # different from exploding columns one after the other!
            if "explode_field" not in explosion and "explode_fields" not in explosion:
                self.logger.error("Missing explosion field(s)!")
                continue
            # we want to be backwards compatible...
            explode_fields = (
                explosion["explode_field"] if "explode_field" in explosion else explosion["explode_fields"]
            )
            flatten_fields = explosion.get("flatten_fields", [])
            split_string_to_list = explosion.get("split_string_to_list", False)
            list_splitter = explosion.get("list_splitter", None)
            self.logger.info(
                "Starting explosion of bulk items by field(s) -> %s (type -> %s). Size of data frame before explosion -> %s.",
                explode_fields,
                str(type(explode_fields)),
                str(len(data)),
            )
            data.explode_and_flatten(
                explode_fields=explode_fields,
                flatten_fields=flatten_fields,
                make_unique=False,
                split_string_to_list=split_string_to_list,
                separator=list_splitter,
                reset_index=True,
            )
            self.logger.info(
                "Size of data frame after explosion -> %s.",
                str(len(data)),
            )
        # end for explosion in explosions

        # Keep only selected rows if filters are specified in bulkDocuments.
        # We have this _after_ "explosions" to allow access to subfields as well.
        # We have this _before_ "sorting" and "deduplication" as we may keep the wrong
        # rows otherwise (unique / deduplication always keeps the first matching row).
        # We may want to do this outside the bulkDatasource to only
        # filter for bulkDocuments and not for bulkWorkspaces or
        # bulkWorkspaceRelationships:
        filters = bulk_item.get("filters", [])
        if filters:
            data.filter(conditions=filters)

        # Sort the data frame if "sort" specified in payload. We may want to do this to have a
        # higher chance that rows with workspace names that may collapse into
        # one name are put into the same partition. This can avoid race conditions
        # between different Python threads.
        # We do this before deduplication (unique) below as sorting has an influence
        # on which duplicates are kept.
        sort_fields = bulk_item.get("sort", [])
        if sort_fields:
            self.logger.info(
                "Start sorting of bulk items data frame based on fields (columns) -> %s...",
                str(sort_fields),
            )
            data.sort(sort_fields=sort_fields, inplace=True)
            self.logger.info(
                "Sorting of bulk items data frame based on fields (columns) -> %s completed.",
                str(sort_fields),
            )

        # Check if duplicate rows for given fields should be removed. It is
        # important to do this after sorting as Pandas always keep the first occurance,
        # so ordering plays an important role in deduplication!
        unique_fields = bulk_item.get("unique", [])
        if unique_fields:
            self.logger.info(
                "Starting deduplication of data frame for bulk items with unique fields -> %s. Size of data frame before deduplication -> %s.",
                str(unique_fields),
                str(len(data)),
            )
            data.deduplicate(unique_fields=unique_fields, inplace=True)
            self.logger.info(
                "Size of data frame after deduplication -> %s.",
                str(len(data)),
            )

        # Read name field from payload:
        name_field = bulk_item.get("name", None)
        if not name_field:
            self.logger.error(
                "Bulk item needs a name field! Skipping to next bulk item...",
            )
            success = False
            continue

        self._log_header_callback(
            text="Process bulk items -> '{}' from data source -> '{}'".format(
                name_field,
                data_source_name,
            ),
            char="-",
        )

        # Read optional description field from payload:
        description_field = bulk_item.get("description", None)

        # Read the optional categories payload:
        categories = bulk_item.get("categories", None)
        if not categories:
            self.logger.info(
                "Bulk item payload has no category data! Will leave category attributes empty...",
            )

        # Should existing items be updated? False (= no) is the default.
        operations = bulk_item.get("operations", ["create"])

        self.logger.info(
            "Bulk create items (name field -> %s. Operations -> %s.)",
            name_field,
            str(operations),
        )

        # See if bulkWorkspace definition has a specific thread number
        # otherwise it is read from a global environment variable
        bulk_thread_number = int(
            bulk_item.get("thread_number", BULK_THREAD_NUMBER),
        )

        partitions = data.partitionate(bulk_thread_number)

        # Create a list to hold the threads
        threads = []
        results = []

        # Create and start a thread for each partition
        for index, partition in enumerate(partitions, start=1):
            # start a thread executing the process_bulk_items_worker() method below:
            thread = threading.Thread(
                name=f"{section_name}_{index:02}",
                target=self.thread_wrapper,
                args=(
                    self.process_bulk_items_worker,
                    bulk_item,
                    partition,
                    name_field,
                    description_field,
                    categories,
                    operations,
                    results,
                ),
            )
            self.logger.info("Starting thread -> '%s'...", str(thread.name))
            thread.start()
            threads.append(thread)
            # Avoid that all threads start at the exact same time with
            # potentially expired cookies that cases race conditions:
            time.sleep(1)
        # end for index, partition in enumerate(partitions, start=1)

        # Wait for all threads to complete
        for thread in threads:
            self.logger.info(
                "Waiting for thread -> '%s' to complete...",
                str(thread.name),
            )
            thread.join()
            self.logger.info("Thread -> '%s' has completed.", str(thread.name))

        if "items" not in bulk_item:
            bulk_item["items"] = {}

        # Print statistics for each thread. In addition,
        # check if all threads have completed without error / failure.
        # If there's a single failure in on of the thread results we
        # set 'success' variable to False.
        results.sort(key=lambda x: x["thread_name"])
        for result in results:
            self.logger.info(
                "Thread -> '%s' completed with overall status '%s'.",
                str(result["thread_name"]),
                "successful" if result["success"] else "failed",
            )
            self.logger.info(
                "Thread -> '%s' processed %s data rows with %s successful, %s failed, and %s skipped.",
                str(result["thread_name"]),
                str(
                    result["success_counter"] + result["failure_counter"] + result["skipped_counter"],
                ),
                str(result["success_counter"]),
                str(result["failure_counter"]),
                str(result["skipped_counter"]),
            )
            self.logger.info(
                "Thread -> '%s' created %s items, updated %s items, and deleted %s items.",
                str(result["thread_name"]),
                str(result["create_counter"]),
                str(result["update_counter"]),
                str(result["delete_counter"]),
            )

            if not result["success"]:
                success = False
            # Record all generated items. This should allow us
            # to restart in case of failures and avoid trying to
            # create items that have been successfully created before.
            bulk_item["items"].update(result["items"])
        # end for result in results
        self._log_header_callback(
            text="Completed processing of bulk items -> '{}' using data source -> '{}'".format(
                name_field,
                data_source_name,
            ),
            char="-",
        )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._bulk_items,
    )

    return success

process_bulk_items_worker(bulk_item, partition, name_field, description_field, categories=None, operations=None, results=None)

Thread worker to create items in bulk.

Each worker thread gets a partition of the rows that include the data required for the item creation.

Parameters:

Name Type Description Default
bulk_item dict

The bulkItem payload element.

required
partition DataFrame

Data partition with rows to process by this thread.

required
name_field str

The payload field where the item name is stored.

required
description_field str

The payload field where the item description is stored.

required
categories list

A list of category dictionaries.

None
operations list

A list of operations that should be applyed on workspaces. Possible values: "create", "update", "delete", "recreate".

None
results list

A mutable list of thread results.

None
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
27706
27707
27708
27709
27710
27711
27712
27713
27714
27715
27716
27717
27718
27719
27720
27721
27722
27723
27724
27725
27726
27727
27728
27729
27730
27731
27732
27733
27734
27735
27736
27737
27738
27739
27740
27741
27742
27743
27744
27745
27746
27747
27748
27749
27750
27751
27752
27753
27754
27755
27756
27757
27758
27759
27760
27761
27762
27763
27764
27765
27766
27767
27768
27769
27770
27771
27772
27773
27774
27775
27776
27777
27778
27779
27780
27781
27782
27783
27784
27785
27786
27787
27788
27789
27790
27791
27792
27793
27794
27795
27796
27797
27798
27799
27800
27801
27802
27803
27804
27805
27806
27807
27808
27809
27810
27811
27812
27813
27814
27815
27816
27817
27818
27819
27820
27821
27822
27823
27824
27825
27826
27827
27828
27829
27830
27831
27832
27833
27834
27835
27836
27837
27838
27839
27840
27841
27842
27843
27844
27845
27846
27847
27848
27849
27850
27851
27852
27853
27854
27855
27856
27857
27858
27859
27860
27861
27862
27863
27864
27865
27866
27867
27868
27869
27870
27871
27872
27873
27874
27875
27876
27877
27878
27879
27880
27881
27882
27883
27884
27885
27886
27887
27888
27889
27890
27891
27892
27893
27894
27895
27896
27897
27898
27899
27900
27901
27902
27903
27904
27905
27906
27907
27908
27909
27910
27911
27912
27913
27914
27915
27916
27917
27918
27919
27920
27921
27922
27923
27924
27925
27926
27927
27928
27929
27930
27931
27932
27933
27934
27935
27936
27937
27938
27939
27940
27941
27942
27943
27944
27945
27946
27947
27948
27949
27950
27951
27952
27953
27954
27955
27956
27957
27958
27959
27960
27961
27962
27963
27964
27965
27966
27967
27968
27969
27970
27971
27972
27973
27974
27975
27976
27977
27978
27979
27980
27981
27982
27983
27984
27985
27986
27987
27988
27989
27990
27991
27992
27993
27994
27995
27996
27997
27998
27999
28000
28001
28002
28003
28004
28005
28006
28007
28008
28009
28010
28011
28012
28013
28014
28015
28016
28017
28018
28019
28020
28021
28022
28023
28024
28025
28026
28027
28028
28029
28030
28031
28032
28033
28034
28035
28036
28037
28038
28039
28040
28041
28042
28043
28044
28045
28046
28047
28048
28049
28050
28051
28052
28053
28054
28055
28056
28057
28058
28059
28060
28061
28062
28063
28064
28065
28066
28067
28068
28069
28070
28071
28072
28073
28074
28075
28076
28077
28078
28079
28080
28081
28082
28083
28084
28085
28086
28087
28088
28089
28090
28091
28092
28093
28094
28095
28096
28097
28098
28099
28100
28101
28102
28103
28104
28105
28106
28107
28108
28109
28110
28111
28112
28113
28114
28115
28116
28117
28118
28119
28120
28121
28122
28123
28124
28125
28126
28127
28128
28129
28130
28131
28132
28133
28134
28135
28136
28137
28138
28139
28140
28141
28142
28143
28144
28145
28146
28147
28148
28149
28150
28151
28152
28153
28154
28155
28156
28157
28158
28159
28160
28161
28162
28163
28164
28165
28166
28167
28168
28169
28170
28171
28172
28173
28174
28175
28176
28177
28178
28179
28180
28181
28182
28183
28184
28185
28186
28187
28188
28189
28190
28191
28192
28193
28194
28195
28196
28197
28198
28199
28200
28201
28202
28203
28204
28205
28206
28207
28208
28209
28210
28211
28212
28213
28214
28215
28216
28217
28218
28219
28220
28221
28222
28223
28224
28225
28226
28227
28228
28229
28230
28231
28232
28233
28234
28235
28236
28237
28238
28239
28240
28241
28242
28243
28244
28245
28246
28247
28248
28249
28250
28251
28252
28253
28254
28255
28256
28257
28258
28259
28260
28261
28262
28263
28264
28265
28266
28267
28268
28269
28270
28271
28272
28273
28274
28275
28276
28277
28278
28279
28280
28281
28282
28283
28284
28285
28286
28287
28288
28289
28290
28291
28292
28293
28294
28295
28296
28297
28298
28299
28300
28301
28302
28303
28304
28305
28306
28307
28308
28309
28310
28311
28312
28313
28314
28315
28316
28317
28318
28319
28320
28321
28322
28323
28324
28325
28326
28327
28328
28329
28330
28331
28332
28333
28334
28335
28336
28337
28338
28339
28340
28341
28342
28343
28344
28345
28346
28347
28348
28349
28350
28351
28352
28353
28354
28355
28356
28357
28358
28359
28360
28361
28362
28363
28364
28365
28366
28367
28368
28369
28370
28371
28372
28373
28374
28375
28376
28377
28378
28379
28380
28381
28382
28383
28384
28385
28386
28387
28388
28389
28390
28391
28392
28393
28394
28395
28396
28397
28398
28399
28400
28401
28402
28403
28404
28405
28406
28407
28408
28409
28410
28411
28412
28413
28414
28415
28416
28417
28418
28419
28420
28421
28422
28423
28424
28425
28426
28427
28428
28429
28430
28431
28432
28433
28434
28435
28436
28437
28438
28439
28440
28441
28442
28443
28444
28445
28446
28447
28448
28449
28450
28451
28452
28453
28454
28455
28456
28457
28458
28459
28460
28461
28462
28463
28464
28465
28466
28467
28468
28469
28470
28471
28472
28473
28474
28475
28476
28477
28478
28479
28480
28481
28482
28483
28484
28485
28486
28487
28488
28489
28490
28491
28492
28493
28494
28495
28496
28497
28498
28499
28500
28501
28502
28503
28504
28505
28506
28507
28508
28509
28510
28511
28512
28513
28514
28515
28516
28517
28518
28519
28520
28521
28522
28523
28524
28525
28526
28527
28528
28529
28530
28531
28532
28533
28534
28535
28536
28537
28538
28539
28540
28541
28542
28543
28544
28545
28546
28547
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_items_worker")
def process_bulk_items_worker(
    self,
    bulk_item: dict,
    partition: pd.DataFrame,
    name_field: str,
    description_field: str,
    categories: list | None = None,
    operations: list | None = None,
    results: list | None = None,
) -> None:
    """Thread worker to create items in bulk.

    Each worker thread gets a partition of the rows that include
    the data required for the item creation.

    Args:
        bulk_item (dict):
            The bulkItem payload element.
        partition (pd.DataFrame):
            Data partition with rows to process by this thread.
        name_field (str):
            The payload field where the item name is stored.
        description_field (str):
            The payload field where the item description is stored.
        categories (list, optional):
            A list of category dictionaries.
        operations (list, optional):
            A list of operations that should be applyed on workspaces.
            Possible values: "create", "update", "delete", "recreate".
        results (list, optional):
            A mutable list of thread results.

    """

    thread_id = threading.get_ident()
    thread_name = threading.current_thread().name

    self.logger.info(
        "Start working on data frame partition of size -> %s to bulk create items...",
        str(len(partition)),
    )

    # Avoid linter warnings - so make parameter default None while we
    # actually want ["create"] to be the default:
    if operations is None:
        operations = ["create"]

    result = {}
    result["thread_id"] = thread_id
    result["thread_name"] = thread_name
    result["success_counter"] = 0
    result["failure_counter"] = 0
    result["skipped_counter"] = 0
    result["create_counter"] = 0
    result["update_counter"] = 0
    result["delete_counter"] = 0
    result["items"] = {}
    result["success"] = True

    # Check if items have been processed before, e.i. testing
    # if a "items" key exists and if it is pointing to a non-empty list:
    # Additionally we check that workspace updates are not enforced:
    if (
        bulk_item.get("items")
        and "update" not in operations
        and "delete" not in operations
        and "recreate" not in operations
    ):
        existing_items = bulk_item["items"]
        self.logger.info(
            "Found %s already processed items. Try to complete the job...",
            str(len(existing_items)),
        )
    else:
        existing_items = {}

    # See if external creation and modification dates are in the payload data:
    external_modify_date_field = bulk_item.get("external_modify_date")
    external_create_date_field = bulk_item.get("external_create_date")

    # See if we have a key field to uniquely identify an existing item:
    key_field = bulk_item.get("key")

    # Get dictionary of replacements for bulk item creations
    # this we will be used of all places data is read from the
    # data frame. Each dictionary item has the field name as the
    # dictionary key and a list of regular expressions as dictionary value
    replacements = bulk_item.get("replacements")

    # In case the name cannot be resolved we allow to
    # specify an alternative name field in the payload.
    name_field_alt = bulk_item.get("name_alt")

    # Determine the type of the item (OTCS subtype ID):
    type_field = bulk_item.get("type", self._otcs_frontend.ITEM_TYPE_URL)

    # In case the type is URL, determine the URL field:
    url_field = bulk_item.get("url")

    # In case the type is Shortcut, determine the origin field:
    original_nickname_field = bulk_item.get("original_nickname")
    original_path = bulk_item.get("original_path")

    # Document names are limited in terms of allowed characters.
    # E.g. if you don't want any path elements and "/" dividers
    # in the item name you could use name_regex = r".*/" in the payload.
    item_name_additional_regex_list = [bulk_item.get("name_regex", r"")]

    # Fetch the nickname field from the payload (if it is specified):
    nickname_field = bulk_item.get("nickname")

    # Nicknames are very limited in terms of allowed characters.
    # For nicknames we need additional regexp as we need to
    # replace all non-alphanumeric, non-space characters with ""
    # We also preserve hyphens in the first step to replace
    # them below with underscores. This is important to avoid
    # that different spellings of names produce different nicknames.
    # We want spellings with spaces match spellings with hyphens.
    # For this the workspace names have a regexp "-| " in the payload.
    nickname_additional_regex_list = [r"[^\w\s-]"]

    total_rows = len(partition)

    # Process all rows in the partition that was given to this thread:
    for index, row in partition.iterrows():
        # Calculate percentage of completion:
        percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

        self.logger.info(
            "Processing data row -> %s for bulk item creation (%.2f %% complete)...",
            str(index),
            percent_complete,
        )

        # Clear variables to ensure clean state for each row:
        parent_id = None
        item_id = None

        # Create a copy of the mutable operations list as we may
        # want to modify it per row:
        row_operations = list(operations)

        # Check if all data conditions to create the item are met
        conditions = bulk_item.get("conditions")
        if conditions:
            evaluated_condition = self.evaluate_conditions(
                conditions=conditions,
                row=row,
            )
            if not evaluated_condition:
                self.logger.info(
                    "Condition for bulk item row -> %s not met. Skipping row for item creation...",
                    str(index),
                )
                result["skipped_counter"] += 1
                continue

        # Check if all data conditions to create the item are met:
        if "create" in row_operations or "recreate" in row_operations:
            conditions_create = bulk_item.get("conditions_create")
            if conditions_create:
                evaluated_conditions_create = self.evaluate_conditions(
                    conditions=conditions_create,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_conditions_create:
                    self.logger.info(
                        "Create condition for bulk item row -> %s not met. Excluding create operation for current row...",
                        str(index),
                    )
                    if "create" in row_operations:
                        row_operations.remove("create")
                    if "recreate" in row_operations:
                        row_operations.remove("recreate")
            elif (
                "recreate" in row_operations
            ):  # we still create and recreate without conditions_create. But give a warning for 'recreate' without condition.
                self.logger.warning(
                    "No create condition provided but 'recreate' operation requested. This will recreate all existing items!",
                )

        # Check if all data conditions to delete the item are met:
        if "delete" in row_operations:
            conditions_delete = bulk_item.get("conditions_delete")
            if conditions_delete:
                evaluated_conditions_delete = self.evaluate_conditions(
                    conditions=conditions_delete,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_conditions_delete:
                    self.logger.info(
                        "Delete condition for bulk item row -> %s not met. Excluding delete operation for current row...",
                        str(index),
                    )
                    row_operations.remove("delete")
            else:  # without delete_conditions we don't delete!!
                self.logger.warning(
                    "Delete operation requested for bulk items but conditions for deletion are missing! (specify 'conditions_delete'!)",
                )
                row_operations.remove("delete")

        # Check if all data conditions to update the item are met:
        if "update" in row_operations:
            conditions_update = bulk_item.get("conditions_update")
            if conditions_update:
                evaluated_conditions_update = self.evaluate_conditions(
                    conditions=conditions_update,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_conditions_update:
                    self.logger.info(
                        "Update condition for bulk item row -> %s not met. Excluding update operation for current row...",
                        str(index),
                    )
                    row_operations.remove("update")

        # Determine the item name:
        item_name = self.replace_bulk_placeholders(
            input_string=name_field,
            row=row,
            replacements=replacements,
            additional_regex_list=item_name_additional_regex_list,
        )
        # If we cannot get the item_name from the
        # name_field we try the alternative name field
        # (if specified in payload via "name_alt"):
        if not item_name and name_field_alt:
            self.logger.debug(
                "Row -> %s does not have the data to resolve the placeholders in item name -> %s! Trying alternative name field -> %s...",
                str(index),
                name_field,
                name_field_alt,
            )
            # The additional regex list will make sure
            # that any path or URL element is stripped from
            # the value in the row element:
            item_name = self.replace_bulk_placeholders(
                input_string=name_field_alt,
                row=row,
                replacements=replacements,
                additional_regex_list=item_name_additional_regex_list,
            )
        if not item_name:
            self.logger.error(
                "Row -> %s does not have the data to resolve the placeholders in item name -> %s%s!",
                str(index),
                name_field,
                (" nor in alternative name field -> " + name_field_alt if name_field_alt else ""),
            )
            result["skipped_counter"] += 1
            continue

        # Cleanse the item name (allowed characters, maximum length):
        item_name = OTCS.cleanse_item_name(item_name)

        # This is an optimization. We check if the item was created
        # in a former run. This helps if the customizer is re-run:
        if item_name and item_name in existing_items:
            self.logger.info(
                "Item -> '%s' does already exist and has ID -> %s. Skipping...",
                item_name,
                existing_items[item_name],
            )
            result["skipped_counter"] += 1
            continue

        # Determine the description field:
        description = (
            self.replace_bulk_placeholders(
                input_string=description_field,
                row=row,
            )
            if description_field
            else ""
        )

        # Determine the item type:
        item_type = self.replace_bulk_placeholders(
            input_string=type_field,
            row=row,
        )

        # Determine the item type:
        item_url = self.replace_bulk_placeholders(
            input_string=url_field,
            row=row,
        )

        # Determine the item origin:
        original_nickname = self.replace_bulk_placeholders(
            input_string=original_nickname_field,
            row=row,
        )

        # Determine the item origin:
        self.replace_bulk_placeholders_list(
            input_list=original_path,
            row=row,
        )

        item_original_node = None
        # First try to get the original item for the shortcut via the nickname:
        if original_nickname:
            item_original_node = self._otcs_frontend.get_node_from_nickname(nickname=original_nickname)
        # FIf that didn't resolve the original node we try a path if provided in the payload:
        if not item_original_node and original_path:
            item_original_node = self._otcs_frontend.get_node_by_volume_and_path(
                volume_type=self._otcs_frontend.VOLUME_TYPE_ENTERPRISE_WORKSPACE,
                path=original_path,
            )
        item_original_node_id = self._otcs_frontend.get_result_value(response=item_original_node, key="id")

        # Determine the external creation date (if any):
        if external_create_date_field:
            external_create_date = self.replace_bulk_placeholders(
                input_string=external_create_date_field,
                row=row,
            )
        else:
            external_create_date = None

        # Determine the external modification date (if any):
        if external_modify_date_field:
            external_modify_date = self.replace_bulk_placeholders(
                input_string=external_modify_date_field,
                row=row,
            )
        else:
            external_modify_date = None

        # Determine the key field (if any):
        key = self.replace_bulk_placeholders(input_string=key_field, row=row) if key_field else None

        # check if workspace with this nickname does already exist.
        # we also store the nickname to assign it to the new workspace:
        if nickname_field:
            nickname = self.replace_bulk_placeholders(
                input_string=nickname_field,
                row=row,
                replacements=replacements,
                additional_regex_list=nickname_additional_regex_list,
            )
        else:
            nickname = None

        # Based on the evaluation of conditions_create, conditions_delete,
        # and conditions_update we could end up with an empty operations list.
        # In such cases we skip the further processing of this row:
        if not row_operations:
            self.logger.info(
                "Skip item -> '%s' as row operations is empty (no create, update, delete or recreate). This may be because conditions_create, conditions_delete, and conditions_update have been evaluated to false for this row.",
                item_name,
            )
            result["skipped_counter"] += 1
            continue

        self.logger.info(
            "Bulk process item -> '%s' using effective operations -> %s...",
            item_name,
            str(row_operations),
        )

        # Now we traverse a list of (multiple) workspaces
        # the item should be added to:
        success = True
        workspaces = bulk_item.get("workspaces", [])
        for workspace in workspaces:
            # success will only be false if a config problem (failure)
            # and not just a data problem (skipped) has occured:
            parent_id, success = self.get_bulk_document_location(
                workspace=workspace,
                row=row,
                index=index,
                replacements=replacements,
            )

            if parent_id is None:
                continue

            #
            # Create the item in the target folder specified by parent_id:
            #

            # Check if the item does already exist.
            # First we try to look up the item by a unique
            # key that does not change over time.
            # For this we expect a "key" value to be defined for the
            # bulkDocument and one of the category / attribute item
            # to be marked with "is_key" = True. If we don't find the
            # item via key we try via parent and name.
            self.logger.info(
                "Check if item -> '%s' is already in target folder with ID -> %s%s...",
                item_name,
                parent_id,
                " (using key -> {})".format(key) if key is not None else "",
            )
            # Initialize variables - this is important!
            item_old_name = None
            item_id = None
            handle_name_clash = False
            item_modify_date = None

            # We have 4 cases to differentiate:

            # 1. key given + key found = name irrelevant, item exist
            # 2. key given + key not found = if name exist it is a name clash
            # 3. no key given + name found = item exist
            # 4. no key given + name not found = item does not exist

            # We are NOT trying to compensate for a failed key lookup with a name lookup!
            # Names are only relevant if no key is in payload!

            if key:
                key_attribute = next(
                    (cat_attr for cat_attr in categories if cat_attr.get("is_key", False) is True),
                    None,
                )
                if key_attribute:
                    cat_name = key_attribute.get("name", None)
                    att_name = key_attribute.get("attribute", None)
                    set_name = key_attribute.get("set", None)
                    # Try to find the node that has the given key attribute value:
                    response = self._otcs_frontend.lookup_nodes(
                        parent_node_id=parent_id,
                        category=cat_name,
                        attribute=att_name,
                        attribute_set=set_name,
                        value=key,
                    )
                    item_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                    if item_id:
                        # Case 1: key given + key found = name irrelevant, item exist
                        self.logger.info(
                            "Found existing item with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                            key,
                            cat_name,
                            att_name,
                            parent_id,
                        )
                        item_old_name = self._otcs_frontend.get_result_value(
                            response=response,
                            key="name",
                        )
                        if item_old_name != item_name and item_old_name != item_name + " (" + key + ")":
                            self.logger.info(
                                "Existing item with ID -> %s has the old name -> '%s' which is different from the new name -> '%s.'",
                                item_id,
                                item_old_name,
                                item_name,
                            )
                        else:
                            # if there was a name clash before and got resolved
                            # then we need to stick to the resolved name also for updates:
                            if item_old_name == item_name + " (" + key + ")":
                                self.logger.info(
                                    "Document name conflict was resolved before. Changing item name to -> '%s' to match former resolution.",
                                    item_old_name,
                                )
                                item_name = item_old_name
                            item_old_name = None
                        # We get the modify date of the existing item.
                        item_modify_date = self._otcs_frontend.get_result_value(
                            response=response,
                            key="modify_date",
                        )
                    else:
                        # Case 2: key given + key not found = if name exist it is a name clash
                        self.logger.info(
                            "Couldn't find existing item with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                            key,
                            cat_name,
                            att_name,
                            parent_id,
                        )
                        handle_name_clash = True
                else:
                    self.logger.error(
                        "Item has a key -> '%s' defined but none of the category attributes is marked as a key attribute ('is_key' is missing)!",
                        key,
                    )
                    success = False
                    continue
                # end if key_attribute:
            # end if key:
            else:
                # If we haven't a key we try by parent + name
                response = self._otcs_frontend.get_node_by_parent_and_name(
                    name=item_name,
                    parent_id=parent_id,
                )
                item_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if item_id:
                    # Case 3: no key given + name found = item exist
                    self.logger.info(
                        "Found existing item -> '%s' (%s) in parent with ID -> %s.",
                        item_name,
                        item_id,
                        parent_id,
                    )
                    # We get the modify date of the existing item.
                    item_modify_date = self._otcs_frontend.get_result_value(
                        response=response,
                        key="modify_date",
                    )
                else:
                    # Case 4: no key given + name not found = item does not exist
                    self.logger.info(
                        "No existing item -> '%s' in parent with ID -> %s.",
                        item_name,
                        parent_id,
                    )

            # Check if we want to recreate an existing item
            # then we handle the "delete" part of "recreate" first:
            if item_id and "recreate" in row_operations:
                response = self._otcs_frontend.delete_node(
                    node_id=item_id,
                    purge=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to recreate existing item -> '%s' (%s) under parent ID -> %s! Delete failed.",
                        (item_name if item_old_name is None else item_old_name),
                        item_id,
                        parent_id,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Successfully deleted existing item -> '%s' (%s) as part of the recreate operation.",
                    (item_name if item_old_name is None else item_old_name),
                    item_id,
                )
                result["delete_counter"] += 1
                item_id = None

            # Check if item does not exists - then we create a new item
            # if this is requested ("create" value in operations list in payload)
            if not item_id and ("create" in row_operations or "recreate" in row_operations):
                # The item does not exist in Content Server - so we
                # add it now...

                # for Case 2 (we looked up the item by key but could have name collisions)
                # we need to see if we have name collisions:
                if handle_name_clash:
                    response = self._otcs_frontend.check_node_name(
                        parent_id=parent_id,
                        node_name=item_name,
                    )
                    # result is a list of names that clash - if it is empty we have no clash
                    if response.get("results"):
                        # We need to handle a race condition here: it could be that
                        # the right item has been created by another thread. This is because
                        # the key could be in multiple rows of the data frame depending how the data loader works.
                        # We can also not assume that this can be resolved by making the key unique
                        # in the data source as it could be that a data set with the same key should
                        # go to multiple workspaces that then are folded into one by synonym resolution.
                        conflicting_node_id = response["results"][0].get("id")
                        conflicting_key = self._otcs_frontend.get_category_value_by_name(
                            node_id=conflicting_node_id,
                            category_name=cat_name,
                            attribute_name=att_name,
                            set_name=set_name,
                        )
                        if key == conflicting_key:
                            # We have a race condition as the two items don't really clash but are identical.
                            # Just skip creating the same item once more.
                            self.logger.warning(
                                "Detected a race condition in name clash handling! Skipping item creation!",
                            )
                            continue

                        # We add the suffix with the key which should be unique:
                        self.logger.warning(
                            "Item -> '%s' does already exist in workspace folder with ID -> %s and we need to handle the name clash and use name -> '%s'",
                            item_name,
                            parent_id,
                            item_name + " (" + key + ")",
                        )
                        item_name = item_name + " (" + key + ")"

                # If category data is in payload we substitute
                # the values with data from the current data row:
                if categories:
                    # Make sure the threads are not changing data structures that
                    # are shared between threads. categories is a list of dicts.
                    # list and dicts are "mutable" data structures in Python!
                    worker_categories = self.process_bulk_categories(
                        row=row,
                        index=index,
                        categories=categories,
                        replacements=replacements,
                    )
                    item_category_data = self.prepare_category_data(
                        categories_payload=worker_categories,
                        source_node_id=parent_id,
                    )
                # end if categories
                else:
                    item_category_data = {}

                response = self._otcs_frontend.create_item(
                    parent_id=int(parent_id),
                    item_type=int(item_type),
                    item_name=item_name,
                    item_description=description,
                    url=item_url,
                    original_id=int(item_original_node_id),
                    category_data=item_category_data,
                    external_create_date=external_create_date,
                    external_modify_date=external_modify_date,
                    show_error=False,
                )
                item_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if not item_id:
                    # We may have a race condition here. Double check the item does not yet exist:
                    response = self._otcs_frontend.get_node_by_parent_and_name(
                        parent_id=int(parent_id),
                        name=item_name,
                    )
                    item_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                if not item_id:
                    self.logger.error(
                        "Cannot create item -> '%s' in folder/workspace with ID -> %s!",
                        item_name,
                        parent_id,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Created item -> '%s' (%s), description -> '%s' in parent with ID -> %s.",
                    item_name,
                    item_id,
                    description,
                    parent_id,
                )
                result["create_counter"] += 1

            # end if not workspace_id and "create" in row_operations

            # If updates are requested we update the existing item with
            # a new item version and with fresh metadata from the payload.
            # Additionally we check the external modify date to support
            # incremental load for content that has really changed.
            # In addition we check that "delete" is not requested as otherwise it will
            # never go in elif "delete" ... below (and it does not make sense to update a item
            # that is deleted in the next step...)
            elif (
                item_id
                and "update" in row_operations
                and "delete" not in row_operations  # note the NOT !
                and OTCS.date_is_newer(
                    date_old=item_modify_date,
                    date_new=external_modify_date,
                )
            ):
                # get the specific update operations given in the payload
                # if not specified we do all 4 update operations (name, description, categories and version)
                update_operations = bulk_item.get(
                    "update_operations",
                    ["name", "description", "categories", "nickname", "url"],
                )

                # If category data is in payload we substitute
                # the values with data from the current data row.
                # This is only done if "categories" update is not
                # excluded from the update_operations:
                if categories and "categories" in update_operations:
                    # Make sure the threads are not changing data structures that
                    # are shared between threads. categories is a list of dicts.
                    # list and dicts are "mutable" data structures in Python!
                    worker_categories = self.process_bulk_categories(
                        row=row,
                        index=index,
                        categories=categories,
                        replacements=replacements,
                    )
                    # Transform the payload structure into the format
                    # the OTCS REST API requires:
                    item_category_data = self.prepare_category_data(
                        categories_payload=worker_categories,
                        source_node_id=item_id,
                        source_is_document=True,
                    )
                    if not item_category_data:
                        self.logger.error(
                            "Failed to prepare the updated category data for item -> '%s' (%s)!",
                            item_name,
                            str(item_id),
                        )
                        success = False
                        continue  # for index, row in partition.iterrows()
                # end if categories
                else:
                    item_category_data = {}

                self.logger.info(
                    "Update existing item -> '%s' (%s) with operations -> %s...",
                    item_old_name if item_old_name else item_name,
                    item_id,
                    str(update_operations),
                )
                response = self._otcs_frontend.update_item(
                    node_id=item_id,
                    parent_id=None,  # None = do not move item
                    item_name=(item_name if "name" in update_operations else None),
                    item_description=(description if "description" in update_operations else None),
                    category_data=(item_category_data if "categories" in update_operations else None),
                    url=(item_url if "url" in update_operations else None),
                    external_create_date=external_create_date,
                    external_modify_date=external_modify_date,
                )
                if not response:
                    self.logger.error(
                        "Failed to update metadata of existing item -> '%s' (%s) with metadata -> %s!",
                        (item_old_name if item_old_name else item_name),
                        item_id,
                        str(item_category_data),
                    )
                    success = False
                    continue
                self.logger.info(
                    "Updated existing item -> '%s' (%s, description -> '%s')",
                    item_name,
                    item_id,
                    description,
                )
                result["update_counter"] += 1
            # end if item_id and "update" in row_operations
            elif item_id and "delete" in row_operations:
                # We delete with immediate purging to keep recycle bin clean
                # and to not run into issues with nicknames used in deleted items:
                response = self._otcs_frontend.delete_node(
                    node_id=item_id,
                    purge=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to bulk delete existing item -> '%s' (%s)!",
                        (item_old_name if item_old_name else item_name),
                        item_id,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Successfully deleted existing item -> '%s' (%s).",
                    item_old_name if item_old_name else item_name,
                    item_id,
                )
                result["delete_counter"] += 1
                item_id = None
            # end elif item_id and "delete" in row_operations

            # this is the plain old "if it does exist we just skip it" case:
            elif item_id:
                self.logger.info(
                    "Skipped existing item -> '%s' (%s).",
                    item_old_name if item_old_name else item_name,
                    item_id,
                )
            # this is the case where we just want to operate on existing items (update or delete)
            # but they do not exist:
            elif not item_id and ("update" in row_operations or "delete" in row_operations):
                result["skipped_counter"] += 1
                self.logger.info(
                    "Skipped update/delete of non-existing item -> '%s'.",
                    item_old_name if item_old_name else item_name,
                )

            # The following code is executed for all operations
            # (create, recreate, update, delete):

            # Check if we want to set / update the nickname of the item.
            # Precondition is we have determined a nickname, we have the item ID
            # and the update of the nickname is either required (create, recreate)
            # or requested (update_operations include "nickname"):
            if (
                nickname
                and item_id
                and (
                    "create" in row_operations
                    or "recreate" in row_operations
                    or ("update" in row_operations and "nickname" in update_operations)
                )
            ):
                response = self._otcs_frontend.set_node_nickname(
                    node_id=item_id,
                    nickname=nickname,
                    show_error=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to assign nickname -> '%s' to item -> '%s'!",
                        nickname,
                        item_name,
                    )
            if item_id is not None:
                # Record the item name and ID to allow to read it from failure file
                # and speedup the process.
                result["items"][item_name] = item_id

        # end for workspaces

        # We want the success, failure and skip counter
        # to consider only complete data frame rows. In
        # case of bulk items we can potentially create
        # update or delete more than 1 item per row.
        # So we use the "success" variable to accumulate
        # this for all items per row:
        if not success:
            result["success"] = False
            result["failure_counter"] += 1
        elif item_name not in result["items"]:
            self.logger.info(
                "Bulk item -> '%s' was not added to any workspace.",
                item_name,
            )
            result["skipped_counter"] += 1
        else:
            result["success_counter"] += 1

    # end for index, row in partition.iterrows()

    self.logger.info("End working...")

    results.append(result)

process_bulk_workspace_relationships(section_name='bulkWorkspaceRelationships')

Process workspaces in payload and bulk create them in Content Server (multi-threaded).

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'bulkWorkspaceRelationships'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
23897
23898
23899
23900
23901
23902
23903
23904
23905
23906
23907
23908
23909
23910
23911
23912
23913
23914
23915
23916
23917
23918
23919
23920
23921
23922
23923
23924
23925
23926
23927
23928
23929
23930
23931
23932
23933
23934
23935
23936
23937
23938
23939
23940
23941
23942
23943
23944
23945
23946
23947
23948
23949
23950
23951
23952
23953
23954
23955
23956
23957
23958
23959
23960
23961
23962
23963
23964
23965
23966
23967
23968
23969
23970
23971
23972
23973
23974
23975
23976
23977
23978
23979
23980
23981
23982
23983
23984
23985
23986
23987
23988
23989
23990
23991
23992
23993
23994
23995
23996
23997
23998
23999
24000
24001
24002
24003
24004
24005
24006
24007
24008
24009
24010
24011
24012
24013
24014
24015
24016
24017
24018
24019
24020
24021
24022
24023
24024
24025
24026
24027
24028
24029
24030
24031
24032
24033
24034
24035
24036
24037
24038
24039
24040
24041
24042
24043
24044
24045
24046
24047
24048
24049
24050
24051
24052
24053
24054
24055
24056
24057
24058
24059
24060
24061
24062
24063
24064
24065
24066
24067
24068
24069
24070
24071
24072
24073
24074
24075
24076
24077
24078
24079
24080
24081
24082
24083
24084
24085
24086
24087
24088
24089
24090
24091
24092
24093
24094
24095
24096
24097
24098
24099
24100
24101
24102
24103
24104
24105
24106
24107
24108
24109
24110
24111
24112
24113
24114
24115
24116
24117
24118
24119
24120
24121
24122
24123
24124
24125
24126
24127
24128
24129
24130
24131
24132
24133
24134
24135
24136
24137
24138
24139
24140
24141
24142
24143
24144
24145
24146
24147
24148
24149
24150
24151
24152
24153
24154
24155
24156
24157
24158
24159
24160
24161
24162
24163
24164
24165
24166
24167
24168
24169
24170
24171
24172
24173
24174
24175
24176
24177
24178
24179
24180
24181
24182
24183
24184
24185
24186
24187
24188
24189
24190
24191
24192
24193
24194
24195
24196
24197
24198
24199
24200
24201
24202
24203
24204
24205
24206
24207
24208
24209
24210
24211
24212
24213
24214
24215
24216
24217
24218
24219
24220
24221
24222
24223
24224
24225
24226
24227
24228
24229
24230
24231
24232
24233
24234
24235
24236
24237
24238
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspace_relationships")
def process_bulk_workspace_relationships(
    self,
    section_name: str = "bulkWorkspaceRelationships",
) -> bool:
    """Process workspaces in payload and bulk create them in Content Server (multi-threaded).

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not self._bulk_workspace_relationships:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    # For efficient idem-potent operation we may want to see which workspaces
    # have already been processed before:
    if self.check_status_file(
        payload_section_name=section_name,
        payload_specific=True,
        prefix="failure_",
    ):
        # Read payload from where we left it last time
        self._bulk_workspace_relationships = self.get_status_file(
            payload_section_name=section_name,
            prefix="failure_",
        )
        if not self._bulk_workspace_relationships:
            self.logger.error(
                "Cannot load existing bulkWorkspaceRelationships failure file. Bailing out!",
            )
            return False

    success: bool = True

    for bulk_workspace_relationship in self._bulk_workspace_relationships:
        # Check if element has been disabled in payload (enabled = false).
        # In this case we skip the element:
        enabled = bulk_workspace_relationship.get("enabled", True)
        if not enabled:
            self.logger.info(
                "Payload for bulk workspace relationship is disabled. Skipping...",
            )
            continue

        # Read Pattern for "From" Workspace from payload:
        from_workspace = bulk_workspace_relationship.get("from_workspace", None)
        if not from_workspace:
            self.logger.error(
                "Bulk workspace relationship creation needs a workspace nickname in from_workspace! Skipping to next bulk workspace relationship...",
            )
            success = False
            continue
        from_sub_workspace = bulk_workspace_relationship.get(
            "from_sub_workspace_name",
            None,
        )

        # Read Pattern for "To" Workspace from payload:
        to_workspace = bulk_workspace_relationship.get("to_workspace", None)
        if not to_workspace:
            self.logger.error(
                "Bulk workspace relationship creation needs a workspace nickname in to_workspace! Skipping to next bulk workspace relationship...",
            )
            success = False
            continue
        to_sub_workspace = bulk_workspace_relationship.get(
            "to_sub_workspace_name",
            None,
        )

        # The payload element must have a "data_source" key:
        data_source_name = bulk_workspace_relationship.get("data_source", None)
        if not data_source_name:
            self.logger.error(
                "Missing data source name in bulk workspace relationship!",
            )
            success = False
            continue

        self._log_header_callback(
            text="Process bulk workspace relationships from -> '{}' to -> '{}'".format(
                from_sub_workspace if from_sub_workspace else from_workspace,
                to_sub_workspace if to_sub_workspace else to_workspace,
            ),
            char="-",
        )

        # Determine which operations should be done for this bulk workspace relationship:
        operations = bulk_workspace_relationship.get("operations", ["create"])
        if "delete" in operations and "create" in operations:
            self.logger.error(
                "Bulk workspace relationships can either have 'create' or 'delete' operation - not both. Use separate payloads for this purpose!",
            )
            success = False
            continue

        copy_data_source = bulk_workspace_relationship.get(
            "copy_data_source",
            False,
        )
        force_reload = bulk_workspace_relationship.get("force_reload", True)

        # Load and prepare the data source for the bulk processing:
        if copy_data_source:
            self.logger.info(
                "Take a copy of data source -> '%s' to avoid side-effects for repeative usage of the data source...",
                bulk_workspace_relationship["data_source"],
            )
            data = Data(
                self.process_bulk_datasource(
                    data_source_name=data_source_name,
                    force_reload=force_reload,
                ),
                logger=self.logger,
            )
        else:
            self.logger.info(
                "Use original data source -> '%s' and not do a copy.",
                bulk_workspace_relationship["data_source"],
            )
            # Load and prepare the data source for the bulk processing:
            data = self.process_bulk_datasource(
                data_source_name=data_source_name,
                force_reload=force_reload,
            )
        if data is None:  # important to use "is None" here!
            self.logger.error(
                "Failed to load data source -> '%s' for bulk workspace relationships from -> '%s' to -> '%s'",
                data_source_name,
                from_sub_workspace if from_sub_workspace else from_workspace,
                to_sub_workspace if to_sub_workspace else to_workspace,
            )
            success = False
            continue
        if data.get_data_frame() is None:  # important to use "is None" here!
            self.logger.warning(
                "Data source for bulk workspace relationships from -> '%s' to -> '%s' is empty!",
                from_sub_workspace if from_sub_workspace else from_workspace,
                to_sub_workspace if to_sub_workspace else to_workspace,
            )
            continue

        # Check if fields with list substructures should be exploded.
        # We may want to do this outside the bulkDatasource to only
        # explode for bulkDocuments and not for bulkWorkspaces or
        # bulkWorkspaceRelationships:
        explosions = bulk_workspace_relationship.get("explosions", [])
        for explosion in explosions:
            # The explode field parameter can be a string or a list.
            # Exploding multiple fields at once avoids combinatorial explosions -
            # this is VERY different from exploding columns one after the other!
            if "explode_field" not in explosion and "explode_fields" not in explosion:
                self.logger.error("Missing explosion field(s)!")
                continue
            # we want to be backwards compatible...
            explode_fields = (
                explosion["explode_field"] if "explode_field" in explosion else explosion["explode_fields"]
            )
            flatten_fields = explosion.get("flatten_fields", [])
            split_string_to_list = explosion.get("split_string_to_list", False)
            list_splitter = explosion.get(
                "list_splitter",
                ",",
            )  # don't have None as default!
            self.logger.info(
                "Starting explosion of bulk relationships by field(s) -> %s (type -> %s). Size of data frame before explosion -> %s.",
                str(explode_fields),
                type(explode_fields),
                str(len(data)),
            )
            data.explode_and_flatten(
                explode_fields=explode_fields,
                flatten_fields=flatten_fields,
                make_unique=False,
                split_string_to_list=split_string_to_list,
                separator=list_splitter,
                reset_index=True,
            )
            self.logger.info(
                "Size of data frame after explosion -> %s.",
                str(len(data)),
            )
        # end for explosion in explosions

        # Keep only selected rows if filters are specified in bulkWorkspaceRelationship.
        # We have this _after_ "explosions" to allow access to subfields as well.
        # We have this _before_ "sorting" and "deduplication" as we may keep the wrong
        # rows otherwise (unique / deduplication always keeps the first matching row).
        # We may want to do this filtering outside the bulkDatasource to only
        # filter the data frame for bulkDocuments and not for bulkWorkspaces or
        # bulkWorkspaceRelationships:
        filters = bulk_workspace_relationship.get("filters", [])
        if filters:
            data.filter(conditions=filters)

        # Sort the data frame if "sort" specified in payload. We may want to do this to have a
        # higher chance that rows with common values that may collapse into
        # one name are put into the same partition. This can avoid race conditions
        # between different Python threads.
        sort_fields = bulk_workspace_relationship.get("sort", [])
        if sort_fields:
            self.logger.info(
                "Start sorting of bulk workspace relationships data frame based on fields (columns) -> %s...",
                str(sort_fields),
            )
            data.sort(sort_fields=sort_fields, inplace=True)
            self.logger.info(
                "Sorting of bulk workspace relationships data frame based on fields (columns) -> %s completed.",
                str(sort_fields),
            )

        # Check if duplicate rows for given fields should be removed. It is
        # important to do this after sorting as Pandas always keep the first occurance,
        # so ordering plays an important role in deduplication!
        unique_fields = bulk_workspace_relationship.get("unique", [])
        if unique_fields:
            self.logger.info(
                "Starting deduplication of data frame for bulk workspace relationships with unique fields -> %s. Size of data frame before deduplication -> %s",
                str(unique_fields),
                str(len(data)),
            )
            data.deduplicate(unique_fields=unique_fields, inplace=True)
            self.logger.info(
                "Size of data frame after deduplication -> %s.",
                str(len(data)),
            )

        self.logger.info(
            "Bulk create workspace relationships (from workspace -> '%s' to workspace -> '%s'). Operations -> %s.",
            from_sub_workspace if from_sub_workspace else from_workspace,
            to_sub_workspace if to_sub_workspace else to_workspace,
            str(operations),
        )

        # See if bulkWorkspace definition has a specific thread number
        # otherwise it is read from a global environment variable
        bulk_thread_number = int(
            bulk_workspace_relationship.get("thread_number", BULK_THREAD_NUMBER),
        )

        partitions = data.partitionate(bulk_thread_number)

        # Create a list to hold the threads
        threads = []
        results = []

        # Create and start a thread for each partition
        for index, partition in enumerate(partitions, start=1):
            # start a thread executing the process_bulk_workspace_relationships_worker() method below:
            thread = threading.Thread(
                name=f"{section_name}_{index:02}",
                target=self.thread_wrapper,
                args=(
                    self.process_bulk_workspace_relationships_worker,
                    bulk_workspace_relationship,
                    partition,
                    operations,
                    results,
                ),
            )
            self.logger.info("Starting thread -> '%s'...", str(thread.name))
            thread.start()
            threads.append(thread)
            # Avoid that all threads start at the exact same time with
            # potentially expired cookies that cases race conditions:
            time.sleep(1)
        # end for index, partition in enumerate(partitions, start=1)

        # Wait for all threads to complete
        for thread in threads:
            self.logger.info(
                "Waiting for thread -> '%s' to complete...",
                str(thread.name),
            )
            thread.join()
            self.logger.info("Thread -> '%s' has completed.", str(thread.name))

        if "relationships" not in bulk_workspace_relationship:
            bulk_workspace_relationship["relationships"] = {}

        # Print statistics for each thread. In addition,
        # check if all threads have completed without error / failure.
        # If there's a single failure in on of the thread results we
        # set 'success' variable to False.
        results.sort(key=lambda x: x["thread_name"])
        for result in results:
            if not result["success"]:
                self.logger.info(
                    "Thread -> '%s' completed with %s failed, %s skipped, and %s created workspace relationships.",
                    str(result["thread_name"]),
                    str(result["failure_counter"]),
                    str(result["skipped_counter"]),
                    str(result["success_counter"]),
                )
                success = False
            else:
                self.logger.info(
                    "Thread -> '%s' completed successful with %s skipped, and %s created workspace relationships.",
                    str(result["thread_name"]),
                    str(result["skipped_counter"]),
                    str(result["success_counter"]),
                )
            # Record all generated workspaces relationships. This should
            # allow us to restart in case of failures and avoid trying to
            # create workspaces that have been created before.
            bulk_workspace_relationship["relationships"].update(
                result["relationships"],
            )
        self._log_header_callback(
            text="Completed processing of bulk workspace relationships from -> '{}' to -> '{}'".format(
                from_sub_workspace if from_sub_workspace else from_workspace,
                to_sub_workspace if to_sub_workspace else to_workspace,
            ),
            char="-",
        )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._bulk_workspace_relationships,
    )

    return success

process_bulk_workspace_relationships_worker(bulk_workspace_relationship, partition, operations=None, results=None)

Thread worker to create workspaces relationships in bulk.

Each worker thread gets a partition of the rows that include the data required for the workspace relationship creation.

Parameters:

Name Type Description Default
bulk_workspace_relationship dict

The payload of the bulkWorkspaceRelationship.

required
partition DataFrame

The data partition with rows to process.

required
from_workspace str

The string pattern for nickname of workspace (from).

required
to_workspace str

The string pattern for nickname of workspace (to).

required
operations list

Defines which operations should be applyed on workspace relationships. Possible values are "create", "delete", "recreate".

None
results list

A mutable list of thread results.

None
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
24449
24450
24451
24452
24453
24454
24455
24456
24457
24458
24459
24460
24461
24462
24463
24464
24465
24466
24467
24468
24469
24470
24471
24472
24473
24474
24475
24476
24477
24478
24479
24480
24481
24482
24483
24484
24485
24486
24487
24488
24489
24490
24491
24492
24493
24494
24495
24496
24497
24498
24499
24500
24501
24502
24503
24504
24505
24506
24507
24508
24509
24510
24511
24512
24513
24514
24515
24516
24517
24518
24519
24520
24521
24522
24523
24524
24525
24526
24527
24528
24529
24530
24531
24532
24533
24534
24535
24536
24537
24538
24539
24540
24541
24542
24543
24544
24545
24546
24547
24548
24549
24550
24551
24552
24553
24554
24555
24556
24557
24558
24559
24560
24561
24562
24563
24564
24565
24566
24567
24568
24569
24570
24571
24572
24573
24574
24575
24576
24577
24578
24579
24580
24581
24582
24583
24584
24585
24586
24587
24588
24589
24590
24591
24592
24593
24594
24595
24596
24597
24598
24599
24600
24601
24602
24603
24604
24605
24606
24607
24608
24609
24610
24611
24612
24613
24614
24615
24616
24617
24618
24619
24620
24621
24622
24623
24624
24625
24626
24627
24628
24629
24630
24631
24632
24633
24634
24635
24636
24637
24638
24639
24640
24641
24642
24643
24644
24645
24646
24647
24648
24649
24650
24651
24652
24653
24654
24655
24656
24657
24658
24659
24660
24661
24662
24663
24664
24665
24666
24667
24668
24669
24670
24671
24672
24673
24674
24675
24676
24677
24678
24679
24680
24681
24682
24683
24684
24685
24686
24687
24688
24689
24690
24691
24692
24693
24694
24695
24696
24697
24698
24699
24700
24701
24702
24703
24704
24705
24706
24707
24708
24709
24710
24711
24712
24713
24714
24715
24716
24717
24718
24719
24720
24721
24722
24723
24724
24725
24726
24727
24728
24729
24730
24731
24732
24733
24734
24735
24736
24737
24738
24739
24740
24741
24742
24743
24744
24745
24746
24747
24748
24749
24750
24751
24752
24753
24754
24755
24756
24757
24758
24759
24760
24761
24762
24763
24764
24765
24766
24767
24768
24769
24770
24771
24772
24773
24774
24775
24776
24777
24778
24779
24780
24781
24782
24783
24784
24785
24786
@tracer.start_as_current_span(
    attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspace_relationships_worker"
)
def process_bulk_workspace_relationships_worker(
    self,
    bulk_workspace_relationship: dict,
    partition: pd.DataFrame,
    operations: list | None = None,
    results: list | None = None,
) -> None:
    """Thread worker to create workspaces relationships in bulk.

    Each worker thread gets a partition of the rows that include
    the data required for the workspace relationship creation.

    Args:
        bulk_workspace_relationship (dict):
            The payload of the bulkWorkspaceRelationship.
        partition (pd.DataFrame):
            The data partition with rows to process.
        from_workspace (str):
            The string pattern for nickname of workspace (from).
        to_workspace (str):
            The string pattern for nickname of workspace (to).
        operations (list, optional):
            Defines which operations should be applyed on workspace relationships.
            Possible values are "create", "delete", "recreate".
        results (list, optional):
            A mutable list of thread results.

    """

    thread_id = threading.get_ident()
    thread_name = threading.current_thread().name

    self.logger.info(
        "Start working on data frame partition of size -> %s...",
        str(len(partition)),
    )

    # Avoid linter warnings - so make parameter default None while we
    # actually want ["create"] to be the default:
    if operations is None:
        operations = ["create"]

    result = {}
    result["thread_id"] = thread_id
    result["thread_name"] = thread_name
    result["success_counter"] = 0
    result["failure_counter"] = 0
    result["skipped_counter"] = 0
    result["delete_counter"] = 0
    result["relationships"] = {}
    result["success"] = True

    # Check if workspace relationships have been processed before, e.i. testing
    # if a "relationships" key exists and if it is pointing to a non-empty list:
    if bulk_workspace_relationship.get("relationships") and "delete" not in operations:
        existing_workspace_relationships = bulk_workspace_relationship["relationships"]
        self.logger.info(
            "Found %s already processed workspace relationships. Try to complete the job...",
            str(len(existing_workspace_relationships)),
        )
    else:
        existing_workspace_relationships = {}

    # Get dictionary of replacements for bulk workspace relationships
    # this we will be used of all places data is read from the
    # current data frame row. Each dictionary item has the field name as the
    # dictionary key and a list of regular expressions as dictionary value
    replacements = bulk_workspace_relationship.get("replacements")

    # Check if all data conditions to create the workspace relationship are met
    conditions = bulk_workspace_relationship.get("conditions")

    # Type of the relationship - can either be child or parent.
    relationship_type = bulk_workspace_relationship.get("type", "child")

    # Whether or not we want to show an error if the lookup of related
    # workspaces fails.
    show_lookup_error_from = bulk_workspace_relationship.get(
        "from_workspace_lookup_error",
        True,
    )
    show_lookup_error_to = bulk_workspace_relationship.get(
        "to_workspace_lookup_error",
        True,
    )

    # Nicknames are very limited in terms of allowed characters.
    # For nicknames we need additional regexp as we need to
    # replace all non-alphanumeric, non-space characters with ""
    # We also preserve hyphens in the first step to replace
    # them below with underscores. This is important to avoid
    # that different spellings of names produce different nicknames.
    # We want spellings with spaces match spellings with hyphens.
    # For this the workspace names have a regexp "-| " in the payload.
    nickname_additional_regex_list = [r"[^\w\s-]"]

    total_rows = len(partition)

    # Process all datasets in the partion that was given to the thread:
    for index, row in partition.iterrows():
        # Calculate percentage of completion
        percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

        self.logger.info(
            "Processing data row -> %s for bulk workspace relationship creation (%.2f %% complete)...",
            str(index),
            percent_complete,
        )

        # Create a copy of the mutable operations list as we may
        # want to modify it per row:
        row_operations = list(operations)

        # check if we have any exlusions that apply here:
        if conditions:
            evaluated_condition = self.evaluate_conditions(
                conditions=conditions,
                row=row,
                replacements=replacements,
            )
            if not evaluated_condition:
                self.logger.info(
                    "Condition for row -> %s not met. Skipping row for workspace relationship...",
                    str(index),
                )
                result["skipped_counter"] += 1
                continue

        (from_workspace_id, from_workspace_name) = self.get_bulk_workspace_relationship_endpoint(
            bulk_workspace_relationship=bulk_workspace_relationship,
            row=row,
            index=index,
            endpoint="from",
            replacements=replacements,
            nickname_additional_regex_list=nickname_additional_regex_list,
            show_error=show_lookup_error_from,
        )

        # Check we have "from" endpoint:
        if not from_workspace_id:
            self.logger.warning(
                "%s%s",
                (
                    "Failed to retrieve 'from' endpoint for bulk workspace relationship! "
                    if not from_workspace_id and not from_workspace_name
                    else ""
                ),
                (
                    "Failed to retrieve 'from' endpoint (workspace name -> {}) for bulk workspace relationship! ".format(
                        from_workspace_name,
                    )
                    if not from_workspace_id and from_workspace_name
                    else ""
                ),
            )
            result["skipped_counter"] += 1
            continue

        # If relationships should be deleted we do it only if the
        # relationships for this workspace ID have not been deleted before.
        # This is checked by a dictionary result["relationships"].
        # If a "delete" operation is requested there cannot be a "create"
        # operation in the same bulk workspace relationships payload!
        # (this would create too many interferences between threads)
        # Because of this we can continue with the next row then:
        if "delete" in row_operations and from_workspace_id not in result["relationships"]:
            # Get the target (to) workspace type if specified:
            to_workspace_type = bulk_workspace_relationship.get("to_workspace_type")
            if not to_workspace_type:
                self.logger.error(
                    "Cannot delete workspace relationships! Need the target (to) workspace type!",
                )
                result["success"] = False
                result["failure_counter"] += 1
                continue
            result = self._otcs_frontend.delete_workspace_relationships(
                workspace_id=from_workspace_id,
                related_workspace_type_id=to_workspace_type,
            )
            if not result:
                result["success"] = False
                result["failure_counter"] += 1
            else:
                result["delete_counter"] += 1
                # Mark that we have deleted the relationships originating from
                # the current workspace ID (from) pointing to workspace of
                # the given type.
                result["relationships"][from_workspace_id] = [to_workspace_type]
            # We are continuing here as we cannot do delete and create
            # operations in one thread. This needs separate bulkRelationships
            # payloads!
            continue

        (to_workspace_id, to_workspace_name) = self.get_bulk_workspace_relationship_endpoint(
            bulk_workspace_relationship=bulk_workspace_relationship,
            row=row,
            index=index,
            endpoint="to",
            replacements=replacements,
            nickname_additional_regex_list=nickname_additional_regex_list,
            show_error=show_lookup_error_to,
        )

        # Check we have "to" endpoint:
        if not from_workspace_id or not to_workspace_id:
            self.logger.warning(
                "%s%s",
                (
                    "Failed to retrieve 'to' endpoint for bulk workspace relationship!"
                    if not to_workspace_id and not to_workspace_name
                    else ""
                ),
                (
                    "Failed to retrieve 'to' endpoint (workspace name -> {}) for bulk workspace relationship!".format(
                        to_workspace_name,
                    )
                    if not to_workspace_id and to_workspace_name
                    else ""
                ),
            )
            result["skipped_counter"] += 1
            continue

        # Check if workspace relationship has been created before (either in this run
        # or in a former run of the customizer):
        if (  # processed in former run?
            from_workspace_id in existing_workspace_relationships
            and to_workspace_id in existing_workspace_relationships[from_workspace_id]
        ) or (  # processed in current run?
            from_workspace_id in result["relationships"]
            and to_workspace_id in result["relationships"][from_workspace_id]
        ):
            self.logger.info(
                "Workspace relationship between workspace -> '%s' (%s) and related workspace -> '%s' (%s) has successfully been processed before. Skipping...",
                from_workspace_name,
                str(from_workspace_id),
                to_workspace_name,
                str(to_workspace_id),
            )
            result["skipped_counter"] += 1
            continue

        # Check if workspace relationship does already exist in Extended ECM
        # (this is an additional safety measure to avoid errors):
        response = self._otcs_frontend.get_workspace_relationships(
            workspace_id=from_workspace_id,
            relationship_type=relationship_type,
            related_workspace_name=to_workspace_name,
        )
        current_workspace_relationships = self._otcs_frontend.exist_result_item(
            response=response,
            key="id",
            value=to_workspace_id,
        )
        if current_workspace_relationships:
            self.logger.info(
                "Workspace relationship between workspace -> '%s' (%s) and related workspace -> '%s' (%s) does already exist. Skipping...",
                from_workspace_name,
                str(from_workspace_id),
                to_workspace_name,
                str(to_workspace_id),
            )
            result["skipped_counter"] += 1
            continue

        self.logger.info(
            "Bulk create workspace relationship '%s' (%s) -> '%s' (%s)...",
            from_workspace_name,
            str(from_workspace_id),
            to_workspace_name,
            str(to_workspace_id),
        )

        response = self._otcs_frontend.create_workspace_relationship(
            workspace_id=from_workspace_id,
            related_workspace_id=to_workspace_id,
            relationship_type=relationship_type,
            show_error=False,
        )

        if response is None:
            # Potential race condition: see if the workspace-2-workspace relationship has been created by a concurrent thread.
            # So we better check if the relationship is there even if the create_workspace_relationship() call delivered
            # a 'None' response:
            response = self._otcs_frontend.get_workspace_relationships(
                workspace_id=from_workspace_id,
                relationship_type=relationship_type,
                related_workspace_name=to_workspace_name,
            )
            current_workspace_relationships = self._otcs_frontend.exist_result_item(
                response=response,
                key="id",
                value=to_workspace_id,
            )
            if current_workspace_relationships:
                self.logger.info(
                    "Workspace relationship between workspace -> '%s' (%s) and related workspace -> '%s' (%s) has been created concurrently. Skipping...",
                    from_workspace_name,
                    str(from_workspace_id),
                    to_workspace_name,
                    str(to_workspace_id),
                )
                result["skipped_counter"] += 1
                continue
            self.logger.error(
                "Failed to bulk create workspace relationship (%s) from -> '%s' (%s) to -> '%s' (%s)!",
                relationship_type,
                from_workspace_name,
                str(from_workspace_id),
                to_workspace_name,
                str(to_workspace_id),
            )
            result["success"] = False
            result["failure_counter"] += 1
        else:
            self.logger.info(
                "Successfully created bulk workspace relationship (%s) from -> '%s' (%s) to -> '%s' (%s).",
                relationship_type,
                from_workspace_name,
                str(from_workspace_id),
                to_workspace_name,
                str(to_workspace_id),
            )
            result["success_counter"] += 1
            # Record the workspace name and ID to allow to read it from failure file
            # and speedup the process.
            if from_workspace_id not in result["relationships"]:
                # Initialize the "to" list:
                result["relationships"][from_workspace_id] = [to_workspace_id]
            else:
                result["relationships"][from_workspace_id].append(to_workspace_id)

    self.logger.info("End working...")

    results.append(result)

process_bulk_workspaces(section_name='bulkWorkspaces')

Process workspaces in payload and bulk create them in Content Server (multi-threaded).

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'bulkWorkspaces'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspaces")
def process_bulk_workspaces(self, section_name: str = "bulkWorkspaces") -> bool:
    """Process workspaces in payload and bulk create them in Content Server (multi-threaded).

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not self._bulk_workspaces:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    # For efficient idem-potent operation we may want to see which workspaces
    # have already been processed before:
    if self.check_status_file(
        payload_section_name=section_name,
        payload_specific=True,
        prefix="failure_",
    ):
        # Read payload from where we left it last time
        self._bulk_workspaces = self.get_status_file(
            payload_section_name=section_name,
            prefix="failure_",
        )
        if not self._bulk_workspaces:
            self.logger.error(
                "Cannot load existing bulkWorkspaces failure file. Bailing out!",
            )
            return False

    success: bool = True

    for bulk_workspace in self._bulk_workspaces:
        # Check if element has been disabled in payload (enabled = false).
        # In this case we skip the element:
        enabled = bulk_workspace.get("enabled", True)
        if not enabled:
            self.logger.info("Payload for bulk workspace is disabled. Skipping...")
            continue

        # Read workspace type name from payload:
        type_name = bulk_workspace.get("type_name", None)
        if not type_name:
            self.logger.error(
                "Bulk workspace needs a workspace type name! Skipping to next workspace...",
            )
            success = False
            continue

        # The payload element must have a "data_source" key:
        data_source_name = bulk_workspace.get("data_source", None)
        if not data_source_name:
            self.logger.error("Missing data source name in bulk workspace!")
            success = False
            continue

        self._log_header_callback(
            text="Process bulk workspaces for -> '{}' using data source -> '{}'".format(
                type_name,
                data_source_name,
            ),
            char="-",
        )

        copy_data_source = bulk_workspace.get("copy_data_source", False)
        force_reload = bulk_workspace.get("force_reload", True)

        # Load and prepare the data source for the bulk processing:
        if copy_data_source:
            self.logger.info(
                "Take a copy of data source -> '%s' to avoid side-effects for repeative usage of the data source...",
                data_source_name,
            )
            data = Data(
                self.process_bulk_datasource(
                    data_source_name=data_source_name,
                    force_reload=force_reload,
                ),
                logger=self.logger,
            )
        else:
            self.logger.info(
                "Use original data source -> '%s' and not do a copy.",
                bulk_workspace["data_source"],
            )
            data = self.process_bulk_datasource(
                data_source_name=data_source_name,
                force_reload=force_reload,
            )
        if data is None:  # important to use "is None" here!
            self.logger.error(
                "Failed to load data source -> '%s' for bulk workspace type -> '%s'!",
                data_source_name,
                type_name,
            )
            success = False
            continue
        if data.get_data_frame() is None:  # important to use "is None" here!
            self.logger.error(
                "Data source for bulk workspace type -> '%s' is empty!",
                type_name,
            )
            continue

        # Check if fields with list substructures should be exploded.
        # We may want to do this outside the bulkDatasource to only
        # explode for bulkDocuments and not for bulkWorkspaces or
        # bulkWorkspaceRelationships:
        explosions = bulk_workspace.get("explosions", [])
        for explosion in explosions:
            # explode field can be a string or a list
            # exploding multiple fields at once avoids
            # combinatorial explosions - this is VERY
            # different from exploding columns one after the other!
            if "explode_field" not in explosion and "explode_fields" not in explosion:
                self.logger.error("Missing explosion field(s)!")
                continue
            # we want to be backwards compatible...
            explode_fields = (
                explosion["explode_field"] if "explode_field" in explosion else explosion["explode_fields"]
            )
            flatten_fields = explosion.get("flatten_fields", [])
            split_string_to_list = explosion.get("split_string_to_list", False)
            list_splitter = explosion.get(
                "list_splitter",
                ",",
            )  # don't have None as default!
            self.logger.info(
                "Starting explosion of bulk workspaces by field(s) -> %s (type -> %s). Size of data frame before explosion -> %s.",
                explode_fields,
                type(explode_fields),
                str(len(data)),
            )
            data.explode_and_flatten(
                explode_fields=explode_fields,
                flatten_fields=flatten_fields,
                make_unique=False,
                split_string_to_list=split_string_to_list,
                separator=list_splitter,
                reset_index=True,
            )
            self.logger.info(
                "Size of data frame after explosion -> %s.",
                str(len(data)),
            )

        # Keep only selected rows if filters are specified in bulkWorkspaces.
        # We have this _after_ "explosions" to allow access to subfields as well.
        # We have this _before_ "sorting" and "deduplication" as may keep the wrong
        # rows otherwise (unique / deduplication always keeps the first matching row).
        # We may want to do this outside the bulkDatasource to only
        # filter for bulkDocuments and not for bulkWorkspaces or
        # bulkWorkspaceRelationships:
        filters = bulk_workspace.get("filters", [])
        if filters:
            data.filter(conditions=filters)

        # Sort the data frame if "sort" specified in payload. We may want to do this to have a
        # higher chance that rows with workspace names that may collapse into
        # one name are put into the same partition. This can avoid race conditions
        # between different Python threads.
        sort_fields = bulk_workspace.get("sort", [])
        if sort_fields:
            self.logger.info(
                "Start sorting of data frame for workspace type -> '%s' based on fields (columns) -> %s...",
                type_name,
                str(sort_fields),
            )
            data.sort(sort_fields=sort_fields, inplace=True)
            self.logger.info(
                "Sorting of data frame for workspace type -> '%s' based on fields (columns) -> %s completed.",
                type_name,
                str(sort_fields),
            )

        # Check if duplicate rows for given fields should be removed. It is
        # important to do this after sorting as Pandas always keep the first occurance,
        # so ordering plays an important role in deduplication!
        unique_fields = bulk_workspace.get("unique", [])
        if unique_fields:
            self.logger.info(
                "Starting deduplication of data frame for workspace type -> '%s' with unique fields -> %s. Size of data frame before deduplication -> %s.",
                type_name,
                str(unique_fields),
                str(len(data)),
            )
            data.deduplicate(unique_fields=unique_fields, inplace=True)
            self.logger.info(
                "Size of data frame after deduplication -> %s.",
                str(len(data)),
            )

        # Read name field from payload:
        workspace_name_field = bulk_workspace.get("name", None)
        if not workspace_name_field:
            self.logger.error(
                "Bulk workspace needs a name field! Skipping to next workspace...",
            )
            success = False
            continue

        # Read optional description field from payload:
        workspace_description_field = bulk_workspace.get("description", None)

        # Find the workspace type with the name given in the payload:
        workspace_type = next(
            (item for item in self._workspace_types if item["name"] == type_name),
            None,
        )
        if workspace_type is None:
            self.logger.error(
                "Workspace type -> '%s' not found. Skipping to next bulk workspace...",
                type_name,
            )
            success = False
            continue
        if workspace_type["templates"] == []:
            self.logger.error(
                "Workspace type -> '%s' does not have templates. Skipping to next bulk workspace...",
                type_name,
            )
            success = False
            continue

        # check if the template to be used is specified in the payload:
        if "template_name" in bulk_workspace:
            template_name_field = bulk_workspace["template_name"]
            workspace_template = next(
                (item for item in workspace_type["templates"] if item["name"] == template_name_field),
                None,
            )
            if workspace_template:  # does this template exist?
                self.logger.info(
                    "Workspace template -> '%s' has been specified in payload and it does exist.",
                    template_name_field,
                )
            elif "{" in template_name_field and "}" in template_name_field:
                self.logger.info(
                    "Workspace template -> '%s' has been specified in payload and contains placeholders, validation cannot be performed.",
                    template_name_field,
                )
            else:
                self.logger.error(
                    "Workspace template -> '%s' has been specified in payload but it doesn't exist!",
                    template_name_field,
                )
                self.logger.error(
                    "Workspace type -> '%s' has only these templates -> %s.",
                    type_name,
                    workspace_type["templates"],
                )
                success = False
                continue
        # template to be used is NOT specified in the payload - then we just take the first one:
        else:
            workspace_template = workspace_type["templates"][0]
            template_name_field = None
            self.logger.info(
                "Workspace template has not been specified in payload - taking the first one (%s)...",
                workspace_template,
            )

        # Read the optional categories payload:
        categories = bulk_workspace.get("categories", None)
        if not categories:
            self.logger.info(
                "Bulk workspace payload has no category data! Will leave category attributes empty...",
            )

        # Should existing workspaces be updated? No is the default.
        operations = bulk_workspace.get("operations", ["create"])

        self.logger.info(
            "Bulk create workspaces (name field -> %s, type -> '%s') from workspace template -> '%s'. Operations -> %s.",
            workspace_name_field,
            type_name,
            template_name_field,
            str(operations),
        )

        # See if bulkWorkspace definition has a specific thread number
        # otherwise it is read from a global environment variable
        bulk_thread_number = int(
            bulk_workspace.get("thread_number", BULK_THREAD_NUMBER),
        )

        partitions = data.partitionate(bulk_thread_number)

        # Create a list to hold the threads
        threads = []
        results = []

        # Create and start a thread for each partition
        for index, partition in enumerate(partitions, start=1):
            # start a thread executing the process_bulk_workspaces_worker() method below:
            thread = threading.Thread(
                name=f"{section_name}_{index:02}",
                target=self.thread_wrapper,
                args=(
                    self.process_bulk_workspaces_worker,
                    bulk_workspace,
                    partition,
                    workspace_type,
                    template_name_field,
                    workspace_name_field,
                    workspace_description_field,
                    categories,
                    operations,
                    results,
                ),
            )
            self.logger.info("Starting thread -> '%s'...", str(thread.name))
            thread.start()
            threads.append(thread)
            # Avoid that all threads start at the exact same time with
            # potentially expired cookies that cases race conditions:
            time.sleep(1)
        # end for index, partition in enumerate(partitions, start=1)

        # Wait for all threads to complete
        for thread in threads:
            self.logger.info(
                "Waiting for thread -> '%s' to complete...",
                str(thread.name),
            )
            thread.join()
            self.logger.info("Thread -> '%s' has completed.", str(thread.name))

        if "workspaces" not in bulk_workspace:
            bulk_workspace["workspaces"] = {}

        # Print statistics for each thread. In addition,
        # check if all threads have completed without error / failure.
        # If there's a single failure in on of the thread results we
        # set 'success' variable to False.
        results.sort(key=lambda x: x["thread_name"])
        for result in results:
            self.logger.info(
                "Thread -> '%s' completed with overall status '%s'.",
                str(result["thread_name"]),
                "successful" if result["success"] else "failed",
            )
            self.logger.info(
                "Thread -> '%s' processed %s data rows with %s successful, %s failed, and %s skipped.",
                str(result["thread_name"]),
                str(
                    result["success_counter"] + result["failure_counter"] + result["skipped_counter"],
                ),
                str(result["success_counter"]),
                str(result["failure_counter"]),
                str(result["skipped_counter"]),
            )
            self.logger.info(
                "Thread -> '%s' created %s workspaces, updated %s workspaces, and deleted %s workspaces.",
                str(result["thread_name"]),
                str(result["create_counter"]),
                str(result["update_counter"]),
                str(result["delete_counter"]),
            )

            if not result["success"]:
                success = False
            # Record all generated workspaces. This should allow us
            # to restart in case of failures and avoid trying to
            # create workspaces that have been created before.
            bulk_workspace["workspaces"].update(result["workspaces"])
        self._log_header_callback(
            text="Completed processing of bulk workspaces for -> '{}' using data source -> '{}'".format(
                type_name,
                data_source_name,
            ),
            char="-",
        )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._bulk_workspaces,
    )

    return success

process_bulk_workspaces_lookup(workspace_nickname=None, workspace_name=None, workspace_type=None, parent_id=None, data_source_name=None, show_error=True)

Look the workspace name and ID.

Use a combination of workspace name, workspace type, and workspace data source (using synonyms) to do so.

Parameters:

Name Type Description Default
workspace_nickname str

The nickname of the workspace.

None
workspace_name str

The name as input for lookup. This must be either the name of an existing workspace instance or one of the synonyms of the workspace name.

None
workspace_type str

The Name of the workspace type.

None
parent_id int

ID of parent workspace (if it is a sub-workspace) or parent folder.

None
data_source_name str

The workspace data source name.

None
show_error bool

Whether or not to log an error if it occurs. If False, just log a warning.

True

Returns:

Type Description
tuple[int | None, str | None]

tuple[int | None, str | None]: The workspace ID and the looked up workspace name.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspaces_lookup")
def process_bulk_workspaces_lookup(
    self,
    workspace_nickname: str | None = None,
    workspace_name: str | None = None,
    workspace_type: str | None = None,
    parent_id: int | None = None,
    data_source_name: str | None = None,
    show_error: bool = True,
) -> tuple[int | None, str | None]:
    """Look the workspace name and ID.

    Use a combination of workspace name, workspace type, and workspace
    data source (using synonyms) to do so.

    Args:
        workspace_nickname (str, optional):
            The nickname of the workspace.
        workspace_name (str, optional):
            The name as input for lookup. This must be either the name of
            an existing workspace instance or one of the synonyms of the workspace name.
        workspace_type (str, optional):
            The Name of the workspace type.
        parent_id (int, optional):
            ID of parent workspace (if it is a sub-workspace) or parent folder.
        data_source_name (str, optional):
            The workspace data source name.
        show_error (bool, optional):
            Whether or not to log an error if it occurs. If False, just log a warning.

    Returns:
        tuple[int | None, str | None]:
            The workspace ID and the looked up workspace name.

    """

    # First we try to find the workspace by a nickname (if provided)
    if workspace_nickname:
        # Nicknames for sure are not allowed to include spaces and dashes:
        workspace_nickname = workspace_nickname.replace(" ", "_")
        workspace_nickname = workspace_nickname.replace("-", "_")
        workspace_nickname = workspace_nickname.replace("___", "_")
        workspace_nickname = workspace_nickname.lower()

        response = self._otcs_frontend.get_node_from_nickname(
            nickname=workspace_nickname,
            show_error=False,
        )
        workspace_id = self._otcs_frontend.get_result_value(
            response=response,
            key="id",
        )
        if workspace_id:
            # If we don't have the name yet, we can get it from the response above:
            if not workspace_name:
                workspace_name = self._otcs_frontend.get_result_value(
                    response=response,
                    key="name",
                )
            return (workspace_id, workspace_name)
        # DON'T RETURN FAILURE AT THIS POINT!

    # Our 2nd try is to find the workspace by a workspace name and workspace type combination:
    if workspace_name:
        workspace_name = workspace_name.strip()
    else:
        self.logger.error(
            "No workspace name specified. Cannot find the workspace by nickname%s, nor by type and name, nor by parent ID and name, nor by synonym.",
            " -> {}".format(workspace_nickname) if workspace_nickname else "",
        )
        return (None, None)

    # If we have workspace name and workspace parent ID then we try this:
    if workspace_name and parent_id is not None:
        response = self._otcs_frontend.get_node_by_parent_and_name(
            parent_id=parent_id,
            name=workspace_name,
        )
        workspace_id = self._otcs_frontend.get_result_value(
            response=response,
            key="id",
        )
        if workspace_id:
            return (workspace_id, workspace_name)

    # If we have workspace name and workspace type then we try to get
    # the workspace by type + name:
    if workspace_name and workspace_type:
        response = self._otcs_frontend.get_workspace_by_type_and_name(
            type_name=workspace_type,
            name=workspace_name,
        )
        workspace_id = self._otcs_frontend.get_result_value(
            response=response,
            key="id",
        )
        if workspace_id:
            return (workspace_id, workspace_name)

    # if the code gets to here we dont have a nickname and the workspace with given name
    # type, or parent ID was not found either. Now we see if we can find the workspace name
    # as a synonym in the workspace data source to find the real/correct workspace name:
    if data_source_name:
        self.logger.info(
            "Try to find the workspace with the synonym -> '%s' using data source -> '%s'...",
            workspace_name,
            data_source_name,
        )

        (workspace_id, workspace_synonym_name) = self.process_bulk_workspaces_synonym_lookup(
            data_source_name=data_source_name,
            workspace_name_synonym=workspace_name,  # see if workspace_name is a synonym
            workspace_type=workspace_type,
        )
        if workspace_id is not None:
            return (workspace_id, workspace_synonym_name)

    # As this message may be hunderds of times in the log
    # we invest some effort to make it look nice:
    message = "Couldn't find a workspace "
    concat_string = ""
    if workspace_nickname:
        message += "by nickname -> '{}'".format(workspace_nickname)
        concat_string = ", nor "
    if workspace_name:
        message += "{}by name -> '{}'".format(concat_string, workspace_name)
        concat_string = ", nor "
    if parent_id:
        message += "{}by parent ID -> {}".format(concat_string, parent_id)
        concat_string = ", nor "
    if data_source_name:
        message += "{}as synonym in data source -> '{}'".format(
            concat_string,
            data_source_name,
        )
    if show_error:
        self.logger.error(message)
    else:
        self.logger.warning(message)

    return (
        None,
        workspace_name,
    )  # it is important to return the name - used by process_bulk_categories()

process_bulk_workspaces_synonym_lookup(data_source_name, workspace_name_synonym='', workspace_type='')

Use a data source to lookup the workspace name (or all fields) and ID using a given synonym.

Parameters:

Name Type Description Default
data_source_name str

The data source name.

required
workspace_name_synonym str

The synonym of the workspace name as input for lookup.

''
workspace_type str

The name of the workspace type. Default is "".

''

Returns:

Type Description
tuple[int | None, str | None] | None

tuple[int | None, int | None]: The workspace ID and the looked up workspace name or None in case the loomkup has failed.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspaces_synonym_lookup")
def process_bulk_workspaces_synonym_lookup(
    self,
    data_source_name: str,
    workspace_name_synonym: str = "",
    workspace_type: str = "",
) -> tuple[int | None, str | None] | None:
    """Use a data source to lookup the workspace name (or all fields) and ID using a given synonym.

    Args:
        data_source_name (str):
            The data source name.
        workspace_name_synonym (str, optional):
            The synonym of the workspace name as input for lookup.
        workspace_type (str, optional):
            The name of the workspace type. Default is "".

    Returns:
        tuple[int | None, int | None]:
            The workspace ID and the looked up workspace name or None in case the loomkup has failed.

    """

    # Determine the data source to use for synonym lookup:
    if not data_source_name:
        self.logger.error(
            "No workspace data source name specified. Cannot lookup the workspace by synonym -> '%s'!",
            workspace_name_synonym,
        )
        return (None, None)
    workspace_data_source = next(
        (item for item in self._bulk_datasources if item["name"] == data_source_name),
        None,
    )
    if not workspace_data_source:
        self.logger.error(
            "Workspace data source -> '%s' not found in payload. Cannot lookup the workspace by synonym -> '%s'!",
            data_source_name,
            workspace_name_synonym,
        )
        return (None, None)

    # Read the synonym column and the name column from the data source payload item:
    workspace_data_source_name_column = workspace_data_source.get(
        "name_column",
        None,  # e.g. "Name"
    )
    workspace_data_source_synonyms_column = workspace_data_source.get(
        "synonyms_column",
        None,  # e.g. "Synonyms"
    )

    if not workspace_data_source_name_column or not workspace_data_source_synonyms_column:
        self.logger.warning(
            "Workspace data source -> '%s' has no synonym lookup columns. Cannot find the workspace by synonym -> '%s'!",
            data_source_name,
            workspace_name_synonym,
        )
        return (None, None)

    # Get the row that has the synonym in the synonym column:
    lookup_row = self.lookup_data_source_value(
        data_source=workspace_data_source,
        lookup_column=workspace_data_source_synonyms_column,
        lookup_value=workspace_name_synonym,
    )

    if lookup_row is None:
        # Handle an edge case where the actual workspace name
        # is already correct (using the name column):
        lookup_row = self.lookup_data_source_value(
            data_source=workspace_data_source,
            lookup_column=workspace_data_source_name_column,
            lookup_value=workspace_name_synonym,
        )

    if lookup_row is not None:
        # Now we determine the real workspace name be taking it from
        # the name column in the result row:
        workspace_name = lookup_row[workspace_data_source_name_column]
        self.logger.info(
            "Found workspace name -> '%s' using synonym -> '%s'.",
            workspace_name,
            workspace_name_synonym,
        )

        # We now have the real name. If the workspace type name is
        # provided as well we should be able to lookup the workspace ID now:
        if workspace_type:
            response = self._otcs_frontend.get_workspace_by_type_and_name(
                type_name=workspace_type,
                name=workspace_name,
            )
            workspace_id = self._otcs_frontend.get_result_value(
                response=response,
                key="id",
            )
        else:
            # This method may be called with workspace_type=None.
            # In this case we can return the synonym but cannot
            # lookup the workspace ID:
            workspace_id = None

        # Return the tuple with workspace_id and the real workspace name
        return (workspace_id, workspace_name)

    return (None, None)

process_bulk_workspaces_worker(bulk_workspace, partition, workspace_type, template_name_field, workspace_name_field, workspace_description_field, categories=None, operations=None, results=None)

Thread worker to create workspaces in bulk.

Each worker thread gets a partition of the rows that include the data required for the workspace creation.

Parameters:

Name Type Description Default
bulk_workspace dict

The payload of the bulkWorkspace.

required
partition DataFrame

Data partition with rows to process.

required
template_id int

ID of the workspace template to use.

required
workspace_type dict

Workspace type data.

required
template_name_field str | None

Field where the template name is stored.

required
workspace_name_field str

Field where the workspace name is stored.

required
workspace_description_field str

Field where the workspace description is stored.

required
categories list

List of category dictionaries.

None
operations list

Defines which operations should be applyed on workspaces. Possible values are "create", "update", "delete", "recreate".

None
results list

A mutable list of thread results.

None
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
22671
22672
22673
22674
22675
22676
22677
22678
22679
22680
22681
22682
22683
22684
22685
22686
22687
22688
22689
22690
22691
22692
22693
22694
22695
22696
22697
22698
22699
22700
22701
22702
22703
22704
22705
22706
22707
22708
22709
22710
22711
22712
22713
22714
22715
22716
22717
22718
22719
22720
22721
22722
22723
22724
22725
22726
22727
22728
22729
22730
22731
22732
22733
22734
22735
22736
22737
22738
22739
22740
22741
22742
22743
22744
22745
22746
22747
22748
22749
22750
22751
22752
22753
22754
22755
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769
22770
22771
22772
22773
22774
22775
22776
22777
22778
22779
22780
22781
22782
22783
22784
22785
22786
22787
22788
22789
22790
22791
22792
22793
22794
22795
22796
22797
22798
22799
22800
22801
22802
22803
22804
22805
22806
22807
22808
22809
22810
22811
22812
22813
22814
22815
22816
22817
22818
22819
22820
22821
22822
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834
22835
22836
22837
22838
22839
22840
22841
22842
22843
22844
22845
22846
22847
22848
22849
22850
22851
22852
22853
22854
22855
22856
22857
22858
22859
22860
22861
22862
22863
22864
22865
22866
22867
22868
22869
22870
22871
22872
22873
22874
22875
22876
22877
22878
22879
22880
22881
22882
22883
22884
22885
22886
22887
22888
22889
22890
22891
22892
22893
22894
22895
22896
22897
22898
22899
22900
22901
22902
22903
22904
22905
22906
22907
22908
22909
22910
22911
22912
22913
22914
22915
22916
22917
22918
22919
22920
22921
22922
22923
22924
22925
22926
22927
22928
22929
22930
22931
22932
22933
22934
22935
22936
22937
22938
22939
22940
22941
22942
22943
22944
22945
22946
22947
22948
22949
22950
22951
22952
22953
22954
22955
22956
22957
22958
22959
22960
22961
22962
22963
22964
22965
22966
22967
22968
22969
22970
22971
22972
22973
22974
22975
22976
22977
22978
22979
22980
22981
22982
22983
22984
22985
22986
22987
22988
22989
22990
22991
22992
22993
22994
22995
22996
22997
22998
22999
23000
23001
23002
23003
23004
23005
23006
23007
23008
23009
23010
23011
23012
23013
23014
23015
23016
23017
23018
23019
23020
23021
23022
23023
23024
23025
23026
23027
23028
23029
23030
23031
23032
23033
23034
23035
23036
23037
23038
23039
23040
23041
23042
23043
23044
23045
23046
23047
23048
23049
23050
23051
23052
23053
23054
23055
23056
23057
23058
23059
23060
23061
23062
23063
23064
23065
23066
23067
23068
23069
23070
23071
23072
23073
23074
23075
23076
23077
23078
23079
23080
23081
23082
23083
23084
23085
23086
23087
23088
23089
23090
23091
23092
23093
23094
23095
23096
23097
23098
23099
23100
23101
23102
23103
23104
23105
23106
23107
23108
23109
23110
23111
23112
23113
23114
23115
23116
23117
23118
23119
23120
23121
23122
23123
23124
23125
23126
23127
23128
23129
23130
23131
23132
23133
23134
23135
23136
23137
23138
23139
23140
23141
23142
23143
23144
23145
23146
23147
23148
23149
23150
23151
23152
23153
23154
23155
23156
23157
23158
23159
23160
23161
23162
23163
23164
23165
23166
23167
23168
23169
23170
23171
23172
23173
23174
23175
23176
23177
23178
23179
23180
23181
23182
23183
23184
23185
23186
23187
23188
23189
23190
23191
23192
23193
23194
23195
23196
23197
23198
23199
23200
23201
23202
23203
23204
23205
23206
23207
23208
23209
23210
23211
23212
23213
23214
23215
23216
23217
23218
23219
23220
23221
23222
23223
23224
23225
23226
23227
23228
23229
23230
23231
23232
23233
23234
23235
23236
23237
23238
23239
23240
23241
23242
23243
23244
23245
23246
23247
23248
23249
23250
23251
23252
23253
23254
23255
23256
23257
23258
23259
23260
23261
23262
23263
23264
23265
23266
23267
23268
23269
23270
23271
23272
23273
23274
23275
23276
23277
23278
23279
23280
23281
23282
23283
23284
23285
23286
23287
23288
23289
23290
23291
23292
23293
23294
23295
23296
23297
23298
23299
23300
23301
23302
23303
23304
23305
23306
23307
23308
23309
23310
23311
23312
23313
23314
23315
23316
23317
23318
23319
23320
23321
23322
23323
23324
23325
23326
23327
23328
23329
23330
23331
23332
23333
23334
23335
23336
23337
23338
23339
23340
23341
23342
23343
23344
23345
23346
23347
23348
23349
23350
23351
23352
23353
23354
23355
23356
23357
23358
23359
23360
23361
23362
23363
23364
23365
23366
23367
23368
23369
23370
23371
23372
23373
23374
23375
23376
23377
23378
23379
23380
23381
23382
23383
23384
23385
23386
23387
23388
23389
23390
23391
23392
23393
23394
23395
23396
23397
23398
23399
23400
23401
23402
23403
23404
23405
23406
23407
23408
23409
23410
23411
23412
23413
23414
23415
23416
23417
23418
23419
23420
23421
23422
23423
23424
23425
23426
23427
23428
23429
23430
23431
23432
23433
23434
23435
23436
23437
23438
23439
23440
23441
23442
23443
23444
23445
23446
23447
23448
23449
23450
23451
23452
23453
23454
23455
23456
23457
23458
23459
23460
23461
23462
23463
23464
23465
23466
23467
23468
23469
23470
23471
23472
23473
23474
23475
23476
23477
23478
23479
23480
23481
23482
23483
23484
23485
23486
23487
23488
23489
23490
23491
23492
23493
23494
23495
23496
23497
23498
23499
23500
23501
23502
23503
23504
23505
23506
23507
23508
23509
23510
23511
23512
23513
23514
23515
23516
23517
23518
23519
23520
23521
23522
23523
23524
23525
23526
23527
23528
23529
23530
23531
23532
23533
23534
23535
23536
23537
23538
23539
23540
23541
23542
23543
23544
23545
23546
23547
23548
23549
23550
23551
23552
23553
23554
23555
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_bulk_workspaces_worker")
def process_bulk_workspaces_worker(
    self,
    bulk_workspace: dict,
    partition: pd.DataFrame,
    workspace_type: dict,
    template_name_field: str | None,
    workspace_name_field: str,
    workspace_description_field: str,
    categories: list | None = None,
    operations: list | None = None,
    results: list | None = None,
) -> None:
    """Thread worker to create workspaces in bulk.

    Each worker thread gets a partition of the rows that include
    the data required for the workspace creation.

    Args:
        bulk_workspace (dict):
            The payload of the bulkWorkspace.
        partition (pd.DataFrame):
            Data partition with rows to process.
        template_id (int):
            ID of the workspace template to use.
        workspace_type (dict):
            Workspace type data.
        template_name_field (str | None):
            Field where the template name is stored.
        workspace_name_field (str):
            Field where the workspace name is stored.
        workspace_description_field (str):
            Field where the workspace description is stored.
        categories (list, optional):
            List of category dictionaries.
        operations (list, optional):
            Defines which operations should be applyed on workspaces.
            Possible values are "create", "update", "delete", "recreate".
        results (list, optional):
            A mutable list of thread results.

    """

    thread_id = threading.get_ident()
    thread_name = threading.current_thread().name

    t = trace.get_current_span()
    t.set_attributes(
        {
            "thread.id": thread_id,
            "thread.name": thread_name,
        }
    )

    self.logger.info(
        "Start working on data frame partition of size -> %s to bulk create workspaces...",
        str(len(partition)),
    )

    # Avoid linter warnings - so make parameter default None while we
    # actually want ["create"] to be the default:
    if operations is None:
        operations = ["create"]

    result = {}
    result["thread_id"] = thread_id
    result["thread_name"] = thread_name
    result["success_counter"] = 0
    result["failure_counter"] = 0
    result["skipped_counter"] = 0
    result["create_counter"] = 0
    result["update_counter"] = 0
    result["delete_counter"] = 0
    result["workspaces"] = {}
    result["success"] = True

    # Check if workspaces have been processed before, e.i. testing
    # if a "workspaces" key exists and if it is pointing to a non-empty list.
    # Additionally we check that workspace updates are not enforced:
    if (
        bulk_workspace.get("workspaces")
        and "update" not in operations
        and "delete" not in operations
        and "recreate" not in operations
    ):
        existing_workspaces = bulk_workspace["workspaces"]
        self.logger.info(
            "Found %s already processed workspaces. Try to complete the job...",
            str(len(existing_workspaces)),
        )
    else:
        existing_workspaces = {}

    # See if external creation and modification dates are in the payload data:
    external_modify_date_field = bulk_workspace.get("external_modify_date")
    external_create_date_field = bulk_workspace.get("external_create_date")

    # See if we have a key field to uniquely identify an existing workspace:
    key_field = bulk_workspace.get("key")

    # Get dictionary of replacements for bulk workspace creations
    # this we will be used of all places data is read from the
    # data frame. Each dictionary item has the field name as the
    # dictionary key and a list of regular expressions as dictionary value
    replacements = bulk_workspace.get("replacements")

    # In case the name cannot be resolved we allow to
    # specify an alternative name field in the payload.
    name_field_alt = bulk_workspace.get("name_alt")

    # In case the description cannot be resolved we allow to
    # specify an alternative description field in the payload.
    description_field_alt = bulk_workspace.get("description_alt")

    # Fetch the nickname field from the payload (if it is specified):
    nickname_field = bulk_workspace.get("nickname")

    # In case the nickname cannot be resolved we allow to
    # specify an alternative nickname field in the payload.
    nickname_field_alt = bulk_workspace.get("nickname_alt")

    # Nicknames are very limited in terms of allowed characters.
    # For nicknames we need additional regexp as we need to
    # replace all non-alphanumeric, non-space characters with ""
    # We also preserve hyphens in the first step to replace
    # them below with underscores. This is important to avoid
    # that different spellings of names produce different nicknames.
    # We want spellings with spaces match spellings with hyphens.
    # For this the workspace names have a regexp "-| " in the payload.
    nickname_additional_regex_list = [r"[^\w\s-]"]

    # Classification can either be provided by classification pathes
    # or by nicknames:
    classification_pathes = bulk_workspace.get("classification_pathes", [])
    classification_nicknames = bulk_workspace.get("classification_nicknames", [])

    total_rows = len(partition)

    # Process all rows in the partition that was given to this thread:
    for index, row in partition.iterrows():
        with tracer.start_as_current_span("process_bulk_workspaces_worker-workspace") as t:
            # Calculate percentage of completion:
            percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

            self.logger.info(
                "Processing data row -> %s for bulk workspace creation (%.2f %% complete)...",
                str(index),
                percent_complete,
            )

            # Clear variables to esure clean state for each row:
            workspace_id = None

            workspace_template = None
            if template_name_field is None:
                workspace_template = workspace_type["templates"][0]

            else:
                workspace_template_name = self.replace_bulk_placeholders(
                    input_string=template_name_field,
                    row=row,
                    replacements=replacements,
                )

                workspace_template = next(
                    (item for item in workspace_type["templates"] if item["name"] == workspace_template_name),
                    None,
                )

            if workspace_template is None:
                self.logger.error(
                    "Workspace template -> '%s' has been specified in payload but it doesn't exist!",
                    workspace_template_name,
                )
                self.logger.error(
                    "Workspace type -> '%s' has only these templates -> %s",
                    workspace_type["name"],
                    workspace_type["templates"],
                )
                result["success"] = False
                result["failure_counter"] += 1
                continue

            template_id = workspace_template["id"]
            template_name = workspace_template["name"]
            workspace_type_id = workspace_type["id"]

            # Determine the workspace name:
            workspace_name = self.replace_bulk_placeholders(
                input_string=workspace_name_field,
                row=row,
                replacements=replacements,
            )
            # If we cannot get the workspace name from the
            # workspace_name_field we try the alternative name field
            # (if specified in payload via "name_alt"):
            if not workspace_name and name_field_alt:
                self.logger.debug(
                    "Row -> %s does not have the data to resolve the placeholders in workspace name -> %s! Trying alternative name field -> %s...",
                    str(index),
                    workspace_name_field,
                    name_field_alt,
                )
                workspace_name = self.replace_bulk_placeholders(
                    input_string=name_field_alt,
                    row=row,
                    replacements=replacements,
                )

            if not workspace_name:
                self.logger.warning(
                    "Row -> %s does not have the required data to resolve -> %s for the workspace name!%s",
                    str(index),
                    workspace_name_field,
                    " There's no alternative field name given in the payload (via 'name_alt')."
                    if not name_field_alt
                    else " The alternative field -> '{}' could not be resolved either!".format(
                        name_field_alt,
                    ),
                )
                result["skipped_counter"] += 1
                continue

            # Cleanse the workspace name (allowed characters, maximum length):
            workspace_name = OTCS.cleanse_item_name(workspace_name)

            # Check if workspace has been created before (either in this run
            # or in a former run of the customizer):
            if (
                workspace_name in existing_workspaces  # processed in former run?
                or workspace_name in result["workspaces"]  # processed in current run?
            ):
                self.logger.info(
                    "Workspace -> '%s' does already exist. Skipping...",
                    workspace_name,
                )
                result["skipped_counter"] += 1
                continue

            # Determine the description field:
            if workspace_description_field:
                description = self.replace_bulk_placeholders(
                    input_string=workspace_description_field,
                    row=row,
                )
                # If we cannot get the description from the
                # workspace_description_field we try the alternative name field
                # (if specified in payload via "description_alt"):
                if not description and description_field_alt:
                    self.logger.debug(
                        "Row -> %s does not have the data to resolve the placeholders in workspace description -> %s! Trying alternative description field -> %s...",
                        str(index),
                        workspace_name_field,
                        description_field_alt,
                    )
                    description = self.replace_bulk_placeholders(
                        input_string=description_field_alt,
                        row=row,
                    )
            else:
                description = ""

            # Create a copy of the mutable operations list as we may
            # want to modify it per row:
            row_operations = list(operations)

            # Check if all data conditions to create the workspace are met
            conditions = bulk_workspace.get("conditions")
            if conditions:
                evaluated_condition = self.evaluate_conditions(
                    conditions=conditions,
                    row=row,
                    replacements=replacements,
                )
                if not evaluated_condition:
                    self.logger.info(
                        "Condition for bulk workspace row -> %s not met. Skipping row for workspace creation...",
                        str(index),
                    )
                    result["skipped_counter"] += 1
                    continue

            # Check if all data conditions to create or recreate the workspace are met:
            if "create" in row_operations or "recreate" in row_operations:
                conditions_create = bulk_workspace.get("conditions_create")
                if conditions_create:
                    evaluated_conditions_create = self.evaluate_conditions(
                        conditions=conditions_create,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_create:
                        self.logger.info(
                            "Create condition for bulk workspace row -> %s not met. Excluding create operation for current row...",
                            str(index),
                        )
                        if "create" in row_operations:
                            row_operations.remove("create")
                        if "recreate" in row_operations:
                            row_operations.remove("recreate")
                elif (
                    "recreate" in row_operations
                ):  # we still create and recreate without conditions_create. But give a warning for 'recreate' without condition.
                    self.logger.warning(
                        "No create condition provided but 'recreate' operation requested. This will recreate all existing workspaces!",
                    )

            # Check if all data conditions to delete the workspace are met:
            if "delete" in row_operations:
                conditions_delete = bulk_workspace.get("conditions_delete")
                if conditions_delete:
                    evaluated_conditions_delete = self.evaluate_conditions(
                        conditions=conditions_delete,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_delete:
                        self.logger.info(
                            "Delete condition for bulk workspace row -> %s not met. Excluding delete operation for current row...",
                            str(index),
                        )
                        row_operations.remove("delete")
                else:  # without delete_conditions we don't delete!!
                    self.logger.warning(
                        "Delete operation requested for bulk workspaces but conditions for deletion are missing! (specify 'conditions_delete')!",
                    )
                    row_operations.remove("delete")

            # Check if all data conditions to delete the workspace are met:
            if "update" in row_operations:
                conditions_update = bulk_workspace.get("conditions_update")
                if conditions_update:
                    evaluated_conditions_update = self.evaluate_conditions(
                        conditions=conditions_update,
                        row=row,
                        replacements=replacements,
                    )
                    if not evaluated_conditions_update:
                        self.logger.info(
                            "Update condition for bulk workspace row -> %s not met. Excluding update operation for current row...",
                            str(index),
                        )
                        row_operations.remove("update")

            # Determine the external modification date (if any):
            if external_modify_date_field:
                external_modify_date = self.replace_bulk_placeholders(
                    input_string=external_modify_date_field,
                    row=row,
                )
            else:
                external_modify_date = None

            # Determine the external creation date (if any):
            if external_create_date_field:
                external_create_date = self.replace_bulk_placeholders(
                    input_string=external_create_date_field,
                    row=row,
                )
            else:
                external_create_date = None

            # Determine the key field (if any):
            key = self.replace_bulk_placeholders(input_string=key_field, row=row) if key_field else None

            # check if workspace with this nickname does already exist.
            # we also store the nickname to assign it to the new workspace:
            found_workspace_name = None
            found_workspace_id = None
            if nickname_field:
                nickname = self.replace_bulk_placeholders(
                    input_string=nickname_field,
                    row=row,
                    replacements=replacements,
                    additional_regex_list=nickname_additional_regex_list,
                )
                # If we cannot get the nickname from the
                # workspace_nickname_field we try the alternative nickname field
                # (if specified in payload via "nickname_alt"):
                if not nickname and nickname_field_alt:
                    nickname = self.replace_bulk_placeholders(
                        input_string=nickname_field_alt,
                        row=row,
                        replacements=replacements,
                        additional_regex_list=nickname_additional_regex_list,
                    )

                # Nicknames for sure should not have leading or trailing spaces:
                nickname = nickname.strip()
                # Nicknames for sure are not allowed to include spaces:
                nickname = nickname.replace(" ", "_")
                # We also want to replace hyphens with underscores
                # to make sure that workspace name spellings with
                # spaces and with hyphens are mapped to the same
                # workspace nicknames (aligned with the workspace names
                # that have a regexp rule for this in the payload):
                nickname = nickname.replace("-", "_")
                nickname = nickname.replace("___", "_")
                nickname = nickname.lower()
                response = self._otcs_frontend.get_node_from_nickname(nickname=nickname)
                if response:
                    found_workspace_name = self._otcs_frontend.get_result_value(
                        response=response,
                        key="name",
                    )
                    found_workspace_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                    if found_workspace_name != workspace_name:
                        self.logger.warning(
                            "Clash of nicknames for -> '%s' and -> '%s' (%s)! We will not create the workspace but allow for updates and deletes",
                            workspace_name,
                            found_workspace_name,
                            found_workspace_id,
                        )
                        # Remove 'create' from row operations as it would produce errors about uniqueness of nicknames.
                        if "create" in row_operations:
                            row_operations.remove("create")
                    # Only skip, if workspace 'update' or 'delete' is NOT requested:
                    elif "update" not in row_operations and "delete" not in row_operations:
                        self.logger.info(
                            "Workspace -> '%s' with nickname -> '%s' does already exist (found -> '%s'). No update or delete operations requested or allowed. Skipping...",
                            workspace_name,
                            nickname,
                            found_workspace_name,
                        )
                        result["skipped_counter"] += 1
                        continue
            # end if nickname_field
            else:
                nickname = None

            t.add_event(name="Preparations done.", timestamp=time.time_ns())

            # Based on the evaluation of conditions_create, conditions_delete,
            # and conditions_update we could end up with an empty operations list.
            # In such cases we skip the further processing of this row:
            if not row_operations:
                self.logger.info(
                    "Skip workspace -> '%s' as row operations is empty (no create, update, delete or recreate). This may be because conditions_create, conditions_delete, and conditions_update have been evaluated to false for this row.",
                    workspace_name,
                )
                result["skipped_counter"] += 1
                continue

            self.logger.info(
                "Bulk process workspace -> '%s' using effective operations -> %s...",
                workspace_name,
                str(row_operations),
            )

            # Check if the workspace does already exist.
            # First we try to look up the workspace by a unique
            # key that does not change over time.
            # For this we expect a "key" value to be defined for the
            # bulkWorkspace and one of the category / attribute item
            # to be marked with "is_key" = True. If we don't find the
            # workspace via key we try via name and type.
            self.logger.info(
                "Check if workspace -> '%s' does already exist...",
                workspace_name,
            )
            # Initialize variables - this is important!
            workspace_old_name = None
            workspace_id = None
            handle_name_clash = False
            workspace_modify_date = None
            parent_id = None

            # We have 4 cases to differentiate:

            # 1. key given + key found = name irrelevant, item exist
            # 2. key given + key not found = if name exist it is a name clash
            # 3. no key given + name found = item exist
            # 4. no key given + name not found = item does not exist

            # We are NOT trying to compensate for a failed key lookup with a name lookup!
            # Names are only relevant if no key is in payload!

            if key:
                # if we have a key we need to also know which category attribute is
                # storing the value for the key:
                key_attribute = next(
                    (cat_attr for cat_attr in categories if cat_attr.get("is_key", False) is True),
                    None,
                )
                if not key_attribute:
                    self.logger.error(
                        "Bulk Workspace has key -> '%s' defined but none of the category attributes is marked as key ('is_key' is missing)!",
                        key,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue
                cat_name = key_attribute.get("name", None)
                att_name = key_attribute.get("attribute", None)
                set_name = key_attribute.get("set", None)

                # determine where workspaces of this type typically reside (IMPORTANT: this
                # may return None if no instances of this workspace type exist!):
                parent_id = self._otcs_frontend.get_workspace_type_location(
                    type_id=workspace_type_id,
                )
                if parent_id:
                    # Try to find the node that has the given key attribute value:
                    response = self._otcs_frontend.lookup_nodes(
                        parent_node_id=parent_id,
                        category=cat_name,
                        attribute=att_name,
                        attribute_set=set_name,
                        value=key,
                    )
                    workspace_id = self._otcs_frontend.get_result_value(
                        response=response,
                        key="id",
                    )
                else:
                    # if we couldn't determine the parent ID this means there are
                    # now workspace instances for this workspace type. Then we set
                    # workspace_id = None and let the code go into the else case below:
                    workspace_id = None

                if workspace_id:
                    # Case 1: key given + key found = name irrelevant, item exist
                    workspace_old_name = self._otcs_frontend.get_result_value(
                        response=response,
                        key="name",
                    )
                    self.logger.info(
                        "Found existing workspace -> %s (%s) in folder with ID -> %s using key value -> '%s', category -> '%s', and attribute -> '%s'.",
                        workspace_old_name,
                        workspace_id,
                        parent_id,
                        key,
                        cat_name,
                        att_name,
                    )
                    if workspace_old_name != workspace_name:
                        self.logger.info(
                            "Existing workspace has the old name -> '%s' which is different from the new name -> '%s'.",
                            workspace_old_name,
                            workspace_name,
                        )
                    else:
                        workspace_old_name = None
                    # We get the modify date of the existing workspace.
                    workspace_modify_date = self._otcs_frontend.get_result_value(
                        response=response,
                        key="modify_date",
                    )
                else:
                    # Case 2: key given + key not found = if name exist it is a name clash
                    self.logger.info(
                        "Couldn't find existing workspace with the key value -> '%s' in category -> '%s' and attribute -> '%s' in folder with ID -> %s.",
                        key,
                        cat_name,
                        att_name,
                        parent_id,
                    )
                    handle_name_clash = True
                # end if key_attribute
            # end if key
            else:
                # If we haven't a key we try by type + name
                response = self._otcs_frontend.get_workspace_by_type_and_name(
                    type_id=workspace_type_id,
                    name=workspace_name,
                )
                workspace_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if workspace_id:
                    # Case 3: no key given + name found = item exist
                    self.logger.info(
                        "Found existing workspace -> '%s' (%s) with type ID -> %s.",
                        workspace_name,
                        workspace_id,
                        workspace_type_id,
                    )
                    # We get the modify date of the existing workspace.
                    workspace_modify_date = self._otcs_frontend.get_result_value(
                        response=response,
                        key="modify_date",
                    )
                else:
                    # Case 4: no key given + name not found = item does not exist
                    self.logger.info(
                        "No existing workspace with name -> '%s' and type ID -> %s.",
                        workspace_name,
                        workspace_type_id,
                    )
                    # Check if we found an existing workspace by the same nickname:
                    if found_workspace_id and found_workspace_name:
                        self.logger.info(
                            "But there's a workspace -> '%s' (%s) that has a matching nickname -> '%s'. Using this workspace instead...",
                            found_workspace_name,
                            found_workspace_id,
                            nickname,
                        )
                        workspace_id = found_workspace_id

            # Check if we want to recreate an existing workspace
            # then we handle the "delete" part of "recreate" first:
            if workspace_id and "recreate" in row_operations:
                response = self._otcs_frontend.delete_node(
                    node_id=workspace_id,
                    purge=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to recreate existing workspace -> '%s' (%s) with type ID -> %s! Delete failed.",
                        (workspace_name if workspace_old_name is None else workspace_old_name),
                        workspace_id,
                        workspace_type_id,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue
                result["delete_counter"] += 1
                self.logger.info(
                    "Deleted existing workspace -> '%s' (%s) as part of the recreate operation.",
                    (workspace_name if workspace_old_name is None else workspace_old_name),
                    workspace_id,
                )
                workspace_id = None

            # Check if workspace does not exists - then we create a new workspace
            # if this is requested ("create" or "recreate" value in operations list in payload)
            if not workspace_id and ("create" in row_operations or "recreate" in row_operations):
                # for Case 2 (we looked up the workspace by key but could have name collisions)
                # we need to see if we have a name collision:
                if handle_name_clash and parent_id:
                    response = self._otcs_frontend.check_node_name(
                        parent_id=parent_id,
                        node_name=workspace_name,
                    )
                    # result is a list of names that clash - if it is empty we have no clash
                    if response["results"]:
                        # We add the suffix with the key which should be unique:
                        self.logger.warning(
                            "Workspace -> '%s' does already exist in folder with ID -> %s and we need to handle the name clash by using name -> '%s'",
                            workspace_name,
                            parent_id,
                            workspace_name + " (" + key + ")",
                        )
                        workspace_name = workspace_name + " (" + key + ")"

                # If category data is in payload we substitute
                # the values with data from the current data row:
                if categories:
                    # Make sure the threads are not changing data structures that
                    # are shared between threads. categories is a list of dicts.
                    # list and dicts are "mutable" data structures in Python!
                    worker_categories = self.process_bulk_categories(
                        row=row,
                        index=index,
                        categories=categories,
                        replacements=replacements,
                    )
                    workspace_category_data = self.prepare_workspace_create_form(
                        categories=worker_categories,
                        template_id=template_id,
                    )
                    if not workspace_category_data:
                        self.logger.error(
                            "Failed to prepare the category data for new workspace -> '%s'!",
                            workspace_name,
                        )
                        result["success"] = False
                        result["failure_counter"] += 1
                        continue  # for index, row in partition.iterrows()
                else:
                    workspace_category_data = {}

                self.logger.info(
                    "Bulk create workspace -> '%s'...",
                    workspace_name,
                )

                if classification_nicknames or classification_pathes:
                    classification_ids = self.process_bulk_classification_assignments(
                        row=row,
                        index=index,
                        classifications=classification_pathes + classification_nicknames,
                        replacements=replacements,
                    )
                else:
                    classification_ids = None

                # Create the workspace with all provided information:
                response = self._otcs_frontend.create_workspace(
                    workspace_template_id=template_id,
                    workspace_name=workspace_name,
                    workspace_description=description,
                    workspace_type=workspace_type_id,
                    category_data=workspace_category_data,
                    classifications=classification_ids,
                    external_create_date=external_create_date,
                    external_modify_date=external_modify_date,
                    show_error=False,
                )
                if response is None:
                    # Potential race condition: see if the workspace has been created by a concurrent thread.
                    # So we better check if the workspace is there even if the create_workspace() call delivered
                    # a 'None' response:
                    response = self._otcs_frontend.get_workspace_by_type_and_name(
                        type_id=workspace_type_id,
                        name=workspace_name,
                    )
                workspace_id = self._otcs_frontend.get_result_value(
                    response=response,
                    key="id",
                )
                if not workspace_id:
                    self.logger.error(
                        "Failed to bulk create workspace -> '%s' with type ID -> %s from template -> '%s' (%s)!",
                        workspace_name,
                        workspace_type_id,
                        template_name,
                        template_id,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue
                self.logger.info(
                    "Successfully created workspace -> '%s' with ID -> %s from template -> '%s' (%s).",
                    workspace_name,
                    workspace_id,
                    template_name,
                    template_id,
                )
                result["create_counter"] += 1

                # Is Content Aviator enabled system-wide and is it enabled for this
                # bulkWorkspaces?
                if self._aviator_enabled and bulk_workspace.get("enable_aviator", False):
                    response = self._otcs_frontend.update_workspace_aviator(
                        workspace_id=workspace_id,
                        status=True,
                    )
                    if not response:
                        self.logger.error(
                            "Failed to enable Content Aviator for workspace -> '%s' (%s)!",
                            workspace_name,
                            workspace_id,
                        )

                # Check if metadata embeddings need to be updated
                aviator_metadata = bulk_workspace.get("aviator_metadata", False)
                if aviator_metadata:
                    self.logger.info(
                        "Trigger Content Aviator metadata embedding for workspace -> '%s' (%s)...",
                        workspace_name,
                        workspace_id,
                    )

                    self._otcs_frontend.aviator_embed_metadata(
                        node_id=workspace_id,
                        wait_for_completion=False,
                    )

            # end if not workspace_id and "create" or "recreate" in row_operations

            # If "updates" are an requested row operation we update the existing workspace with
            # fresh metadata from the payload. Additionally we check the external
            # modify date to support incremental load for content that has really
            # changed.
            # In addition we check that "delete" is not requested as otherwise it will
            # never go in elif "delete" ... below (and it does not make sense to update a workspace
            # that is deleted in the next step...)
            elif (
                workspace_id
                and "update" in row_operations
                and "delete" not in row_operations  # note the NOT !
                and OTCS.date_is_newer(
                    date_old=workspace_modify_date,
                    date_new=external_modify_date,
                )
            ):
                # get the specific update operations given in the payload
                # if not specified we do all 4 update operations (name, description, categories and nickname)
                update_operations = bulk_workspace.get(
                    "update_operations",
                    ["name", "description", "categories", "nickname", "classifications", "members"],
                )

                # If category data is in payload we substitute
                # the values with data from the current data row.
                # This is only done if "categories" update is not
                # excluded from the update_operations:
                if categories and "categories" in update_operations:
                    # Make sure the threads are not changing data structures that
                    # are shared between threads. categories is a list of dicts.
                    # list and dicts are "mutable" data structures in Python!
                    worker_categories = self.process_bulk_categories(
                        row=row,
                        index=index,
                        categories=categories,
                        replacements=replacements,
                    )
                    # response = self._otcs_frontend.get_node(node_id=workspace_id)
                    # parent_id = self._otcs_frontend.get_result_value(response=response, key="parent_id")
                    # workspace_category_data = self.prepare_item_create_form(
                    #     parent_id=parent_id,
                    #     categories=worker_categories,
                    #     subtype=self._otcs_frontend.ITEM_TYPE_BUSINESS_WORKSPACE,
                    # )
                    # Transform the payload structure into the format
                    # the OTCS REST API requires:
                    workspace_category_data = self.prepare_category_data(
                        categories_payload=worker_categories,
                        source_node_id=workspace_id,
                    )
                    if not workspace_category_data:
                        self.logger.error(
                            "Failed to prepare the updated category data for workspace -> '%s'!",
                            workspace_name,
                        )
                        result["success"] = False
                        result["failure_counter"] += 1
                        continue  # for index, row in partition.iterrows()
                # end if categories
                else:
                    workspace_category_data = {}

                self.logger.info(
                    "Update existing workspace -> '%s' (%s) with operations -> %s...",
                    workspace_old_name if workspace_old_name else workspace_name,
                    str(workspace_id),
                    str(update_operations),
                )
                # Update the workspace with all provided information:
                response = self._otcs_frontend.update_workspace(
                    workspace_id=workspace_id,
                    workspace_name=(workspace_name if "name" in update_operations else None),
                    workspace_description=(description if "description" in update_operations else None),
                    category_data=(workspace_category_data if "categories" in update_operations else None),
                    external_create_date=external_create_date,
                    external_modify_date=external_modify_date,
                    show_error=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to update existing workspace -> '%s' (%s) with type ID -> %s!",
                        (workspace_old_name if workspace_old_name else workspace_name),
                        workspace_id,
                        workspace_type_id,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue
                self.logger.info(
                    "Updated existing workspace -> '%s' (%s) with type ID -> %s.",
                    workspace_name if "name" in update_operations or not workspace_old_name else workspace_old_name,
                    workspace_id,
                    workspace_type_id,
                )
                result["update_counter"] += 1

                if "classifications" in update_operations and (classification_nicknames or classification_pathes):
                    classification_ids = self.process_bulk_classification_assignments(
                        row=row,
                        index=index,
                        classifications=classification_pathes + classification_nicknames,
                    )
                    response = self._otcs.assign_classifications(
                        node_id=workspace_id,
                        classifications=classification_ids,
                        apply_to_sub_items=False,
                    )
                    if response is None:
                        self.logger.error(
                            "Failed to assign classifications -> %s to workspace -> '%s' (%s)!",
                            classification_ids,
                            workspace_name
                            if "name" in update_operations or not workspace_old_name
                            else workspace_old_name,
                            workspace_id,
                        )
                    else:
                        self.logger.info(
                            "Successfully assigned classifications -> %s to workspace -> '%s' (%s).",
                            classification_ids,
                            workspace_name
                            if "name" in update_operations or not workspace_old_name
                            else workspace_old_name,
                            workspace_id,
                        )

                # Check if metadata embeddings need to be updated
                aviator_metadata = bulk_workspace.get("aviator_metadata", False)
                if aviator_metadata:
                    self.logger.info(
                        "Update Content Aviator metadata embedding for workspace -> %s (%s)...",
                        workspace_name,
                        workspace_id,
                    )

                    self._otcs_frontend.aviator_embed_metadata(
                        node_id=workspace_id,
                        wait_for_completion=False,
                    )
            # end elif "update" in row_operations...
            elif workspace_id and "delete" in row_operations:
                # We delete with immediate purging to keep recycle bin clean
                # and to not run into issues with nicknames used in deleted items:
                response = self._otcs_frontend.delete_node(
                    node_id=workspace_id,
                    purge=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to delete existing workspace -> '%s' (%s) with type ID -> %s!",
                        (workspace_old_name if workspace_old_name else workspace_name),
                        workspace_id,
                        workspace_type_id,
                    )
                    result["success"] = False
                    result["failure_counter"] += 1
                    continue
                self.logger.info(
                    "Successfully deleted existing workspace -> '%s' (%s).",
                    workspace_old_name if workspace_old_name else workspace_name,
                    workspace_id,
                )
                result["delete_counter"] += 1
                workspace_id = None
            # end elif workspace_id and "delete" in row_operations

            # this is the plain old "it does exist and we just skip it" case:
            elif workspace_id:
                result["skipped_counter"] += 1
                self.logger.info(
                    "Skipped existing workspace -> '%s' (%s)",
                    workspace_old_name if workspace_old_name else workspace_name,
                    workspace_id,
                )
            # this is the case where we just want to operate on existing workspaces (update or delete)
            # but they do not exist:
            elif not workspace_id and ("update" in row_operations or "delete" in row_operations):
                result["skipped_counter"] += 1
                self.logger.info(
                    "Skipped update/delete of non-existing workspace -> '%s'.",
                    workspace_old_name if workspace_old_name else workspace_name,
                )

            # The following code is executed for all operations
            # (create, recreate, update, delete):

            # Check if we want to set / update the nickname of the workspace.
            # Precondition is we have determined a nickname, we have the workspace ID
            # and the update of the nickname is either required (create, recreate)
            # or requested (update_operations include "nickname"):
            if (
                nickname
                and workspace_id
                and (
                    "create" in row_operations
                    or "recreate" in row_operations
                    or ("update" in row_operations and "nickname" in update_operations)
                )
            ):
                response = self._otcs_frontend.set_node_nickname(
                    node_id=workspace_id,
                    nickname=nickname,
                    show_error=True,
                )
                if not response:
                    self.logger.error(
                        "Failed to assign nickname -> '%s' to workspace -> '%s'!",
                        nickname,
                        workspace_name,
                    )

            # Check if we want to set / update the members for the workspace roles.
            # Precondition is we have members configured in the payload and setting
            # of roles is requested (workspaces is created or update_operations
            # include "members" operation):
            members = bulk_workspace.get("members")
            if (
                members
                and workspace_id
                and (
                    "create" in row_operations
                    or "recreate" in row_operations
                    or ("update" in row_operations and "members" in update_operations)
                )
            ):
                self.looger.info(
                    "Update workspace role members for workspace -> '%s' (%s)...",
                    workspace_name,
                    workspace_id,
                )
                workspace_roles = self._otcs_frontend.get_workspace_roles(
                    workspace_id=workspace_id,
                )

                # Traverse the members payload:
                for member in members:
                    # Get the member role from payload first. It is mandatory:
                    member_role = member.get("role", None)
                    if not member_role:
                        self.logger.warning(
                            "Members of workspace -> '%s' is missing the role name.",
                            workspace_name,
                        )
                        continue
                    member_role = self.replace_bulk_placeholders(input_string=member_role, row=row)
                    if not member_role:
                        continue
                    role_id = self._otcs.lookup_result_value(
                        response=workspace_roles,
                        key="name",
                        value=member_role,
                        return_key="id",
                    )
                    if not role_id:
                        self.logger.warning(
                            "Cannot find workspace role -> '%s' for workspace -> '%s' (%s). Skipping...",
                            member_role,
                            workspace_name,
                            workspace_id,
                        )
                        continue

                    # read user list and role name from payload:
                    member_users = member.get("users", [])
                    member_groups = member.get("groups", [])

                    if member_users:
                        self.replace_bulk_placeholders_list(input_list=member_users, row=row)
                    if member_groups:
                        self.replace_bulk_placeholders_list(input_list=member_groups, row=row)

                    if not member_users and not member_groups:
                        self.logger.debug(
                            "Role -> '%s' of workspace -> '%s' does not have any members (no users nor groups).",
                            member_role,
                            workspace_name,
                        )
                        continue

                    # check if we want to clear (remove) existing members of this role:
                    member_clear = member.get("clear", False)
                    if member_clear:
                        self.logger.info(
                            "Clear existing members of role -> '%s' (%s) for workspace -> '%s' (%s)...",
                            member_role,
                            role_id,
                            workspace_name,
                            workspace_id,
                        )
                        self._otcs.remove_workspace_members(
                            workspace_id=workspace_id,
                            role_id=role_id,
                        )
                # TODO: complete the implementation...
            # end if members...

            # Depending on the bulk operations (create, update, delete)
            # and the related conditions it may well be that workspace_id is None.
            # Only if workspace ID is not none we want to count this row as success:
            if workspace_id is not None:
                result["success_counter"] += 1
                # Record the workspace name and ID to allow to read it from failure file
                # and speedup the process.
                result["workspaces"][workspace_name] = workspace_id
    # end for index...

    self.logger.info("End working...")

    results.append(result)

process_business_object_types(section_name='businessObjectTypes')

Create a data structure for all business object types in the Extended ECM system.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'businessObjectTypes'

Returns:

Name Type Description
list list

A list of business object types. Each list element is a dict with these values: - id (str) - name (str) - type (str) - ext_system_id (str) - business_properties (list) - business_property_groups (list)

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_business_object_types")
def process_business_object_types(
    self,
    section_name: str = "businessObjectTypes",
) -> list:
    """Create a data structure for all business object types in the Extended ECM system.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        list:
            A list of business object types. Each list element is a dict with these values:
            - id (str)
            - name (str)
            - type (str)
            - ext_system_id (str)
            - business_properties (list)
            - business_property_groups (list)

    """

    # If this payload section has been processed successfully before we
    # still need to read the data structure from the status file and
    # initialize self._business_object_types:
    if self.check_status_file(payload_section_name=section_name):
        # read the list from the json file in admin Home
        # this is important for restart of customizer pod
        # as this data structure is used later on for workspace processing
        self.logger.info(
            "Re-Initialize business object types list from status file -> '%s' for later use...",
            self.get_status_file_name(payload_section_name=section_name),
        )
        self._business_object_types = self.get_status_file(
            payload_section_name=section_name,
        )
        if self._business_object_types:
            self.logger.info(
                "Found -> %s business object types.",
                str(len(self._business_object_types)),
            )
            self.logger.debug(
                "Business object types -> %s",
                str(self._business_object_types),
            )
            return self._business_object_types
        else:
            self.logger.warning(
                "Couldn't read business object types from status file -> '%s'. Regenerate list...",
                self.get_status_file_name(payload_section_name=section_name),
            )

    success: bool = True

    # Read payload_section "businessObjectTypes" if available.
    # For some (external) payload we may want to skip this time-
    # consuming processing as there are not BO specific payload
    # elements:
    for section in self._payload_sections:
        # Check if the section does exist and it is explicitly disabled
        # (enabled = False). Then we can skip further processing:
        if section.get("name", "") == "businessObjectTypes" and not section.get(
            "enabled",
            True,
        ):
            self.logger.info(
                "Skip business object type processing as its section is disabled in payload.",
            )
            return self._business_object_types

    # get all business object types (these have been created by the transports and are not in the payload!)
    # we need to do this each time as it needs to work across multiple payload files...
    response = self._otcs.get_business_object_types()
    if response is None:
        self.logger.info("No business object types found!")
        self._business_object_types = []
    else:
        # Store result list in self._business_object_types for later use:
        self._business_object_types = response["results"]
        self.logger.info(
            "Found -> %s business object types.",
            str(len(self._business_object_types)),
        )
        self.logger.debug(
            "Business object types -> %s",
            str(self._business_object_types),
        )

    otcs_version = float(self._otcs.get_server_version())
    if otcs_version >= 25.3:
        self.logger.info(
            "Using new REST API of OTCS %s to retrieve business object type information...",
            str(otcs_version),
        )
    else:
        self.logger.info(
            "Have to use transport extraction with OTCS %s to retrieve business object type information...",
            str(otcs_version),
        )

    # now we enrich the business_object_type list elments (which are dicts)
    # with additional dict elements for further processing:
    for business_object_type in self._business_object_types:
        # Flatten the response structure for more easy retrieval:
        # Get BO Type (e.g. KNA1):
        bo_type = business_object_type["data"]["properties"]["bo_type"]
        self.logger.debug("Business object type -> %s", bo_type)
        business_object_type["type"] = bo_type
        # Get BO Type ID:
        bo_type_id = business_object_type["data"]["properties"]["bo_type_id"]
        self.logger.debug("Business object type ID -> %s", bo_type_id)
        business_object_type["id"] = bo_type_id
        # Get BO Type Name:
        bo_type_name = business_object_type["data"]["properties"]["bo_type_name"]
        self.logger.debug("Business object type name -> %s", bo_type_name)
        business_object_type["name"] = bo_type_name
        # Get External System ID:
        ext_system_id = business_object_type["data"]["properties"]["ext_system_id"]
        self.logger.debug("External system ID -> %s", ext_system_id)
        business_object_type["ext_system_id"] = ext_system_id
        # Get External System ID:
        workspace_type_id = business_object_type["data"]["properties"]["workspace_type_id"]
        self.logger.debug("Workspace type ID -> %s", workspace_type_id)
        business_object_type["workspace_type_id"] = workspace_type_id

        if not bo_type_name or not bo_type_id:
            self.logger.info(
                "Business object type %sis void (dummy data for workspace types without a business object connection)! Skipping...",
                "for workspace type ID -> {} ".format(workspace_type_id) if workspace_type_id else "",
            )
            continue
        self.logger.info(
            "Processing business object type -> '%s' (%s) with ID -> %s for workspace type with ID -> %d...",
            bo_type_name,
            bo_type,
            bo_type_id,
            workspace_type_id,
        )

        # Get additional information per BO Type (before 25.3 REST API is severly
        # limited) - it does not return Property names from External System
        # and is also missing Business Property Groups. Thus, for older versions
        # we extract that information from the Transport XML files (see else case):
        if otcs_version >= 25.3:
            response = self._otcs.get_business_object_type(type_id=bo_type_id)
            if response is None or not response["results"]:
                self.logger.warning(
                    "Cannot retrieve additional information for business object type -> %s (%s). Skipping...",
                    bo_type,
                    bo_type_id,
                )
                success = False
                continue

            business_properties = response["results"]["GeneralTab"]["data"]["properties"]["fWkspCreationConfig"][
                "propertyMappings"
            ]
            business_object_type["business_properties"] = business_properties

            business_property_groups = response["results"]["GeneralTab"]["data"]["properties"][
                "fWkspCreationConfig"
            ]["propertyGroups"]
            business_object_type["business_properties_groups"] = business_property_groups
        elif not self.extract_properties_from_transport_packages(business_object_type, bo_type_id, bo_type_name):
            success = False
    # end for business_object_type in self._business_object_types

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._business_object_types,
    )

    return self._business_object_types

process_cs_applications(otcs_object, section_name='csApplications')

Process CS applications in payload and install them in Content Server.

The CS Applications need to be installed in all frontend and backends.

Parameters:

Name Type Description Default
otcs_object object

This can either be the OTCS frontend or OTCS backend. If None then the otcs_backend is used.

required
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'csApplications'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_cs_applications")
def process_cs_applications(
    self,
    otcs_object: OTCS,
    section_name: str = "csApplications",
) -> bool:
    """Process CS applications in payload and install them in Content Server.

    The CS Applications need to be installed in all frontend and backends.

    Args:
        otcs_object (object):
            This can either be the OTCS frontend or OTCS backend. If None
            then the otcs_backend is used.
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._cs_applications:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # OTCS backend is the default:
    if not otcs_object:
        otcs_object = self._otcs_backend

    for cs_application in self._cs_applications:
        application_name = cs_application.get("name")
        if not application_name:
            self.logger.error("Missing CS application name in payload! Skipping...")
            continue

        # Check if CS Application has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not cs_application.get("enabled", True):
            self.logger.info(
                "Payload for CS Application -> '%s' is disabled. Skipping...",
                application_name,
            )
            continue

        application_description = cs_application.get("description", "")

        self.logger.info(
            "Install CS Application -> '%s'%s...",
            application_name,
            " ({})".format(application_description) if application_description else "",
        )
        response = otcs_object.install_cs_application(
            application_name=application_name,
        )
        if response is None:
            self.logger.error(
                "Failed to install CS Application -> '%s'!",
                application_name,
            )
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._cs_applications,
    )

    return success

process_docgen_settings(section_name='docgenSettings')

Process OTPD settings in payload and configure them in OTPD.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'docgenSettings'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_docgen_settings")
def process_docgen_settings(self, section_name: str = "docgenSettings") -> bool:
    """Process OTPD settings in payload and configure them in OTPD.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._docgen_settings:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for docgen_setting in self._docgen_settings:
        # Check if the setting has a proper name in the payload:
        setting_name = docgen_setting.get("name")
        if not setting_name:
            self.logger.error(
                "OTPD document generation setting needs a name in payload! Skipping...",
            )
            success = False
            continue

        # Check if the document geneneration has been explicitly disabled in payload
        # (enabled = false). In this case we skip the payload element:
        if not docgen_setting.get("enabled", True):
            self.logger.info(
                "Payload for OTPD setting -> '%s' is disabled. Skipping...",
                setting_name,
            )
            continue

        setting_value = docgen_setting.get("value")
        if not setting_value:
            self.logger.error(
                "OTPD setting -> '%s' needs a value in payload! Skipping...",
                setting_name,
            )
            continue

        tenant_name = docgen_setting.get("tenant", None)
        description = docgen_setting.get("description", "")

        # Apply the setting to PowerDocs (OTPD):
        response = self._otpd.apply_setting(
            setting_name=setting_name,
            setting_value=setting_value,
            tenant_name=tenant_name,
        )
        if response:
            self.logger.info(
                "Added OTPD setting -> '%s' with value -> '%s'%s%s.",
                setting_name,
                setting_value,
                " for tenant -> '{}'".format(tenant_name) if tenant_name else "",
                " ({})".format(description) if description else "",
            )
        else:
            self.logger.error(
                "Failed to configure OTPD setting -> '%s' with value -> '%s'%s%s!",
                setting_name,
                setting_value,
                " for tenant -> '{}'".format(tenant_name) if tenant_name else "",
                " ({})".format(description) if description else "",
            )
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._docgen_settings,
    )

    return success

process_document_generators(section_name='documentGenerators')

Generate documents for a defined workspace type based on template.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'documentGenerators'

Returns:

Name Type Description
bool bool

, True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_document_generators")
def process_document_generators(
    self,
    section_name: str = "documentGenerators",
) -> bool:
    """Generate documents for a defined workspace type based on template.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:,
            True if payload has been processed without errors, False otherwise.

    """

    if not self._doc_generators:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # save admin credentials for later switch back to admin user:
    #        admin_credentials = self._otcs.credentials()
    authenticated_user = "admin"

    for doc_generator in self._doc_generators:
        if "workspace_type" not in doc_generator:
            self.logger.error(
                "To generate documents for workspaces the workspace type needs to be specified in the payload! Skipping to next document generator...",
            )
            success = False
            continue
        workspace_type = doc_generator["workspace_type"]

        self._log_header_callback(
            text="Process Document Generator for workspace type -> '{}'".format(workspace_type),
            char="-",
        )

        # Check if doc generator has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not doc_generator.get("enabled", True):
            self.logger.info(
                "Payload for document generator of workspace type -> '%s' is disabled. Skipping...",
                workspace_type,
            )
            continue

        template_path = doc_generator.get("template_path")
        if not template_path:
            self.logger.error(
                "To generate documents for workspaces of type -> '%s' the path to the document template needs to be specified in the payload! Skipping to next document generator...",
                workspace_type,
            )
            success = False
            continue

        template = self._otcs.get_node_by_volume_and_path(
            volume_type=self._otcs.VOLUME_TYPE_CONTENT_SERVER_DOCUMENT_TEMPLATES,
            path=template_path,
        )
        if not template:
            self.logger.error(
                "Cannot find document template in path -> %s. Skipping to next document generator...",
                template_path,
            )
            success = False
            continue
        template_id = self._otcs.get_result_value(response=template, key="id")
        template_name = self._otcs.get_result_value(response=template, key="name")

        if "classification_path" not in doc_generator:
            self.logger.error(
                "To generate documents for workspaces of type -> '%s' the path to the document classification needs to be specified in the payload! Skipping to next document generator...",
                workspace_type,
            )
            success = False
            continue
        classification_path = doc_generator["classification_path"]
        classification = self._otcs.get_node_by_volume_and_path(
            volume_type=self._otcs.VOLUME_TYPE_CLASSIFICATION_VOLUME,
            path=classification_path,
        )
        if not classification:
            self.logger.error(
                "Cannot find document classification in path -> %s. Skipping to next document generator...",
                classification_path,
            )
            success = False
            continue
        classification_id = self._otcs.get_result_value(
            response=classification,
            key="id",
        )

        # "category_name" is optional. But if it is specified
        # then also "attributes" needs to be specified:
        if "category_name" not in doc_generator:
            self.logger.info(
                "No metadata (category name) specified in the payload for this document generator.",
            )
            category_name = ""
            attributes = {}
            category_data = {}
        else:
            category_name = doc_generator["category_name"]
            if "attributes" not in doc_generator:
                self.logger.error(
                    "To generate documents for workspaces of type -> '%s' with metadata, the attributes needs to be specified in the payload! Skipping to next document generator...",
                    workspace_type,
                )
                success = False
                continue
            attributes = doc_generator["attributes"]

            # The following method returns two values: the category ID and
            # a dict of the attributes. If the category is not found
            # on the document template it returns -1 for the category ID
            # and an empty dict for the attribute definitions:
            (
                category_id,
                attribute_definitions,
            ) = self._otcs.get_node_category_definition(
                node_id=template_id,
                category_name=category_name,
            )
            if category_id == -1:
                self.logger.error(
                    "The document template -> '%s' does not have the specified category -> %s. Skipping to next document generator...",
                    template_name,
                    category_name,
                )
                success = False
                continue

            category_data = {str(category_id): {}}

            # now we fill the prepared (but empty) category_data
            # with the actual attribute values from the payload:
            for attribute in attributes:
                attribute_name = attribute["name"]
                attribute_value = attribute["value"]
                attribute_type = attribute_definitions[attribute_name]["type"]
                attribute_id = attribute_definitions[attribute_name]["id"]

                # Special treatment for type user: determine the ID for the login name.
                # the ID is the actual value we have to put in the attribute:
                if attribute_type == "user":
                    user = self._otcs.get_user(
                        name=attribute_value,
                        show_error=True,
                    )
                    user_id = self._otcs.get_result_value(response=user, key="id")
                    if not user_id:
                        self.logger.error(
                            "Cannot find user with login name -> '%s'. Skipping...",
                            attribute_value,
                        )
                        success = False
                        continue
                    attribute_value = user_id
                category_data[str(category_id)][attribute_id] = attribute_value

        if "workspace_folder_path" not in doc_generator:
            self.logger.info(
                "No workspace folder path defined for workspaces of type -> '%s'. Documents will be stored in workspace root.",
                workspace_type,
            )
            workspace_folder_path = []
        else:
            workspace_folder_path = doc_generator["workspace_folder_path"]

        if "exec_as_user" in doc_generator:
            exec_as_user = doc_generator["exec_as_user"]

            # Find the user in the users payload:
            exec_user = next(
                (item for item in self._users if item["name"] == exec_as_user),
                None,
            )
            # Have we found the user in the payload?
            if exec_user is not None:
                self.logger.info(
                    "Executing document generator with user -> '%s'...",
                    exec_as_user,
                )
                # Impersonate as the user specified in the payload:
                self.logger.info("Impersonate user -> '%s'...", exec_as_user)
                result = self.start_impersonation(username=exec_as_user)
                if not result:
                    self.logger.error(
                        "Couldn't impersonate user -> '%s'!",
                        exec_as_user,
                    )
                    continue
                admin_context = False
                authenticated_user = exec_as_user
            else:
                self.logger.error(
                    "Cannot find user with login name -> '%s' for executing document generator. Executing as admin...",
                    exec_as_user,
                )
                admin_context = True
                success = False
        else:
            admin_context = True
            exec_as_user = "admin"

        if admin_context and authenticated_user != "admin":
            # Impersonate as the admin user:
            self.logger.info(
                "Impersonate as admin user -> '%s'...",
                self._otcs.credentials()["username"],
            )
            # Stop the impersonation as a user:
            result = self.stop_impersonation()
            authenticated_user = "admin"

        if category_data:
            self.logger.info(
                "Generate documents for workspace type -> '%s' based on template -> '%s' with metadata -> %s...",
                workspace_type,
                template_name,
                category_data,
            )
        else:
            self.logger.info(
                "Generate documents for workspace type -> '%s' based on template -> '%s' without metadata...",
                workspace_type,
                template_name,
            )

        # Find the workspace type with the name given in the _workspace_types
        # datastructure that has been generated by process_workspace_types() method before:
        workspace_type_id = next(
            (item["id"] for item in self._workspace_types if item["name"] == workspace_type),
            None,
        )

        # The workspace type name is used as a "starts with" search on the
        # workspace type name. This can cause issues, so we prefer to use the type ID
        # if we have it. Otherwise the REST API prefers the name over the type:
        workspace_instances = self._otcs.get_workspace_instances_iterator(
            type_name=workspace_type if not workspace_type_id else None,
            type_id=workspace_type_id,
        )
        for workspace_instance in workspace_instances:
            workspace = workspace_instance.get("data").get("properties")
            workspace_id = workspace["id"]
            workspace_name = workspace["name"]
            if workspace_folder_path:
                workspace_folder = self._otcs.get_node_by_workspace_and_path(
                    workspace_id=workspace_id,
                    path=workspace_folder_path,
                )
                if workspace_folder:
                    workspace_folder_id = self._otcs.get_result_value(
                        response=workspace_folder,
                        key="id",
                    )
                else:
                    # If the workspace template is not matching
                    # the path we may have an error here. Then
                    # we fall back to workspace root level.
                    self.logger.warning(
                        "Folder path does not exist in workspace -> '%s'. Using workspace root level instead...",
                        workspace_name,
                    )
                    workspace_folder_id = workspace_id
            else:
                workspace_folder_id = workspace_id

            document_name = workspace_name + " - " + template_name
            self.logger.info("Generate document -> '%s'...", document_name)

            response = self._otcs.check_node_name(
                parent_id=int(workspace_folder_id),
                node_name=document_name,
            )
            if response["results"]:
                self.logger.warning(
                    "Node -> '%s' does already exist in workspace folder with ID -> %s",
                    document_name,
                    workspace_folder_id,
                )
                continue
            response = self._otcs.create_document_from_template(
                template_id=int(template_id),
                parent_id=int(workspace_folder_id),
                classification_id=int(classification_id),
                category_data=category_data,
                doc_name=document_name,
                doc_description="This document has been auto-generated by Terrarium",
            )
            if not response:
                self.logger.error(
                    "Failed to generate document -> '%s' in workspace -> '%s' (%s) as user -> '%s'!",
                    document_name,
                    workspace_name,
                    workspace_id,
                    exec_as_user,
                )
                success = False
            else:
                self.logger.info(
                    "Successfully generated document -> '%s' in workspace -> '%s' (%s) as user -> '%s'.",
                    document_name,
                    workspace_name,
                    workspace_id,
                    exec_as_user,
                )

    if authenticated_user != "admin":
        # Impersonate as the admin user:
        self.logger.info(
            "Impersonate as admin user -> '%s'...",
            self._otcs.credentials()["username"],
        )
        result = self.stop_impersonation()
        if not result:
            self.logger.error(
                "Couldn't impersonate as admin user -> '%s'!",
                self._otcs.credentials()["username"],
            )
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._doc_generators,
    )

    return success

process_embeddings(section_name='embeddings')

Process additional Aviator embeddings.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'embeddings'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_embeddings")
def process_embeddings(self, section_name: str = "embeddings") -> bool:
    """Process additional Aviator embeddings.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._embeddings:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for embedding in self._embeddings:
        # Check if item has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not embedding.get("enabled", True):
            self.logger.info(
                "Payload for embedding -> '%s' is disabled for FEME. Skipping...",
                str(embedding),
            )
            continue

        # Backwards-compatibility for old syntax "documents":
        if "document_metadata" not in embedding:
            document_metadata = bool(embedding.get("documents", False))
        else:
            document_metadata = bool(embedding.get("document_metadata", False))

        # Backwards-compatibility for old syntax "workspaces":
        if "workspace_metadata" not in embedding:
            workspace_metadata = bool(embedding.get("workspaces", False))
        else:
            workspace_metadata = bool(embedding.get("workspace_metadata", False))

        wait_for_completion = bool(embedding.get("wait_for_completion", True))
        crawl = bool(embedding.get("crawl", False))
        images = bool(embedding.get("images", False))

        # We support 3 ways to determine the node ID(s):
        # 1. Node ID is specified directly in the payload using 'id = "..."'
        # 2. Node is is specified via a nickname of the node using 'nickname = "..."'
        # 3. Nodes are specified via the name of a workspace type. In this
        #    case all nodes of workspace instances are considered.

        node_id = None

        if embedding.get("id", None) is None and "nickname" in embedding:
            response = self._otcs.get_node_from_nickname(
                nickname=embedding.get("nickname"),
            )
            node_id = self._otcs.get_result_value(response=response, key="id")

        else:
            node_id = embedding.get("id", None)
            response = None

        if node_id:
            result = self._otcs.aviator_embed_metadata(
                node_id=node_id,
                node=response,
                wait_for_completion=wait_for_completion,
                crawl=crawl,
                document_metadata=document_metadata,
                workspace_metadata=workspace_metadata,
                images=images,
            )
            if not result:
                success = False
        elif "workspace_types" in embedding:
            workspace_types = embedding["workspace_types"]
            # Handle the case of a single workspace type name:
            if isinstance(workspace_types, str):
                workspace_types = [workspace_types]
            for workspace_type_name in workspace_types:
                self.logger.info(
                    "Embedding metadata of workspace instances with type -> '%s'...", workspace_type_name
                )
                workspace_instances = self._otcs.get_workspace_instances_iterator(type_name=workspace_type_name)
                for workspace in workspace_instances:
                    properties = workspace.get("data").get("properties")
                    self.logger.info(
                        "Embedding metadata of workspace instance -> '%s' (%s)...",
                        properties["name"],
                        properties["id"],
                    )
                    result = self._otcs.aviator_embed_metadata(
                        node_id=None,
                        node=workspace,
                        wait_for_completion=wait_for_completion,
                        crawl=crawl,
                        document_metadata=document_metadata,
                        workspace_metadata=workspace_metadata,
                        images=images,
                    )
                    if not result:
                        success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._embeddings,
    )

    return success

process_exec_commands(section_name='execCommands')

Process Payload items to execute a command.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'execCommands'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_exec_commands")
def process_exec_commands(self, section_name: str = "execCommands") -> bool:
    """Process Payload items to execute a command.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise

    """

    if not self._exec_commands:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for exec_command in self._exec_commands:
        if "command" not in exec_command or not exec_command.get("command"):
            self.logger.error(
                "Command is not specified! It needs to be a non-empty list! Skipping to next command...",
            )
            success = False

            continue
        command = exec_command["command"]

        # Check if exec command has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not exec_command.get("enabled", True):
            self.logger.info(
                "Payload for exec command is disabled. Skipping...",
            )
            continue

        description = exec_command.get("description")
        if not description:
            self.logger.info(
                "Executing command -> %s...",
                command,
            )
        else:
            self.logger.info(
                "Executing command -> %s (%s)...",
                command,
                description,
            )

        try:
            output = subprocess.run(command, stdout=subprocess.PIPE, check=False)
            result = output.stdout.decode("utf-8")
        except Exception:
            self.logger.error(
                "Execution of command -> %s failed!",
                command,
            )
            success = False

        # we need to differentiate 3 cases here:
        # 1. result = None is returned - this is an error (exception)
        # 2. result is empty string - this is OK
        # 3. result is a non-empty string - this is OK - print it to log
        if result is None:
            self.logger.error(
                "Execution of command -> %s failed",
                command,
            )
            success = False
        elif result != "":
            self.logger.info(
                "Execution of command -> %s returned result -> %s.",
                command,
                result,
            )
        else:
            # It is not an error if no result is returned. It depends on the nature of the command
            # if a result is written to stdout or stderr.
            self.logger.info(
                "Execution of command -> %s did not return a result.",
                command,
            )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._exec_pod_commands,
    )

    return success

process_exec_database_commands(section_name='execDatabaseCommands')

Process commands that should be executed in the PostgreSQL database.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'execDatabaseCommands'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_exec_database_commands")
def process_exec_database_commands(self, section_name: str = "execDatabaseCommands") -> bool:
    """Process commands that should be executed in the PostgreSQL database.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not self._exec_database_commands:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    if not psycopg_installed:
        self.logger.warning("Python module 'psycopg' not installed. Cannot execute database commands! Skipping...")
        return False

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for database_command_set in self._exec_database_commands:
        # Check if exec pod command has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not database_command_set.get("enabled", True):
            self.logger.info(
                "Payload for database command set is disabled. Skipping...",
            )
            continue

        db_connection = database_command_set.get("db_connection")
        if not db_connection:
            self.logger.error(
                "To execute a command in a database the connection information needs to be specified in the payload! Skipping to next database command set...",
            )
            success = False
            continue

        db_name = db_connection.get("db_name", None)
        if not db_name:
            self.logger.error(
                "Database connection information is missing the database name! Skipping to next database command set...",
            )
            success = False
            continue
        db_hostname = db_connection.get("db_hostname", None)
        if not db_hostname:
            self.logger.error(
                "Database connection information is missing the database hostname! Skipping to next database command set...",
            )
            success = False
            continue
        db_port = int(db_connection.get("db_port", 5432))
        db_username = db_connection.get("db_username", None)
        if not db_username:
            self.logger.error(
                "Database connection information is missing the database username! Skipping to next database command set...",
            )
            success = False
            continue
        db_password = db_connection.get("db_password", None)
        if not db_password:
            self.logger.error(
                "Database connection information is missing the database password! Skipping to next database command set...",
            )
            success = False
            continue

        connect_string = "dbname={} user={} password={} host={} port={}".format(
            db_name, db_username, db_password, db_hostname, db_port
        )

        db_commands = database_command_set.get("db_commands", [])

        # TODO: Add support for sql file

        db_connection = None  # Predefine for safe access in except
        allowed_verbs = {"SELECT", "INSERT", "UPDATE", "CREATE"}

        try:
            # Using a context managers (with ...) for automatic resource management:
            with psycopg.connect(connect_string) as db_connection:
                self.logger.info(
                    "Connected to database -> '%s' (%s) with user -> '%s'...", db_name, db_hostname, db_username
                )
                with db_connection.cursor() as cursor:
                    for db_command in db_commands:
                        cmd = db_command.get("command", None)
                        if not cmd:
                            self.logger.warning(
                                "Cannot execute database command without SQL statement. Skipping..."
                            )
                            continue
                        params = db_command.get("params", None)
                        if params is not None and isinstance(params, (list, tuple)):
                            self.logger.error(
                                "Database parameters -> %s must be given as a list or tuple!", str(params)
                            )
                            continue
                        # Get the command verb (like "SELECT", "CREATE")
                        verb = cmd.strip().split()[0].upper()
                        if verb not in allowed_verbs:
                            self.logger.error("Database command -> '%s' is not allowed!", verb)
                            continue
                        if params:
                            self.logger.info(
                                "Execute database command -> '%s' with parameters -> %s...", cmd, str(params)
                            )
                        else:
                            self.logger.info("Execute database command -> '%s' without parameters...", cmd)
                        cursor.execute(cmd, params)
                        if verb == "SELECT":
                            response = cursor.fetchall()
                            self.logger.debug("Database response -> '%s'", response)
                db_connection.commit()
        except psycopg.Error as e:
            success = False
            self.logger.error("Database error -> %s", e)
            if db_connection is not None:
                db_connection.rollback()

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._exec_database_commands,
    )

    return success

process_exec_pod_commands(section_name='execPodCommands')

Process commands that should be executed in the Kubernetes pods.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'execPodCommands'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_exec_pod_commands")
def process_exec_pod_commands(self, section_name: str = "execPodCommands") -> bool:
    """Process commands that should be executed in the Kubernetes pods.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise

    """

    self.logger.warning("execPodCommand is deprecated - use kubernetes section with action 'execPodCommands'!")

    if not isinstance(self._k8s, K8s):
        self.logger.error(
            "K8s not setup properly -> Skipping payload section -> '%s'...",
            section_name,
        )
        return False

    if not self._exec_pod_commands:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for exec_pod_command in self._exec_pod_commands:
        pod_name = exec_pod_command.get("pod_name")
        if not pod_name:
            self.logger.error(
                "To execute a command in a pod the pod name needs to be specified in the payload! Skipping to next pod command...",
            )
            success = False
            continue

        command = exec_pod_command.get("command", [])
        if not command:
            self.logger.error(
                "Command is not specified for pod -> %s! It needs to be a non-empty list! Skipping to next pod command...",
                pod_name,
            )
            success = False
            continue

        container = exec_pod_command.get("container", None)
        timeout = int(exec_pod_command.get("timeout", 60))

        # Check if exec pod command has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not exec_pod_command.get("enabled", True):
            self.logger.info(
                "Payload for exec pod command in pod -> '%s' is disabled. Skipping...",
                pod_name,
            )
            continue

        if "description" not in exec_pod_command:
            self.logger.info(
                "Executing command -> %s in pod -> '%s'...",
                command,
                pod_name,
            )
        else:
            description = exec_pod_command["description"]
            self.logger.info(
                "Executing command -> %s in pod -> '%s' (%s)...",
                command,
                pod_name,
                description,
            )

        if "interactive" not in exec_pod_command or exec_pod_command["interactive"] is False:
            result = self._k8s.exec_pod_command(
                pod_name=pod_name, command=command, container=container, timeout=timeout
            )
        else:
            result = self._k8s.exec_pod_command_interactive(
                pod_name=pod_name,
                commands=command,
                timeout=timeout,
            )

        # we need to differentiate 3 cases here:
        # 1. result = None is returned - this is an error (exception)
        # 2. result is empty string - this is OK
        # 3. result is a non-empty string - this is OK - print it to log
        if result is None:
            self.logger.error(
                "Execution of command -> %s in pod -> '%s' failed",
                command,
                pod_name,
            )
            success = False
        elif result != "":
            self.logger.info(
                "Execution of command -> %s in pod -> '%s' returned result -> %s.",
                command,
                pod_name,
                result,
            )
        else:
            # It is not an error if no result is returned. It depends on the nature of the command
            # if a result is written to stdout or stderr.
            self.logger.info(
                "Execution of command -> %s in pod -> '%s' did not return a result.",
                command,
                pod_name,
            )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._exec_pod_commands,
    )

    return success

process_external_systems(section_name='externalSystems')

Process external systems in payload and create them in Content Server.

The payload section is a list of dicts (each representing one external
system) with these items:
{
    enabled: True or False to enable or disable the payload item
    external_system_name: Name of the external systen.
    external_system_type: Type of the external system.
                          Possible values are
                          * SAP
                          * SuccessFactors
                          * Salesforce
                          * Guidewire
                          * AppWorks Platform
                          * Business Scenario Sample
    base_url: Base URL of the external system
    as_url: Application Server URL of the external system
    username: (Technical) User Name for the connection
    password: Passord of the (technical) user
    oauth_client_id: OAuth client ID
    oauth_client_secret: OAuth client secret
    archive_logical_name: Logical name of Archive for SAP
    archive_certificate_file: Path and filename to certificate file.
                              This file is inside the customizer
                              pof file system.
    skip_connection_test: Should we skip the connection test for this
                          external system?
}
If OAuth Client ID and Client Secret are provided then username
and password are no longer used.

In the payload for SAP external systems there are additional
items "client", "destination" that are processed by init_sap()

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'externalSystems'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Side Effects
  • based on system_type different other settings in the dict are set
  • reachability is tested and a flag is set in the payload dict
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_external_systems")
def process_external_systems(self, section_name: str = "externalSystems") -> bool:
    """Process external systems in payload and create them in Content Server.

        The payload section is a list of dicts (each representing one external
        system) with these items:
        {
            enabled: True or False to enable or disable the payload item
            external_system_name: Name of the external systen.
            external_system_type: Type of the external system.
                                  Possible values are
                                  * SAP
                                  * SuccessFactors
                                  * Salesforce
                                  * Guidewire
                                  * AppWorks Platform
                                  * Business Scenario Sample
            base_url: Base URL of the external system
            as_url: Application Server URL of the external system
            username: (Technical) User Name for the connection
            password: Passord of the (technical) user
            oauth_client_id: OAuth client ID
            oauth_client_secret: OAuth client secret
            archive_logical_name: Logical name of Archive for SAP
            archive_certificate_file: Path and filename to certificate file.
                                      This file is inside the customizer
                                      pof file system.
            skip_connection_test: Should we skip the connection test for this
                                  external system?
        }
        If OAuth Client ID and Client Secret are provided then username
        and password are no longer used.

        In the payload for SAP external systems there are additional
        items "client", "destination" that are processed by init_sap()

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    Side Effects:
        - based on system_type different other settings in the dict are set
        - reachability is tested and a flag is set in the payload dict

    """

    if not self._external_systems:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # Even if this payload section has been processed successfully before we
    # cannot skip processing it once more to re-initialize self._sap and self._salesforce!

    success: bool = True

    for external_system in self._external_systems:
        #
        # 1: Do sanity checks for the payload:
        #
        if "external_system_name" not in external_system:
            self.logger.error(
                "External System connection needs a logical system name! Skipping to next external system...",
            )
            success = False
            continue
        system_name = external_system["external_system_name"]

        if "external_system_type" not in external_system:
            self.logger.error(
                "External System connection -> '%s' needs a type (SAP, Salesfoce, SuccessFactors, AppWorks Platform)! Skipping...",
                system_name,
            )
            success = False
            continue
        system_type = external_system["external_system_type"]

        self._log_header_callback(
            text="Process External System -> '{}' ({})".format(system_name, system_type),
            char="-",
        )

        # Check if external system has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not external_system.get("enabled", True):
            self.logger.info(
                "Payload for External System -> '%s' (%s) is disabled. Skipping...",
                system_name,
                system_type,
            )
            continue

        """
        Possible Connection Types for external systems:
        "Business Scenario Sample" (Business Scenarios Sample Adapter)
        "ot.sap.c4c.SpiAdapter" (SAP C4C SPI Adapter)
        "ot.sap.c4c.SpiAdapterV2" (C4C SPI Adapter V2)
        "HTTP" (Default WebService Adapter)
        "ot.sap.S4HANAAdapter" (S/4HANA SPI Adapter)
        "SF" (SalesForce Adapter)
        "SFInstance" (SFWebService)
        "GWInstance" (Guidewire Adapter)
        """

        # Set the default settings for the different system types:
        match system_type:
            # Check if we have a SuccessFactors system:
            case "SuccessFactors":
                connection_type = "SFInstance"
                auth_method = "OAUTH"
                username = None
                password = None
            case "SAP":
                connection_type = "HTTP"
                auth_method = "BASIC"
                oauth_client_id = None
                oauth_client_secret = None
            case "Salesforce":
                connection_type = "SF"
                auth_method = "OAUTH"
                username = None
                password = None
            case "Guidewire":
                connection_type = "GWInstance"
                auth_method = "BASIC"
                oauth_client_id = None
                oauth_client_secret = None
            case "AppWorks Platform":
                connection_type = "HTTP"
                auth_method = "BASIC"
                oauth_client_id = None
                oauth_client_secret = None
            case "Business Scenario Sample":
                connection_type = "Business Scenario Sample"
                auth_method = "BASIC"
                oauth_client_id = None
                oauth_client_secret = None
            case _:
                self.logger.error(
                    "Unsupported system_type defined -> '%s'",
                    system_type,
                )
                return False

        base_url = external_system.get("base_url", "")

        if "as_url" not in external_system:
            self.logger.warning(
                "External system connection -> '%s' needs an application server URL! Skipping to next external system...",
                system_name,
            )
            success = False
            continue
        as_url = external_system["as_url"]

        self.logger.info(
            "Processing external system -> '%s' (type -> '%s', connection type -> '%s', endpoint -> '%s')...",
            system_name,
            system_type,
            connection_type,
            as_url,
        )

        skip_connection_test = external_system.get("skip_connection_test", False)
        # If skip_connection_test is not enabled in payload and the external system is
        # not of type 'Business Scenario Sample', we run the external system check:
        if not skip_connection_test and system_type != "Business Scenario Sample":
            # Check if external system is reachable and
            # update the payload dict with a "reachable" key/value pair:
            if not self.check_external_system(external_system=external_system):
                self.logger.warning(
                    "External System connection -> '%s' (%s) is not reachable! Skipping to next external system...",
                    system_name,
                    system_type,
                )
                success = False
                continue
        else:
            self.logger.info(
                "Skipped reachability check for external system -> '%s' (%s)...",
                system_name,
                system_type,
            )

        # Read either username/password (BASIC) or client ID / secret (OAuth)
        match auth_method:
            case "BASIC":
                if "username" not in external_system:
                    self.logger.warning(
                        "External System connection -> '%s' needs a user name for BASIC authentication! Skipping to next external system...",
                        system_name,
                    )
                    continue
                if "password" not in external_system:
                    self.logger.warning(
                        "External System connection -> '%s' needs a password for BASIC authentication! Skipping to next external system...",
                        system_name,
                    )
                    continue
                username = external_system["username"]
                password = external_system["password"]
                oauth_client_id = ""
                oauth_client_secret = ""

            case "OAUTH":
                if "oauth_client_id" not in external_system:
                    self.logger.error(
                        "External System connection -> '%s' is missing OAuth client ID! Skipping to next external system...",
                        system_name,
                    )
                    success = False
                    continue
                if "oauth_client_secret" not in external_system:
                    self.logger.error(
                        "External System connection -> '%s' is missing OAuth client secret! Skipping to next external system...",
                        system_name,
                    )
                    success = False
                    continue
                oauth_client_id = external_system["oauth_client_id"]
                oauth_client_secret = external_system["oauth_client_secret"]
                # For backward compatibility we also read username/password
                # with OAuth settings:
                username = external_system["username"] if external_system.get("username") else None
                password = external_system["password"] if external_system.get("password") else None
            case _:
                self.logger.error(
                    "Unsupported authorization method specified (%s) , Skipping ... ",
                    auth_method,
                )
                return False
        # end match

        # We do this existance test late in this function to make sure the payload
        # datastructure is properly updated for debugging purposes.
        self.logger.info(
            "Test if external system -> '%s' does already exist...",
            system_name,
        )
        if self._otcs.get_external_system_connection(connection_name=system_name):
            self.logger.info(
                "External system connection -> '%s' already exists.",
                system_name,
            )
            # This is for handling re-runs of customizer pod where the transports
            # are skipped and thus self._sap or self._salesforce may not be initialized:
            if system_type == "SAP" and not self._sap:
                self.logger.info(
                    "Re-Initialize SAP connection for external system -> '%s'...",
                    system_name,
                )
                # Initialize SAP object responsible for communication to SAP:
                self._sap = self.init_sap(external_system)
            if system_type == "Salesforce" and not self._salesforce:
                self.logger.info(
                    "Re-Initialize Salesforce connection for external system -> '%s'...",
                    system_name,
                )
                # Initialize Salesforce object responsible for communication to Salesforce:
                self._salesforce = self.init_salesforce(
                    salesforce_external_system=external_system,
                )
            if system_type == "SuccessFactors" and not self._successfactors:
                self.logger.info(
                    "Re-Initialize SuccessFactors connection for external system -> '%s'...",
                    system_name,
                )
                # Initialize SuccessFactors object responsible for communication to SuccessFactors:
                self._successfactors = self.init_successfactors(
                    sucessfactors_external_system=external_system,
                )
            if system_type == "Guidewire" and "policy" in system_name.lower() and not self._guidewire_policy_center:
                self.logger.info(
                    "Re-Initialize Guidewire connection for external system -> '%s'...",
                    system_name,
                )
                # Initialize Guidewire object responsible for communication to Guidewire Policy Center:
                self._guidewire_policy_center = self.init_guidewire(
                    guidewire_external_system=external_system,
                )
            if system_type == "Guidewire" and "claim" in system_name.lower() and not self._guidewire_claims_center:
                self.logger.info(
                    "Re-Initialize Guidewire connection for external system -> '%s'...",
                    system_name,
                )
                # Initialize Guidewire object responsible for communication to Guidewire Claims Center:
                self._guidewire_claims_center = self.init_guidewire(
                    guidewire_external_system=external_system,
                )

            continue

        #
        # Create External System:
        #

        response = self._otcs.add_external_system_connection(
            connection_name=system_name,
            connection_type=connection_type,
            as_url=as_url,
            base_url=base_url,
            username=str(username),
            password=str(password),
            authentication_method=auth_method,
            client_id=oauth_client_id,
            client_secret=oauth_client_secret,
        )
        if response is None:
            self.logger.error(
                "Failed to create external system -> '%s'; type -> '%s'!",
                system_name,
                connection_type,
            )
            success = False
        else:
            self.logger.info(
                "Successfully created external system -> '%s'.",
                system_name,
            )

        #
        # In case of an SAP external system we also initialize the SAP object
        #
        if system_type == "SAP":
            # Initialize SAP object responsible for communication to SAP:
            self._sap = self.init_sap(sap_external_system=external_system)

        #
        # In case of an SuccessFactors external system we also initialize the SuccessFactors object
        #
        if system_type == "SuccessFactors":
            # Initialize SuccessFactors object responsible for communication to SuccessFactors:
            self._successfactors = self.init_successfactors(
                sucessfactors_external_system=external_system,
            )

        #
        # In case of an Salesforce external system we also initialize the Salesforce object
        #
        if system_type == "Salesforce":
            # Initialize Salesforce object responsible for communication to Salesforce:
            self._salesforce = self.init_salesforce(
                salesforce_external_system=external_system,
            )

        #
        # In case of an Guidewire external system we also initialize the Guidewire objects:
        #
        if system_type == "Guidewire":
            if "claim" in system_name.lower():
                # Initialize Guidewire Claim Center object:
                self._guidewire_claims_center = self.init_guidewire(
                    guidewire_external_system=external_system,
                )
            elif "policy" in system_name.lower():
                # Initialize Guidewire Policy Center object:
                self._guidewire_policy_center = self.init_guidewire(
                    guidewire_external_system=external_system,
                )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._external_systems,
    )

    return success

process_group_placeholders()

Replace a group placeholder (sourrounded by %%...%%) with the actual ID of the Content Server group.

For this we prepare a lookup dict. The dict self._placeholder_values already includes lookups for the OTCS and OTAWP OTDS resource IDs (see main.py)

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_group_placeholders")
def process_group_placeholders(self) -> None:
    """Replace a group placeholder (sourrounded by %%...%%) with the actual ID of the Content Server group.

    For this we prepare a lookup dict. The dict self._placeholder_values already includes
    lookups for the OTCS and OTAWP OTDS resource IDs (see main.py)
    """

    for group in self._groups:
        group_name = group.get("name")
        if not group_name:
            self.logger.error(
                "Group needs a name for placeholder definition in payload! Skipping...",
            )
            continue

        # Check if group has been explicitly disabled in payload
        # (enabled = false). In this case we skip the payload element:
        if not group.get("enabled", True):
            self.logger.info(
                "Payload for group -> '%s' is disabled. Skipping...",
                group_name,
            )
            continue

        # Now we determine the ID. Either it is in the payload section from
        # the current customizer run or we try to look it up in the system.
        # The latter case may happen if the customizer pod got restarted.
        group_id = self.determine_group_id(group=group)
        if not group_id:
            self.logger.warning(
                "Group needs an ID for placeholder definition. Skipping...",
            )
            continue

        # Add Group with its ID to the dict self._placeholder_values:
        self._placeholder_values["OTCS_GROUP_ID_" + group_name.upper().replace(" & ", "_").replace(" ", "_")] = str(
            group_id,
        )

    self.logger.debug(
        "Placeholder values after group processing = %s",
        self._placeholder_values,
    )

process_groups(section_name='groups')

Process groups in payload and create them in Content Server.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'groups'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Side Effects

The group items are modified by adding an "id" dict element that includes the technical ID of the group in Content Server

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_groups")
def process_groups(self, section_name: str = "groups") -> bool:
    """Process groups in payload and create them in Content Server.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    Side Effects:
        The group items are modified by adding an "id" dict element that
        includes the technical ID of the group in Content Server

    """

    if not self._groups:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # First run through groups: create all groups in payload
    # and store the IDs of the created groups:
    for group in self._groups:
        group_name = group.get("name")
        if not group_name:
            self.logger.error("Group needs a name inb payload! Skipping...")
            success = False
            continue

        # Check if group has been explicitly disabled in payload
        # (enabled = false). In this case we skip the payload element:
        if not group.get("enabled", True):
            self.logger.info(
                "Payload for group -> '%s' is disabled. Skipping...",
                group_name,
            )
            continue

        # Check if the group does already exist (e.g. if job is restarted)
        group_id = self.determine_group_id(group=group)
        if group_id:
            self.logger.info(
                "Found existing group -> '%s' (%s). Skipping to next group...",
                group_name,
                group_id,
            )
            continue

        # Now we know it is a new group...
        new_group = self._otcs.add_group(name=group_name)
        if new_group:
            new_group_id = self._otcs.get_result_value(response=new_group, key="id")
            self.logger.info(
                "Successfully created group -> '%s' (%s).",
                group_name,
                new_group_id,
            )
            # Remember the OTCS group ID in the payload structure:
            group["id"] = new_group_id
        else:
            self.logger.error("Failed to create group -> '%s'!", group_name)
            success = False
            continue

        # Assign usage privileges to the new group:
        usage_privileges = group.get("usage_privileges", [])
        for usage_privilege in usage_privileges:
            response = self._otcs.assign_usage_privilege(
                usage_privilege=usage_privilege,
                member_id=new_group_id,
            )
            if response:
                self.logger.info(
                    "Successfully assigned usage privilege -> '%s' to group -> '%s' (%s).",
                    usage_privilege,
                    group_name,
                    new_group_id,
                )
            else:
                self.logger.info(
                    "Failed to assign usage privilege -> '%s' to group -> '%s' (%s)!",
                    usage_privilege,
                    group_name,
                    new_group_id,
                )

        # Assign usage privileges to the new group:
        object_privileges = group.get("object_privileges", [])
        for object_type in object_privileges:
            response = self._otcs.assign_object_privilege(
                object_type=object_type,
                member_id=new_group_id,
            )
            if response:
                self.logger.info(
                    "Successfully assigned object privilege -> '%s' to group -> '%s' (%s).",
                    object_type,
                    group_name,
                    new_group_id,
                )
            else:
                self.logger.info(
                    "Failed to assign object privilege -> '%s' to group -> '%s' (%s)!",
                    object_type,
                    group_name,
                    new_group_id,
                )

    # end for group in self._groups: (first run)

    # Second run through groups: create all group memberships
    # (nested groups) based on the IDs created in first run:
    for group in self._groups:
        group_name = group.get("name")
        group_id = group.get("id")  # this should have been set in the first loop
        if not group_id:
            self.logger.error(
                "Group -> '%s' does not have an ID! Creation may have failed before. Skipping...", group_name
            )
            success = False
            continue
        parent_group_names = group.get("parent_groups", [])
        for parent_group_name in parent_group_names:
            # First, try to find parent group in payload by parent group name:
            parent_group = next(
                (item for item in self._groups if item["name"] == parent_group_name),
                None,
            )
            if parent_group is None:
                # If this didn't work, try to get the parent group from OTCS. This covers
                # cases where the parent group is system-generated or part
                # of a former payload processing run (or part of another payload):
                parent_group = self._otcs.get_group(name=parent_group_name)
                parent_group_id = self._otcs.get_result_value(
                    response=parent_group,
                    key="id",
                )
                if not parent_group_id:
                    self.logger.error(
                        "Parent group -> '%s' not found! Skipping...",
                        parent_group_name,
                    )
                    success = False
                    continue
            elif "id" not in parent_group:
                self.logger.error(
                    "Parent group -> '%s' does not have an ID! Cannot establish group nesting. Skipping...",
                    parent_group["name"],
                )
                success = False
                continue
            else:  # we can read the ID from the
                parent_group_id = parent_group["id"]

            # retrieve all members of the parent group (1 = get only groups)
            # to check if the current group is already a member in the parent group:
            members = self._otcs.get_group_members(
                group=parent_group_id,
                member_type=1,
            )
            if self._otcs.exist_result_item(
                response=members,
                key="id",
                value=group["id"],
            ):
                self.logger.info(
                    "Group -> '%s' (%s) is already a member of parent group -> '%s' (%s). Skipping to next parent group...",
                    group_name,
                    group_id,
                    parent_group_name,
                    parent_group_id,
                )
            else:
                self.logger.info(
                    "Add group -> '%s' (%s) to parent group -> '%s' (%s)...",
                    group_name,
                    group_id,
                    parent_group_name,
                    parent_group_id,
                )
                self._otcs.add_group_member(
                    member_id=group["id"],
                    group_id=parent_group_id,
                )
        # end for parent_group_name in parent_group_names:

        # Assign application roles to the new group:
        application_roles = group.get("application_roles", [])
        if application_roles:
            self.logger.info(
                "Group -> '%s' has application roles -> %s. Assigning...", group_name, str(application_roles)
            )
        for role in application_roles:
            group_partition = self._otcs.config()["partition"]
            if not group_partition:
                self.logger.error("Group partition not found! Skipping application role -> %s...", str(role))
                success = False
                continue

            # Split role at the @ sign to identify Partitions
            role_parts = role.split("@", 1)
            role_name = role_parts[0]
            role_partition = role_parts[1] if len(role_parts) > 1 else "OAuthClients"

            # This is on OTDS method (not OTCS) thuis the group ID is the group name!
            response = self._otds.assign_group_to_application_role(
                group_id=group_name,
                group_partition=group_partition,
                role_name=role_name,
                role_partition=role_partition,
            )

            if response:
                self.logger.info(
                    "Successfully assigned application role -> '%s' in partition -> '%s' to group -> '%s' (%s) in partition -> '%s'.",
                    role_name,
                    role_partition,
                    group_name,
                    group_id,
                    group_partition,
                )
            else:
                self.logger.info(
                    "Failed to assign application role -> '%s' in partition -> %s to group -> '%s' (%s) in partition -> '%s'!",
                    role_name,
                    role_partition,
                    group_name,
                    group_id,
                    group_partition,
                )
        # end for role in application_roles:
    # end for group in self._groups: (second run)

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._groups,
    )

    return success

process_groups_core_share(section_name='groupsCoreShare')

Process groups in payload and create them in Core Share.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'groupsCoreShare'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_groups_core_share")
def process_groups_core_share(self, section_name: str = "groupsCoreShare") -> bool:
    """Process groups in payload and create them in Core Share.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not isinstance(self._core_share, CoreShare):
        self.logger.error(
            "Core Share connection not setup properly. Skipping payload section '%s'...",
            section_name,
        )
        return False

    if not self._groups:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # Create all groups specified in payload
    # and store the IDs of the created Core Share groups:
    for group in self._groups:
        group_name = group.get("name")
        if not group_name:
            self.logger.error("Group needs a name. Skipping...")
            success = False
            continue

        # Check if group has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not group.get("enabled", True):
            self.logger.info(
                "Payload for group -> '%s' is disabled. Skipping...",
                group_name,
            )
            continue

        # Core Share is disabled per default. There needs to be "enable_core_share" in payload
        # and it needs to be True:
        if not group.get("enable_core_share", False):
            self.logger.info(
                "Group -> '%s' is not enabled for Core Share. Skipping...",
                group_name,
            )
            continue

        # Check if the group does already exist (e.g. if job is restarted)
        core_share_group = self._core_share.get_group_by_name(name=group_name)
        core_share_group_id = self._core_share.get_result_value(
            response=core_share_group,
            key="id",
        )
        if core_share_group_id:
            self.logger.info(
                "Found existing Core Share group -> '%s' (%s). Just do cleanup of potential left-overs...",
                group_name,
                core_share_group_id,
            )
            # Write Core Share group ID back into the payload (for the success file)
            group["core_share_id"] = core_share_group_id

            # For existing users we want to cleanup possible left-overs form old deployments
            self.logger.info(
                "Cleanup existing file shares of Core Share group -> '%s' (%s)...",
                group_name,
                core_share_group_id,
            )
            response = self._core_share.cleanup_group_shares(
                group_id=core_share_group_id,
            )
            if not response:
                self.logger.error("Failed to cleanup group shares!")

            continue

        self.logger.info(
            "Creating a new Core Share group -> '%s'...",
            group_name,
        )

        # Now we know it is a new group...
        new_group = self._core_share.add_group(group_name=group_name)
        new_group_id = self._core_share.get_result_value(
            response=new_group,
            key="id",
        )
        if new_group_id:
            # Store the Microsoft 365 group ID in payload:
            group["core_share_id"] = new_group_id
            self.logger.info(
                "Successfully created Core Share group -> '%s' with ID -> %s.",
                group_name,
                new_group_id,
            )
        else:
            self.logger.error(
                "Failed to create Core Share group -> %s!",
                group_name,
            )
            success = False

    # Core Share groups cannot be nested. So we are fone here.

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._groups,
    )

    return success

process_groups_m365(section_name='groupsM365')

Process groups in payload and create them in Microsoft 365.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'groupsM365'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_groups_m365")
def process_groups_m365(self, section_name: str = "groupsM365") -> bool:
    """Process groups in payload and create them in Microsoft 365.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not isinstance(self._m365, M365):
        self.logger.error(
            "Microsoft 365 connection not setup properly. Skipping payload section '%s'...",
            section_name,
        )
        return False

    if not self._groups:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # First run through groups: create all groups in payload
    # and store the IDs of the created groups:
    for group in self._groups:
        group_name = group.get("name")
        if not group_name:
            self.logger.error("Group needs a name. Skipping...")
            success = False
            continue

        # Check if group has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not group.get("enabled", True):
            self.logger.info(
                "Payload for group -> '%s' is disabled. Skipping...",
                group_name,
            )
            continue
        # M365 is disabled per default. There needs to be "enable_o365" in payload
        # and it needs to be True:
        if not group.get("enable_o365", False):
            self.logger.info(
                "Microsoft 365 is not enabled in payload for group -> '%s'. Skipping...",
                group_name,
            )
            continue

        # Check if the group does already exist (e.g. if job is restarted)
        # as this is a pattern search it could return multiple groups:
        existing_groups = self._m365.get_group(group_name=group_name)

        if existing_groups and existing_groups["value"]:
            self.logger.debug(
                "Found existing Microsoft 365 groups -> %s",
                existing_groups["value"],
            )
            # Get list of all matching groups:
            existing_groups_list = existing_groups["value"]
            # Find the group with the exact match of the name:
            existing_group = next(
                (item for item in existing_groups_list if item["displayName"] == group_name),
                None,
            )
            # Have we found an exact match?
            if existing_group is not None:
                self.logger.info(
                    "Found existing Microsoft 365 group -> '%s' (%s) - skip creation of group...",
                    existing_group["displayName"],
                    existing_group["id"],
                )
                # Write M365 group ID back into the payload (for the success file)
                group["m365_id"] = existing_group["id"]
                continue

        self.logger.info(
            "Creating a new Microsoft 365 group -> '%s'...",
            group_name,
        )

        # Now we know it is a new group...
        new_group = self._m365.add_group(name=group_name)
        if new_group is not None:
            # Store the Microsoft 365 group ID in payload:
            group["m365_id"] = new_group["id"]
            self.logger.info(
                "New Microsoft 365 group -> '%s' with ID -> %s has been created",
                group_name,
                group["m365_id"],
            )
        else:
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._groups,
    )

    return success

process_groups_salesforce(section_name='groupsSalesforce')

Process groups in payload and create them in Salesforce.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'groupsSalesforce'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_groups_salesforce")
def process_groups_salesforce(self, section_name: str = "groupsSalesforce") -> bool:
    """Process groups in payload and create them in Salesforce.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not isinstance(self._salesforce, Salesforce):
        self.logger.error(
            "Salesforce connection not setup properly. Skipping payload section '%s'...",
            section_name,
        )
        return False

    if not self._groups:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # First run through groups: create all groups in payload
    # and store the IDs of the created groups:
    for group in self._groups:
        group_name = group.get("name")
        if not group_name:
            self.logger.error("Group needs a name. Skipping...")
            success = False
            continue

        # Check if group has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not group.get("enabled", True):
            self.logger.info(
                "Payload for group -> '%s' is disabled. Skipping...",
                group_name,
            )
            continue

        # Salesforce is disabled per default. There needs to be "enable_salesforce" in payload
        # and it needs to be True:
        if not group.get("enable_salesforce", False):
            self.logger.info(
                "Salesforce is not enabled in payload for group -> '%s'. Skipping...",
                group_name,
            )
            continue

        # Check if the group does already exist (e.g. if job is restarted)
        existing_group_id = self._salesforce.get_group_id(group_name=group_name)
        if existing_group_id:
            self.logger.info(
                "Found existing Salesforce group -> '%s' (%s). Skipping...",
                group_name,
                existing_group_id,
            )
            # Write M365 group ID back into the payload (for the success file)
            group["salesforce_id"] = existing_group_id
            continue

        self.logger.info(
            "Creating a new Salesforce group -> '%s'...",
            group_name,
        )

        # Now we know it is a new group...
        new_group = self._salesforce.add_group(group_name=group_name)
        new_group_id = self._salesforce.get_result_value(
            response=new_group,
            key="id",
        )
        if new_group_id:
            # Store the Microsoft 365 group ID in payload:
            group["salesforce_id"] = new_group_id
            self.logger.info(
                "Successfully created Salesforce group -> '%s' with ID -> %s.",
                group_name,
                new_group_id,
            )
        else:
            self.logger.error(
                "Failed to create Salesforce group -> '%s'!",
                group_name,
            )
            success = False

    # Second run through groups: create all group memberships
    # (nested groups) based on the IDs created in first run:
    for group in self._groups:
        if "salesforce_id" not in group:
            self.logger.info(
                "Group -> '%s' does not have an Salesforce ID. Skipping...",
                group["name"],
            )
            # Not all groups may be enabled for Salesforce. This is not an error.
            continue
        group_id = group["salesforce_id"]
        parent_group_names = group["parent_groups"]
        for parent_group_name in parent_group_names:
            # First, try to find parent group in payload by parent group name:
            parent_group = next(
                (item for item in self._groups if item["name"] == parent_group_name),
                None,
            )
            if not parent_group:
                self.logger.error(
                    "Parent group -> '%s' not found. Cannot establish group nesting. Skipping...",
                    parent_group["name"],
                )
                success = False
                continue
            if "salesforce_id" not in parent_group:
                self.logger.info(
                    "Parent group -> '%s' does not have a Salesforce ID. Cannot establish group nesting. Parent group may not be enabled for Salesforce. Skipping...",
                    parent_group["name"],
                )
                # We don't treat this as an error - there may be payload groups which are not enabled for Salesforce!
                continue

            parent_group_id = parent_group["salesforce_id"]

            # retrieve all members of the parent group
            # to check if the current group is already a member in the parent group:
            members = self._salesforce.get_group_members(group_id=parent_group_id)
            if self._salesforce.exist_result_item(
                members,
                "UserOrGroupId",
                group_id,
            ):
                self.logger.info(
                    "Salesforce group -> '%s' (%s) is already a member of parent Salesforce group -> '%s' (%s). Skipping to next parent group...",
                    group["name"],
                    group["id"],
                    parent_group_name,
                    parent_group_id,
                )
                continue
            self.logger.info(
                "Add Salesforce group -> '%s' (%s) to parent Salesforce group -> '%s' (%s)...",
                group["name"],
                group_id,
                parent_group_name,
                parent_group_id,
            )
            self._salesforce.add_group_member(
                group_id=parent_group_id,
                member_id=group_id,
            )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._groups,
    )

    return success

process_holds(section_name='holds')

Process Records Management Holds.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'holds'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_holds")
def process_holds(self, section_name: str = "holds") -> bool:
    """Process Records Management Holds.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._holds:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for hold in self._holds:
        hold_name = hold.get("name")
        if not hold_name:
            self.logger.error("Cannot create a hold without a name! Skipping...")
            continue

        hold_type = hold.get("type")
        if not hold_type:
            self.logger.error(
                "Cannot create hold -> '%s' without a type! Skipping...",
                hold_name,
            )
            success = False
            continue

        # Check if hold has been explicitly disabled in payload
        # (enabled = false). In this case we skip this payload element:
        if not hold.get("enabled", True):
            self.logger.info(
                "Payload for hold -> '%s' (%s) is disabled. Skipping...",
                hold_name,
                hold_type,
            )
            continue

        hold_group = hold.get("group")
        hold_comment = hold.get("comment", "")
        hold_alternate_id = hold.get("alternate_id")
        hold_date_applied = hold.get("date_applied")
        hold_date_to_remove = hold.get("date_to_remove")

        response = self._otcs.get_node_by_volume_and_path(
            volume_type=self._otcs.VOLUME_TYPE_RECORDS_MANAGEMENT,
            path=["Hold Maintenance"],
        )
        if not response:
            self.logger.error("Cannot find 'Records Management' volume!")
            continue
        holds_maintenance_id = self._otcs.get_result_value(
            response=response,
            key="id",
        )
        if not holds_maintenance_id:
            self.logger.error(
                "Cannot find 'Holds Maintenance' folder in 'Records Management' volume!",
            )
            continue

        if hold_group:
            # Check if the Hold Group (folder) does already exist.
            response = self._otcs.get_node_by_parent_and_name(
                parent_id=holds_maintenance_id,
                name=hold_group,
            )
            parent_id = self._otcs.get_result_value(response=response, key="id")
            if not parent_id:
                response = self._otcs.create_item(
                    parent_id=holds_maintenance_id,
                    item_type=self._otcs.ITEM_TYPE_HOLD,
                    item_name=hold_group,
                )
                parent_id = self._otcs.get_result_value(response=response, key="id")
                if not parent_id:
                    self.logger.error(
                        "Failed to create hold group -> '%s'!",
                        hold_group,
                    )
                    continue
        else:
            parent_id = holds_maintenance_id

        # Holds are special - they ahve folders that cannot be traversed
        # in the normal way - we need to get the whole list of holds and use
        # specialparameters for the exist_result_items() method as the REST
        # API calls delivers a results->data->holds structure (not properties)
        response = self._otcs.get_records_management_holds()
        if self._otcs.exist_result_item(
            response=response,
            key="HoldName",
            value=hold_name,
            property_name="holds",
        ):
            self.logger.info(
                "Hold -> '%s' does already exist. Skipping...",
                hold_name,
            )
            continue

        hold = self._otcs.create_records_management_hold(
            hold_type=hold_type,
            name=hold_name,
            comment=hold_comment,
            alternate_id=hold_alternate_id,
            parent_id=int(parent_id),
            date_applied=hold_date_applied,
            date_to_remove=hold_date_to_remove,
        )

        if hold and hold["holdID"]:
            self.logger.info(
                "Successfully created hold -> '%s' with ID -> %s.",
                hold_name,
                hold["holdID"],
            )
        else:
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._holds,
    )

    return success

process_items(items, section_name='items')

Process items specified in payload and create them in Content Server.

Parameters:

Name Type Description Default
items list

List of items to create (need this as parameter as we have multiple lists). Each list item in the payload is a dict with this structure: { enabled = "..." name = "..." description = "..." nickname = "..." parent_nickname = "..." parent_path = [...] parent_volume = ... original_nickname = "..." original_path = [] type = ... url = "..." details = { "scheduledbotdetails" : ... } # additional parameters }

required
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'items'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_items")
def process_items(self, items: list, section_name: str = "items") -> bool:
    """Process items specified in payload and create them in Content Server.

    Args:
        items (list):
            List of items to create (need this as parameter as we
            have multiple lists).
            Each list item in the payload is a dict with this structure:
            {
                enabled = "..."
                name = "..."
                description = "..."
                nickname = "..."
                parent_nickname = "..."
                parent_path = [...]
                parent_volume = ...
                original_nickname = "..."
                original_path = []
                type = ...
                url = "..."
                details = {
                    "scheduledbotdetails" : ...
                } # additional parameters
            }
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not items:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )

        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for item in items:
        item_name = item.get("name")
        if not item_name:
            self.logger.error("Item needs a name. Skipping...")
            continue

        # Check if element has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not item.get("enabled", True):
            self.logger.info(
                "Payload for item -> '%s' is disabled. Skipping...",
                item_name,
            )
            continue

        item_description = item.get("description", "")
        item_nickname = item.get("nickname", None)
        parent_nickname = item.get("parent_nickname", None)
        parent_path = item.get("parent_path", None)

        if parent_nickname:  # parent nickname has preference over parent path
            parent_node = self._otcs.get_node_from_nickname(
                nickname=parent_nickname,
            )
            parent_id = self._otcs.get_result_value(response=parent_node, key="id")
            if not parent_id:
                self.logger.error(
                    "Item -> '%s' has a parent nickname -> '%s' that does not exist. Skipping...",
                    item_name,
                    parent_nickname,
                )
                success = False
                continue
        elif parent_path is not None:  # parent_path can be [] which is valid for top-level items!
            parent_volume = item.get("parent_volume", self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE)
            parent_node = self._otcs.get_node_by_volume_and_path(
                volume_type=parent_volume,
                path=parent_path,
                create_path=True,
            )
            parent_id = self._otcs.get_result_value(response=parent_node, key="id")
            if not parent_id:
                # if not parent_node:
                self.logger.error(
                    "Item -> '%s' has a parent path -> %s that does not exist and couldn't be created in volume -> %d. Skipping...",
                    item_name,
                    parent_path,
                    self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE,
                )
                success = False
                continue
        else:
            self.logger.error("The parent for the item -> '%s' is not specified by nickname nor path!", item_name)
            success = False
            continue

        # Handling for shortcut items that have an orginal node:
        original_nickname = item.get("original_nickname")
        original_path = item.get("original_path")

        if original_nickname:
            original_node = self._otcs.get_node_from_nickname(
                nickname=original_nickname,
            )
            original_id = self._otcs.get_result_value(
                response=original_node,
                key="id",
            )
            if not original_id:
                # if not original_node:
                self.logger.error(
                    "Item -> '%s' has a original nickname -> '%s' that does not exist. Skipping...",
                    item_name,
                    original_nickname,
                )
                success = False
                continue
        elif original_path is not None:  # original_path can be [] which is valid for top-level items!
            original_volume = item.get("original_volume", self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE)
            original_node = self._otcs.get_node_by_volume_and_path(
                volume_type=original_volume,
                path=original_path,
            )
            original_id = self._otcs.get_result_value(
                response=original_node,
                key="id",
            )
            if not original_id:
                # if not original_node:
                self.logger.error(
                    "Item -> '%s' has a original path that does not exist. Skipping...",
                    item_name,
                )
                success = False
                continue
        else:
            original_id = 0

        if "type" not in item:
            self.logger.error("Item -> '%s' needs a type. Skipping...", item_name)
            success = False
            continue

        item_type = int(item.get("type", self._otcs.ITEM_TYPE_FOLDER))
        item_url = item.get("url", "")
        item_details = item.get("details", {})
        create_item_details = item.get("create_details", {})
        # check that we have the required information
        # for the given item type:
        match item_type:
            case self._otcs.ITEM_TYPE_URL:  # URL
                if item_url == "":
                    self.logger.error(
                        "Item -> '%s' has type URL but the URL is not in the payload. Skipping...",
                        item_name,
                    )
                    success = False
                    continue
            case self._otcs.ITEM_TYPE_SHORTCUT:  # Shortcut
                if not original_id:
                    self.logger.error(
                        "Item -> '%s' has type Shortcut but the original item is not in the payload. Skipping...",
                        item_name,
                    )
                    success = False
                    continue
            case self._otcs.ITEM_TYPE_COLLECTION:  # Collection
                item_ids = item.get("ids", None)
                if item_ids is None:
                    self.logger.error(
                        "Item -> '%s' has type Collection but the list of collected items is not provided in payload. Skipping...",
                        item_name,
                    )
                    success = False
                    continue
            case self._otcs.ITEM_TYPE_SCHEDULED_BOT:  # Scheduled Bots
                if any(
                    key not in item_details for key in ["xecmpfJobType", "scheduledbotdetails"]
                ):  #  not in item_details:
                    self.logger.error(
                        "Item -> '%s' has type Scheduled Bot but the mandatory details are not provided in payload (xecmpfJobType, scheduledbotdetails). Skipping...",
                        item_name,
                    )
                    success = False
                    continue

                create_item_details["xecmpfJobType"] = item_details["xecmpfJobType"]

        # Check if an item with the same name does already exist.
        # This can also be the case if the python container runs a 2nd time.
        # For this reason we are also not issuing an error but just an info (False):
        response = self._otcs.get_node_by_parent_and_name(
            parent_id=int(parent_id),
            name=item_name,
            show_error=False,
        )
        if self._otcs.get_result_value(response=response, key="name") == item_name:
            self.logger.info(
                "Item with name -> '%s' does already exist in parent folder with ID -> %s.",
                item_name,
                parent_id,
            )
            continue
        response = self._otcs.create_item(
            parent_id=int(parent_id),
            item_type=item_type,
            item_name=item_name,
            item_description=item_description,
            url=item_url,
            original_id=int(original_id),
            **create_item_details,
        )
        node_id = self._otcs.get_result_value(response=response, key="id")
        if not node_id:
            self.logger.error(
                "Failed to create item -> '%s' under parent%s!",
                item_name,
                " with nickname -> '{}'".format(parent_nickname)
                if parent_nickname
                else " path -> {} in volume -> {}".format(parent_path, parent_volume),
            )
            success = False
            continue

        self.logger.info(
            "Successfully created item -> '%s' with ID -> %s under parent%s.",
            item_name,
            node_id,
            " with nickname -> '{}'".format(parent_nickname)
            if parent_nickname
            else " path -> {} in volume -> {}".format(parent_path, parent_volume),
        )

        # Special handling for scheduled bot items:
        if item_type == self._otcs.ITEM_TYPE_SCHEDULED_BOT:
            scheduled_bot_details = item_details.get("scheduledbotdetails", {})
            if not scheduled_bot_details:
                self.logger.error("Failed to get details of scheduled bot item -> '%s'!", item_name)
                success = False
                continue
            start_mode = scheduled_bot_details.get("startmodus")
            if not start_mode:
                self.logger.error("Failed to get start mode of scheduled bot item -> '%s'!", item_name)
                success = False
                continue
            start_mode = start_mode.get("startMode")
            if not start_mode:
                self.logger.error("Failed to get start mode of scheduled bot item -> '%s'!", item_name)
                success = False
                continue
            old_schedule_data = scheduled_bot_details.get("oldscheduleData")
            # Check if this bot should start after another bot:
            if start_mode == "AfterJob":
                after_job_nickname = scheduled_bot_details["startmodus"].get("afterJob")
                if not after_job_nickname:
                    after_job_nickname = item_details.get("xecmpfAfterJobDataId")
                self.logger.info(
                    "Scheduled bot item -> '%s' starts after another scheduled bot with nickname -> '%s'. Resolving nickname...",
                    item_name,
                    after_job_nickname,
                )
                # Get the Scheduled Bot node that this bot depends on:
                response = self._otcs.get_node_from_nickname(nickname=after_job_nickname)
                after_job_id = self._otcs.get_result_value(
                    response=response,
                    key="id",
                )
                # Update the bot details - changing the name coming from the
                # payload to the actual node ID (both in details and in 'old' details):
                scheduled_bot_details["startmodus"]["afterJob"] = after_job_id
                old_schedule_data["afterJob"] = after_job_id

                item_details["xecmpfAfterJobDataId"] = after_job_id
            # Check if this bot should start based on a schedule.
            # Then we need to configure the agent to run it:
            elif str(start_mode) == "7558":  # 7558 is NOT a object ID but an internal agent type ID!
                self.logger.info(
                    "Scheduled bot item -> '%s' starts based on a schedule. Setting the agent ID to '7558'...",
                    item_name,
                )
                # Make sure the agent ID is configured:
                item_details["xecmpfAgentId"] = 7558
            self.logger.debug("Scheduled bot details -> %s", str(scheduled_bot_details))

            # The REST API requires to have the scheduled bot details wrapped into a JSON structure:
            item_details["scheduledbotdetails"] = json.dumps(item_details.get("scheduledbotdetails", {}))

            response = self._otcs.update_item(node_id=node_id, body=False, **item_details)
            if not response:
                self.logger.error("Failed to update scheduled bot item -> '%s'!", item_name)
                success = False
                continue

            # Check if we want to execute an action immediately after creation, like "Runnow":
            actions = item_details.get("actions", [])
            for action in actions:
                self.logger.info("Execute action -> '%s' for scheduled bot -> '%s'...", action, item_name)
                response = self._otcs.update_item(node_id=node_id, body=False, actionName=action)
                if not response:
                    self.logger.error(
                        "Failed to execute action -> '%s' for scheduled bot item -> '%s'!", action, item_name
                    )
                    success = False
                    continue
            if not actions:
                self.logger.info("No immediate actions specified for scheduled bot -> '%s'.", item_name)

        # end if item_type == self._otcs.ITEM_TYPE_SCHEDULED_BOT:

        # Special handling for collection items:
        elif item_type == self._otcs.ITEM_TYPE_COLLECTION:
            item_node_ids = []
            for collection_item in item_ids or []:
                # First see if the collection item is a workspace we can
                # lookup in the payload by its logical payload ID
                member_item = next(
                    (item for item in self._workspaces if item["id"] == collection_item),
                    None,
                )
                if member_item:
                    if member_item.get("enabled", True):
                        self.logger.info(
                            "Found collection item (workspace) -> '%s' in payload and it is enabled.",
                            member_item["name"],
                        )
                    else:
                        self.logger.info(
                            "Found collection item (workspace) -> '%s' in payload but it is not enabled. Skipping...",
                            member_item["name"],
                        )
                        continue
                    member_id = self.determine_workspace_id(workspace=member_item)
                    if not member_id:
                        self.logger.warning(
                            "Workspace of type -> '%s' and name -> '%s' does not exist. Cannot create collection item. Skipping...",
                            member_item["type_name"],
                            member_item["name"],
                        )
                        continue
                else:
                    # alternatively try to find the item as a nickname:
                    member_item = self._otcs.get_node_from_nickname(
                        nickname=collection_item,
                    )
                    member_id = self._otcs.get_result_value(
                        response=member_item,
                        key="id",
                    )
                    if not member_id:
                        self.logger.warning(
                            "Item -> '%s' does not exist. Cannot create collection item. Skipping...",
                            member_item["name"],
                        )
                        continue
                item_node_ids.append(member_id)
            # end for collection_item in item_ids
            if item_node_ids:
                response = self._otcs.add_node_to_collection(
                    collection_id=node_id,
                    node_ids=item_node_ids,
                )
        # end if item_type == self._otcs.ITEM_TYPE_COLLECTION

        # Do we have a nickname for the item in the payload? Then assign it:
        if item_nickname:
            self.logger.info("Assign nickname -> '%s' to item -> '%s' (%s)...", item_nickname, item_name, node_id)
            self._otcs.set_node_nickname(node_id=node_id, nickname=item_nickname)
    # end for item in items:

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=items,
    )

    return success

process_kubernetes(section_name='kubernetes')

Process actions that should be executed in the Kubernetess.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'kubernetes'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_kubernetes")
def process_kubernetes(self, section_name: str = "kubernetes") -> bool:
    """Process actions that should be executed in the Kubernetess.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not isinstance(self._k8s, K8s):
        self.logger.error(
            "K8s not setup properly -> Skipping payload section -> '%s'...",
            section_name,
        )
        return False

    if not self._kubernetes:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for item in self._kubernetes:
        # Check if element has been disabled in payload (enabled = false).
        # In this case we skip the element:
        if isinstance(item, dict) and not item.get("enabled", True):
            self.logger.info("Skipping disabled item -> %s", item)
            continue

        match item.get("action"):
            case "execPodCommand":
                pod_name = item.get("pod_name")

                if not pod_name:
                    self.logger.error(
                        "To execute a command in a pod the pod name needs to be specified in the payload! Skipping to next kubernetes item...",
                    )
                    success = False
                    continue

                command = item.get("command", [])
                if not command:
                    self.logger.error(
                        "Command is not specified for pod -> %s! It needs to be a non-empty list! Skipping to next kubernetes item...",
                        pod_name,
                    )
                    success = False
                    continue

                container = item.get("container", None)
                timeout = int(item.get("timeout", 60))

                # Check if exec pod command has been explicitly disabled in payload
                # (enabled = false). In this case we skip the element:
                if not item.get("enabled", True):
                    self.logger.info(
                        "Payload for exec pod command in pod -> '%s' is disabled. Skipping...",
                        pod_name,
                    )
                    continue

                if "description" not in item:
                    self.logger.info(
                        "Executing command -> %s in pod -> '%s'",
                        command,
                        pod_name,
                    )

                else:
                    description = item["description"]
                    self.logger.info(
                        "Executing command -> %s in pod -> '%s' (%s)...",
                        command,
                        pod_name,
                        description,
                    )

                if "interactive" not in item or item["interactive"] is False:
                    result = self._k8s.exec_pod_command(
                        pod_name=pod_name, command=command, container=container, timeout=timeout
                    )
                else:
                    result = self._k8s.exec_pod_command_interactive(
                        pod_name=pod_name,
                        commands=command,
                        timeout=timeout,
                    )

                # we need to differentiate 3 cases here:
                # 1. result = None is returned - this is an error (exception)
                # 2. result is empty string - this is OK
                # 3. result is a non-empty string - this is OK - print it to log
                if result is None:
                    self.logger.error(
                        "Execution of command -> %s in pod -> '%s' failed!",
                        command,
                        pod_name,
                    )
                    success = False
                elif result != "":
                    self.logger.info(
                        "Execution of command -> %s in pod -> '%s' returned result -> %s.",
                        command,
                        pod_name,
                        result,
                    )
                else:
                    # It is not an error if no result is returned. It depends on the nature of the command
                    # if a result is written to stdout or stderr.
                    self.logger.info(
                        "Execution of command -> %s in pod -> '%s' did not return a result.",
                        command,
                        pod_name,
                    )

            case "restart":
                k8s_type = item.get("type")
                name = item.get("name")
                message = item.get("message")

                if not name:
                    self.logger.error("Name not specified for kubernetes item -> %s", item)
                if not k8s_type:
                    self.logger.error("Type not specified for kubernetes item -> %s", item)

                if message:
                    self.logger.info("%s", message)

                if k8s_type.lower() == "statefulset":
                    self.logger.info("Restarting statefulset -> %s...", name)
                    restart_result = self._k8s.restart_stateful_set(sts_name=name)

                elif k8s_type.lower() == "deployment":
                    self.logger.info("Restarting deployment -> %s...", name)
                    restart_result = self._k8s.restart_deployment(deployment_name=name)

                elif k8s_type.lower() == "pod":
                    self.logger.info("Deleting pod -> %s...", name)
                    restart_result = self._k8s.delete_pod(pod_name=name)

                if not restart_result:
                    success = False

            case _:
                self.logger.error("Action not defined for work item -> %s", item)
                continue

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._kubernetes,
    )

    return success

process_licenses(section_name='licenses')

Process OTDS licenses in payload and create them in OTDS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'licenses'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_licenses")
def process_licenses(self, section_name: str = "licenses") -> bool:
    """Process OTDS licenses in payload and create them in OTDS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise

    """

    if not self._licenses:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for lic in self._licenses:
        self.logger.debug("Start processing License -> '%s'", lic)
        if not lic.get("enabled", True):
            self.logger.info(
                "Payload for License -> '%s' is disabled. Skipping...",
                lic,
            )
            continue

        path = lic.get("path")
        if not path:
            self.logger.error("Required attribute path not specified (%s)! Skipping ...", lic)
            success = False
            continue

        product_name = lic.get("product_name")
        if not product_name:
            self.logger.error("Required attribute 'product_name' not specified (%s)! Skipping ...", lic)
            success = False
            continue

        if "resource" in lic:
            try:
                resource_id = self._otds.get_resource(name=lic["resource"])["resourceID"]
            except Exception:
                self.logger.error("Error getting resource ID from resource -> '%s'!", lic["resource"])
                success = False
                continue
        else:
            self.logger.error("Required attribute 'resource' not specified (%s)! Skipping ...", lic)
            success = False
            continue

        self.logger.info(
            "Adding license -> '%s' for product -> '%s' to resource -> '%s'...", path, product_name, resource_id
        )

        add_license = self._otds.add_license_to_resource(
            path_to_license_file=path,
            product_name=product_name,
            product_description=lic.get("description", ""),
            resource_id=resource_id,
            update=lic.get("update", True),
        )

        if not add_license:
            success = False
            continue

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._licenses,
    )

    return success

process_nifi_flows(section_name='nifi')

Process Knowledge Discovery Nifi flows in payload and create them in Nifi.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'nifi'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_nifi_flows")
def process_nifi_flows(self, section_name: str = "nifi") -> bool:
    """Process Knowledge Discovery Nifi flows in payload and create them in Nifi.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise

    """

    if not self._nifi_flows:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for nifi_flow in self._nifi_flows:
        if "name" not in nifi_flow:
            self.logger.error(
                "Knowledge Discovery Nifi flow needs a name! Skipping to next Nifi flow...",
            )
            success = False
            continue
        name = nifi_flow["name"]

        # Check if element has been disabled in payload (enabled = false).
        # In this case we skip the element:
        if not nifi_flow.get("enabled", True):
            self.logger.info(
                "Payload for Knowledge Discovery Nifi flow -> '%s' is disabled. Skipping...",
                name,
            )
            continue

        if "file" not in nifi_flow:
            self.logger.error(
                "Knowledge Discovery Nifi flow -> '%s' needs a file! Skipping to next Nifi flow...", name
            )
            success = False
            continue
        filename = nifi_flow["file"]

        parameters = nifi_flow.get("parameters", [])

        if not self._otkd:
            self.logger.error("Knowledge Discovery is not initialized. Stop processing Nifi flows.")
            success = False
            break

        # Optional layout positions of the flow:
        position_x = nifi_flow.get("position_x", 0.0)
        position_y = nifi_flow.get("position_y", 0.0)
        start = nifi_flow.get("start", False)

        self.logger.info("Processing Knowledge Discovery Nifi flow -> '%s'...", name)

        existing = self._otkd.get_process_group_by_name(name=name)
        if existing:
            self.logger.warning("Nifi flow -> '%s' does already exist. Updating parameters only...", name)
            # We better don't start existing flows!? Otherwise this may produce errors.
            start = False
        else:
            response = self._otkd.upload_process_group(
                file_path=filename, name=name, position_x=position_x, position_y=position_y
            )
            if not response:
                self.logger.error("Failed to upload new Nifi flow -> '%s' for Knowledge Discovery!", name)
                success = False
                continue
            self.logger.info("Sucessfully uploaded new Nifi flow -> '%s' for Knowledge Discovery.", name)

        for parameter in parameters:
            component = parameter.get("component", None)
            if not component:
                self.logger.error("Missing component in parameter of Nifi flow -> '%s'!", name)
                success = False
                continue
            parameter_name = parameter.get("name", None)
            if not parameter_name:
                self.logger.error(
                    "Missing name in parameter of Nifi flow -> '%s', component -> '%s'!", name, component
                )
                success = False
                continue
            parameter_description = parameter.get("description", "")
            parameter_value = parameter.get("value", None)
            if not parameter_value:
                self.logger.error(
                    "Missing value in parameter of Nifi flow -> '%s', component -> '%s'!", name, component
                )
                success = False
                continue
            parameter_sensitive = parameter.get("sensitive", False)

            response = self._otkd.update_parameter(
                component=component,
                parameter=parameter_name,
                value=parameter_value,
                sensitive=parameter_sensitive,
                description=parameter_description,
            )
            if not response:
                self.logger.error("Failed to update parameter -> '%s' of Nifi flow -> '%s'!", parameter_name, name)
                success = False
                continue
            self.logger.info(
                "Successfully updated parameter -> '%s' of component -> '%s' in Nifi flow -> '%s' to value -> '%s'.",
                parameter_name,
                component,
                name,
                parameter_value if not parameter_sensitive else "<sensitive>",
            )
        # end for parameter in parameters:
        if start:
            response = self._otkd.start_all_processors(name=name)
            if response:
                self.logger.info("Successfully started Nifi flow -> '%s'.", name)
            else:
                self.logger.error("Failed to start Nifi flow -> '%s'!", name)
                success = False

            response = self._otkd.set_controller_services_state(name=name, state="ENABLED")
            if response:
                self.logger.info("Successfully enabled Nifi Controller Services for Nifi flow -> '%s'.", name)
            else:
                self.logger.error("Failed to enable Nifi Controller Services for Nifi flow -> '%s'!", name)
                success = False

        else:
            self.logger.info("Don't (re)start Nifi flow -> '%s'.", name)
    # end for nifi_flow in self._nifi_flows:

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._nifi_flows,
    )

    return success

process_oauth_clients(section_name='oauthClients')

Process OTDS OAuth clients in payload and create them in OTDS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'oauthClients'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_oauth_clients")
def process_oauth_clients(self, section_name: str = "oauthClients") -> bool:
    """Process OTDS OAuth clients in payload and create them in OTDS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._oauth_clients:
        self.logger.info("Payload section -> %s is empty. Skipping...", section_name)
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for oauth_client in self._oauth_clients:
        client_name = oauth_client.get("name")
        if not client_name:
            self.logger.error("OAuth client does not have a name in payload. Skipping...")
            success = False
            continue

        # Check if oauth client has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not oauth_client.get("enabled", True):
            self.logger.info(
                "Payload for OAuth client -> '%s' is disabled in payload. Skipping...",
                client_name,
            )
            continue

        client_description = oauth_client.get("description", "")
        client_confidential = oauth_client.get("confidential", True)
        client_partition = oauth_client.get("partition", "Global")
        if client_partition == "Global":
            client_partition = []
        client_redirect_urls = oauth_client.get("redirect_urls", [])
        client_permission_scopes = oauth_client.get("permission_scopes")
        client_default_scopes = oauth_client.get("default_scopes")
        client_allow_impersonation = oauth_client.get("allow_impersonation", True)
        client_secret = oauth_client.get("secret", "")

        # Check if OAuth client does already exist
        # (in an attempt to make the code idem-potent)
        self.logger.info(
            "Check if OTDS OAuth client -> '%s' does already exist...",
            client_name,
        )
        response = self._otds.get_oauth_client(
            client_id=client_name,
            show_error=False,
        )
        if response:
            self.logger.info(
                "OAuth client -> '%s' does already exist. Skipping...",
                client_name,
            )
            continue
        self.logger.info(
            "OAuth client -> '%s' does not exist. Creating...",
            client_name,
        )

        response = self._otds.add_oauth_client(
            client_id=client_name,
            description=client_description,
            redirect_urls=client_redirect_urls,
            allow_impersonation=client_allow_impersonation,
            confidential=client_confidential,
            auth_scopes=client_partition,
            allowed_scopes=client_permission_scopes,
            default_scopes=client_default_scopes,
            secret=client_secret,
        )
        if response:
            self.logger.info("Added OTDS OAuth client -> '%s'.", client_name)
        else:
            self.logger.error(
                "Failed to add OTDS OAuth client -> '%s'!",
                client_name,
            )
            success = False
            continue

        # in case the secret has not been provided in the payload we retrieve
        # the automatically created secret:
        client_secret = response.get("secret")
        if not client_secret:
            self.logger.error(
                "OAuth client -> '%s' does not have a secret!",
                client_name,
            )
            continue

        client_description += " Client Secret: " + str(client_secret)
        response = self._otds.update_oauth_client(
            client_id=client_name,
            updates={"description": client_description},
        )
        # Write the secret back into the payload
        oauth_client["secret"] = client_secret

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._oauth_clients,
    )

    return success

process_partitions(section_name='partitions')

Process OTDS partitions in payload and create them in OTDS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'partitions'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_partitions")
def process_partitions(self, section_name: str = "partitions") -> bool:
    """Process OTDS partitions in payload and create them in OTDS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise

    """

    if not self._partitions:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for partition in self._partitions:
        partition_name = partition.get("name")
        if not partition_name:
            self.logger.error("Partition does not have a name in payload! Skipping...")
            success = False
            continue

        # Check if partition has explicitly been disabled in payload
        # (enabled = false). In this case we skip the element:
        if not partition.get("enabled", True):
            self.logger.info(
                "Payload for partition -> '%s' is disabled. Skipping...",
                partition_name,
            )
            continue

        partition_description = partition.get("description", "")

        # Check if Partition does already exist (to make the code idem-potent):
        self.logger.info(
            "Check if OTDS partition -> '%s' does already exist...",
            partition_name,
        )
        response = self._otds.get_partition(name=partition_name, show_error=False)
        if response:
            self.logger.info(
                "Partition -> '%s' does already exist.",
                partition_name,
            )
        else:
            # Only continue if Partition does not exist already
            self.logger.info(
                "Partition -> '%s' does not yet exist. Creating...",
                partition_name,
            )

            response = self._otds.add_partition(
                name=partition_name,
                description=partition_description,
            )
            if response:
                self.logger.info(
                    "Added OTDS partition -> '%s'%s.",
                    partition_name,
                    " ({})".format(partition_description) if partition_description else "",
                )
            else:
                self.logger.error(
                    "Failed to add OTDS partition -> '%s'!",
                    partition_name,
                )
                success = False
                continue

        access_role = partition.get("access_role")
        if access_role:
            response = self._otds.add_partition_to_access_role(
                access_role=access_role,
                partition=partition_name,
            )
            if response:
                self.logger.info(
                    "Added OTDS partition -> '%s' to access role -> '%s'.",
                    partition_name,
                    access_role,
                )
            else:
                self.logger.error(
                    "Failed to add OTDS partition -> '%s' to access role -> '%s'!",
                    partition_name,
                    access_role,
                )
                success = False
        # end if access_role

        # Partions may have an optional list of licenses in
        # the payload. Assign the partition to all these licenses:
        partition_specific_licenses = partition.get("licenses")
        if partition_specific_licenses:
            # We assume these licenses are Extended ECM licenses!
            otcs_resource_name = self._otcs.config()["resource"]
            otcs_resource = self._otds.get_resource(name=otcs_resource_name)
            if not otcs_resource:
                self.logger.error(
                    "Cannot find OTCS resource -> '%s'!",
                    otcs_resource_name,
                )
                success = False
                continue
            otcs_resource_id = otcs_resource["resourceID"]
            otcs_license_name = "EXTENDED_ECM"
            for license_item in partition_specific_licenses:
                if isinstance(license_item, dict):
                    license_feature = license_item.get("feature")
                    license_name = license_item.get("name", "EXTENDED_ECM")
                    if "enabled" in license_item and not license_item["enabled"]:
                        self.logger.info(
                            "Payload for License '%s' -> '%s' is disabled. Skipping...",
                            license_name,
                            license_feature,
                        )
                        continue

                    if "resource" in license_item:
                        try:
                            resource_id = self._otds.get_resource(name=license_item["resource"])["resourceID"]
                        except Exception:
                            self.logger.error(
                                "Error getting resource ID from resource -> '%s'!", license_item["resource"]
                            )
                    else:
                        resource_id = otcs_resource_id

                elif isinstance(license_item, str):
                    license_feature = license_item
                    resource_id = otcs_resource_id
                    license_name = otcs_license_name
                else:
                    self.logger.error("Invalid License feature specified -> %s!", license_item)
                    success = False
                    continue

                if self._otds.is_partition_licensed(
                    partition_name=partition_name,
                    resource_id=resource_id,
                    license_feature=license_feature,
                    license_name=license_name,
                ):
                    self.logger.info(
                        "Partition -> '%s' is already licensed for -> '%s' (%s).",
                        partition_name,
                        license_name,
                        license_feature,
                    )
                    continue

                assigned_license = self._otds.assign_partition_to_license(
                    partition_name=partition_name,
                    resource_id=resource_id,
                    license_feature=license_feature,
                    license_name=license_name,
                )

                if not assigned_license:
                    self.logger.error(
                        "Failed to assign partition -> '%s' to license feature -> '%s' of license -> '%s'!",
                        partition_name,
                        license_feature,
                        license_name,
                    )
                    success = False
                else:
                    self.logger.info(
                        "Successfully assigned partition -> '%s' to license feature -> '%s' of license -> '%s'.",
                        partition_name,
                        license_feature,
                        license_name,
                    )
        # end if partition_specific_licenses:
    # end for partition in self._partitions:

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._partitions,
    )

    return success

process_payload()

Process a payload file.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_payload")
def process_payload(self) -> None:
    """Process a payload file."""

    if not self._payload_sections:
        self.logger.warning("No payload sections defined!")
        return

    for payload_section in self._payload_sections:
        with tracer.start_as_current_span("process_payload-section") as t:
            t.set_attributes({"payload_section": payload_section["name"]})
            self.logger.debug("processing payload_section -> %s", payload_section)
            if not payload_section.get("enabled", True):
                self.logger.info(
                    "Payload section -> '%s' is disabled. Skipping...",
                    payload_section.get("name", "Name not defined"),
                )
                continue

            match payload_section["name"]:
                case "embeddings" | "feme":
                    self._log_header_callback(
                        text="Process additional Embeddings for Content Aviator",
                    )
                    self.process_embeddings()
                case "avtsRepositories":
                    self._log_header_callback(
                        text="Process Aviator Search repositories",
                    )
                    self.process_avts_repositories()
                case "avtsQuestions":
                    self._log_header_callback(
                        text="Process Aviator Search Sample Questions",
                    )
                    self.process_avts_questions()
                case "appworks":
                    self._log_header_callback(text="Process AppWorks Configurations")
                    self.process_appworks_configurations()
                case "webHooks":
                    self._log_header_callback(text="Process Web Hooks")
                    self.process_web_hooks(webhooks=self._webhooks)
                case "webHooksPost":
                    self._log_header_callback(text="Process Web Hooks (post)")
                    self.process_web_hooks(
                        webhooks=self._webhooks_post,
                        section_name="webHooksPost",
                    )
                case "resources":
                    self._log_header_callback(text="Process OTDS Resources")
                    self.process_resources()
                case "partitions":
                    self._log_header_callback(text="Process OTDS Partitions")
                    self.process_partitions()
                case "licenses":
                    self._log_header_callback(text="Process OTDS Licenses")
                    self.process_licenses()
                case "synchronizedPartitions":
                    self._log_header_callback(
                        text="Process OTDS Synchronized Partitions",
                    )
                    self.process_synchronized_partitions()
                case "oauthClients":
                    self._log_header_callback(text="Process OTDS OAuth Clients")
                    self.process_oauth_clients()
                case "applicationRoles":
                    self._log_header_callback(text="Process OTDS Application Roles")
                    self.process_application_roles()
                case "authHandlers":
                    self._log_header_callback(text="Process OTDS Auth Handlers")
                    self.process_auth_handlers()
                case "trustedSites":
                    self._log_header_callback(text="Process OTDS Trusted Sites")
                    self.process_trusted_sites()
                case "systemAttributes":
                    self._log_header_callback(text="Process OTDS System Attributes")
                    self.process_system_attributes()
                case "docgenSettings":
                    if self._otpd and isinstance(self._otpd, OTPD):
                        self._log_header_callback(text="Process OTPD Settings")
                        self.process_docgen_settings()
                case "groups":
                    self._log_header_callback(text="Process Groups")
                    self.process_groups()
                    # Add all groups with ID the a lookup dict for placeholder replacements
                    # in adminSetting. This also updates the payload with group IDs from OTCS
                    # if the group already exists in Content Server. This is important especially
                    # if the customizer pod is restarted / run multiple times:
                    self.process_group_placeholders()
                    if self._core_share and isinstance(self._core_share, CoreShare):
                        self._log_header_callback(text="Process Core Share Groups", char="-")
                        self.process_groups_core_share()
                    if self._m365 and isinstance(self._m365, M365):
                        self._log_header_callback(text="Cleanup existing M365 Teams", char="-")
                        self.cleanup_all_teams_m365()
                        self._log_header_callback(text="Process M365 Groups", char="-")
                        self.process_groups_m365()
                case "users":
                    self._log_header_callback(text="Process Users")
                    self._user_customization = bool(
                        payload_section.get("user_customization", "True"),
                    )
                    self.process_users()
                    # Add all users with ID the a lookup dict for placeholder replacements
                    # in adminSetting. This also updates the payload with user IDs from OTCS
                    # if the user already exists in Content Server. This is important especially
                    # if the cutomizer pod is restarted / run multiple times:
                    self.process_user_placeholders()
                    self._log_header_callback(
                        text="Assign OTCS licenses to users",
                        char="-",
                    )
                    self.process_user_licenses(
                        resource_name=self._otcs.config()["resource"],
                        license_feature=self._otcs.config()["license"],
                        user_specific_payload_field="licenses",
                    )
                    self._log_header_callback(
                        text="Process OTDS user settings",
                        char="-",
                    )
                    self.process_user_settings()
                    if self._core_share and isinstance(self._core_share, CoreShare):
                        self._log_header_callback(text="Process Core Share users", char="-")
                        self.process_users_core_share()
                    if self._m365 and isinstance(self._m365, M365):
                        self._log_header_callback(text="Process M365 users", char="-")
                        self.process_users_m365()
                        # We need to do the MS Teams creation after the creation of
                        # the M365 users as we require Group Owners to create teams.
                        # Note: this is just for the teams of the top-level OTCS groups
                        # (departments), not the MS Teams for the Workspaces. These
                        # are created via the scheduled bots!
                        self._log_header_callback(text="Process M365 Teams for departmental groups", char="-")
                        self.process_teams_m365()
                case "adminSettings":
                    self._log_header_callback(
                        text="Process OTCS administration settings",
                    )
                    restart_required = self.process_admin_settings(
                        admin_settings=self._admin_settings,
                    )
                    if restart_required:
                        self.logger.info(
                            "Admin Settings require a restart of OTCS services...",
                        )
                        # Restart OTCS frontend and backend pods:
                        self._otcs_restart_callback(
                            backend=self._otcs_backend,
                            frontend=self._otcs_frontend,
                        )
                case "adminSettingsPost":
                    self._log_header_callback(
                        text="Process OTCS administration settings (post)",
                    )
                    restart_required = self.process_admin_settings(
                        self._admin_settings_post,
                        "adminSettingsPost",
                    )
                    if restart_required:
                        self.logger.info(
                            "Admin settings (Post) require a restart of OTCS services...",
                        )
                        # Restart OTCS frontend and backend pods:
                        self._otcs_restart_callback(
                            backend=self._otcs_backend,
                            frontend=self._otcs_frontend,
                        )
                case "execPodCommands":
                    self._log_header_callback(text="Process Pod Commands")
                    self.process_exec_pod_commands()
                case "kubernetes":
                    self._log_header_callback(text="Process Kubernetes Commands")
                    self.process_kubernetes()
                case "execCommands":
                    self._log_header_callback(text="Process Commands in Customizer Pod")
                    self.process_exec_commands()
                case "execDatabaseCommands":
                    self._log_header_callback(text="Process Database Commands")
                    self.process_exec_database_commands()
                case "csApplications":
                    self._log_header_callback(text="Process CS Apps (backend)")
                    self.process_cs_applications(
                        otcs_object=self._otcs_backend,
                        section_name="csApplicationsBackend",
                    )
                    self._log_header_callback("Process CS Apps (frontend)")
                    self.process_cs_applications(
                        otcs_object=self._otcs_frontend,
                        section_name="csApplicationsFrontend",
                    )
                case "externalSystems":
                    self._log_header_callback(
                        text="Process External System Connections",
                    )
                    self.process_external_systems()
                    # Now the SAP, SuccessFactors and Salesforce objects
                    # should be initialized and we can process users and groups
                    # in these external systems:
                    if self._sap and isinstance(self._sap, SAP):
                        self._log_header_callback(text="Process SAP Users")
                        self.process_users_sap()
                    if self._successfactors and isinstance(
                        self._successfactors,
                        SuccessFactors,
                    ):
                        self._log_header_callback(text="Process SuccessFactors Users")
                        self.process_users_successfactors()
                    if self._salesforce and isinstance(self._salesforce, Salesforce):
                        self._log_header_callback(text="Process Salesforce Groups")
                        self.process_groups_salesforce()
                        self._log_header_callback(text="Process Salesforce Users")
                        self.process_users_salesforce()
                case "transportPackages":
                    self._log_header_callback(text="Process Transport Packages")
                    self.process_transport_packages(self._transport_packages)
                    # Right after the transport that create the business object types
                    # and the workspace types we extract them and put them into
                    # generated payload lists:
                    self._log_header_callback(text="Process Business Object Types")
                    self.process_business_object_types()
                    self._log_header_callback(text="Process Workspace Types")
                    self.process_workspace_types()
                    if self._m365 and isinstance(self._m365, M365):
                        # Right after the transport that creates the top level folders
                        # we can add the M365 Teams apps for Extended ECM as its own tab:
                        self._log_header_callback(text="Process M365 Teams Apps")
                        self.process_teams_m365_apps()
                        # The SharePoint sites require the top-level departmental folder.
                        # So we can do this only after the transport packages have been
                        # processed:
                        self._log_header_callback(text="Process M365 SharePoint sites for departmental groups")
                        self.process_sites_m365()
                case "contentTransportPackages":
                    self._log_header_callback("Process Content Transport Packages")
                    self.process_transport_packages(
                        transport_packages=self._content_transport_packages,
                        section_name="contentTransportPackages",
                    )
                    # Process workspace permissions after content has been added:
                    # (this is a workaround for a flaw in transport warehouse that don't
                    # set workspace role permissions for content transported into workspaces)
                    self._log_header_callback(
                        text="Process Workspace Member Permissions",
                        char="-",
                    )
                    self.process_workspace_member_permissions()
                case "transportPackagesPost":
                    self._log_header_callback("Process Transport Packages (post)")
                    self.process_transport_packages(
                        transport_packages=self._transport_packages_post,
                        section_name="transportPackagesPost",
                    )
                case "workspaceTemplates":
                    # If a payload file (e.g. additional ones) does not have
                    # transportPackages then it can happen that the
                    # self._business_object_types and self._workspace_types are
                    # not yet initialized. As we need these structures for
                    # workspaceTemplates we initialize them here:
                    if not self._business_object_types:
                        self._log_header_callback("Process Business Object Types")
                        self.process_business_object_types()
                    if not self._workspace_types:
                        self._log_header_callback("Process Workspace Types")
                        self.process_workspace_types()

                    self._log_header_callback(
                        "Process Workspace Templates (Template Role Assignments)",
                    )
                    self.process_workspace_templates()
                case "workspaces":
                    # If a payload file (e.g. additional ones) does not have
                    # transportPackages then it can happen that the self._business_object_types and
                    # self._workspace_types are not yet initialized. As we need
                    # these structures for workspaces we initialize it here:
                    if not self._business_object_types:
                        self._log_header_callback("Process Business Object Types")
                        self.process_business_object_types()
                    if not self._workspace_types:
                        self._log_header_callback("Process Workspace Types")
                        self.process_workspace_types()

                    self._log_header_callback("Process Workspaces")
                    self.process_workspaces()
                    self._log_header_callback("Process Workspace Relationships")
                    self.process_workspace_relationships()
                    self._log_header_callback("Process Workspace Memberships")
                    self.process_workspace_members()

                    # This has to run after the processing of webReports that are
                    # used to enable Content Aviator in KINI database table:
                    if self._aviator_enabled:
                        self._log_header_callback("Process Workspace Aviators")
                        self.process_workspace_aviators()
                case "bulkDatasources":
                    # this is here just to avoid an error in catch all below
                    # the bulkDatasources dictionary will be processed in
                    # the other bulk* sections
                    pass
                case "bulkWorkspaces":
                    if not self._workspace_types:
                        self._log_header_callback("Process Workspace Types")
                        self.process_workspace_types()
                    self._log_header_callback("Process Bulk Workspaces")
                    self.process_bulk_workspaces()
                case "bulkWorkspaceRelationships":
                    if not self._workspace_types:
                        self._log_header_callback("Process Workspace Types")
                        self.process_workspace_types()
                    self._log_header_callback("Process Bulk Workspace Relationships")
                    self.process_bulk_workspace_relationships()
                case "bulkDocuments":
                    if not self._workspace_types:
                        self._log_header_callback("Process Workspace Types")
                        self.process_workspace_types()
                    self._log_header_callback("Process Bulk Documents")
                    self.process_bulk_documents()
                case "bulkItems":
                    if not self._workspace_types:
                        self._log_header_callback("Process Workspace Types")
                        self.process_workspace_types()
                    self._log_header_callback("Process Bulk Items")
                    self.process_bulk_items()
                case "bulkClassifications":
                    self._log_header_callback("Process Bulk Classifications")
                    self.process_bulk_classifications()
                case "sapRFCs":
                    if self._sap and isinstance(self._sap, SAP):
                        self._log_header_callback("Process SAP RFCs")
                        self.process_sap_rfcs(sap_rfcs=self._sap_rfcs)
                    else:
                        self.logger.warning(
                            "SAP RFC in payload but SAP external system is not configured or not enabled! RFCs will not be processed.",
                        )
                case "sapRFCsPost":
                    if self._sap and isinstance(self._sap, SAP):
                        self._log_header_callback("Process SAP RFCs (post)")
                        self.process_sap_rfcs(
                            sap_rfcs=self._sap_rfcs_post,
                            section_name="sapRFCsPost",
                        )
                    else:
                        self.logger.warning(
                            "SAP RFC in payload but SAP external system is not configured or not enabled! RFCs will not be processed.",
                        )
                case "webReports":
                    self._log_header_callback(text="Process Web Reports")
                    restart_required = self.process_web_reports(
                        web_reports=self._web_reports,
                    )
                    if restart_required:
                        self.logger.info(
                            "Web Reports require a restart of OTCS services...",
                        )
                        # Restart OTCS frontend and backend pods:
                        self._otcs_restart_callback(
                            backend=self._otcs_backend,
                            frontend=self._otcs_frontend,
                        )
                case "webReportsPost":
                    self._log_header_callback(text="Process Web Reports (post)")
                    restart_required = self.process_web_reports(
                        web_reports=self._web_reports_post,
                        section_name="webReportsPost",
                    )
                    if restart_required:
                        self.logger.info(
                            "WebReports (Post) require a restart of OTCS services...",
                        )
                        # Restart OTCS frontend and backend pods:
                        self._otcs_restart_callback(
                            backend=self._otcs_backend,
                            frontend=self._otcs_frontend,
                        )
                case "additionalGroupMemberships":
                    self._log_header_callback(
                        text="Process additional group members for OTDS",
                    )
                    self.process_additional_group_members()
                case "additionalAccessRoleMemberships":
                    self._log_header_callback(
                        text="Process additional access role members for OTDS",
                    )
                    self.process_additional_access_role_members()
                case "additionalApplicationRoleAssignments":
                    self._log_header_callback(
                        text="Process additional application role assignments for OTDS",
                    )
                    self.process_additional_application_role_assignments()
                case "renamings":
                    self._log_header_callback(text="Process Node Renamings")
                    self.process_renamings()
                case "items":
                    self._log_header_callback(text="Process Items")
                    self.process_items(items=self._items)
                case "itemsPost":
                    self._log_header_callback(text="Process Items (post)")
                    self.process_items(items=self._items_post, section_name="itemsPost")
                case "permissions":
                    self._log_header_callback(text="Process Permissions")
                    self.process_permissions(permissions=self._permissions)
                case "permissionsPost":
                    self._log_header_callback(text="Process Permissions (post)")
                    self.process_permissions(
                        permissions=self._permissions_post,
                        section_name="permissionsPost",
                    )
                case "workspacePermissions":
                    self._log_header_callback(text="Process Workspace Permissions")
                    self.process_workspace_permissions()
                case "assignments":
                    self._log_header_callback(text="Process Assignments")
                    self.process_assignments()
                case "securityClearances":
                    self._log_header_callback(text="Process Security Clearances")
                    self.process_security_clearances()
                case "supplementalMarkings":
                    self._log_header_callback(text="Process Supplemental Markings")
                    self.process_supplemental_markings()
                case "recordsManagementSettings":
                    self._log_header_callback(
                        text="Process Records Management Settings",
                    )
                    self.process_records_management_settings()
                case "holds":
                    self._log_header_callback(text="Process Records Management Holds")
                    self.process_holds()
                case "documentGenerators":
                    # If a payload file (e.g. additional ones) does not have
                    # transportPackages then it can happen that the
                    # self._workspace_types is not yet initialized. As we need
                    # this structure for documentGenerators we initialize it here:
                    if not self._workspace_types:
                        self._log_header_callback(text="Process Workspace Types")
                        self.process_workspace_types()

                    self._log_header_callback(text="Process Document Generators")
                    self.process_document_generators()
                case "workflowInitiations":
                    # If a payload file (e.g. additional ones) does not have
                    # transportPackages then it can happen that the
                    # self._workspace_types is not yet initialized. As we need
                    # this structure for workflowInitiations we initialize it here:
                    if not self._workspace_types:
                        self._log_header_callback(text="Process Workspace Types")
                        self.process_workspace_types()

                    self._log_header_callback(text="Process Workflow Initiations")
                    self.process_workflows()
                case "browserAutomations":
                    self._log_header_callback(text="Process Browser Automations")
                    self.process_browser_automations(
                        browser_automations=self._browser_automations,
                    )
                case "browserAutomationsPost":
                    self._log_header_callback(text="Process Browser Automations (post)")
                    self.process_browser_automations(
                        browser_automations=self._browser_automations_post,
                        section_name="browserAutomationsPost",
                    )
                case "testAutomations":
                    self._log_header_callback(text="Process Test Automations")
                    self.process_browser_automations(
                        browser_automations=self._test_automations, section_name="testAutomations"
                    )
                case "workspaceTypes":
                    if not self._workspace_types:
                        self._log_header_callback(text="Process Workspace Types")
                        self.process_workspace_types()
                case "businessObjectTypes":
                    if not self._business_object_types:
                        self._log_header_callback(text="Process Business Object Types")
                        self.process_business_object_types()
                case "nifi":
                    self._log_header_callback("Process Knowledge Discovery Nifi Flows")
                    self.process_nifi_flows()
                case _:
                    self.logger.error(
                        "Illegal payload section name -> '%s' in payloadSections!",
                        payload_section["name"],
                    )
            payload_section_restart = payload_section.get("restart", False)
            if payload_section_restart:
                self.logger.info(
                    "Payload section -> '%s' requests a restart of OTCS services...",
                    payload_section["name"],
                )
                # Restart OTCS frontend and backend pods:
                self._otcs_restart_callback(
                    backend=self._otcs_backend,
                    frontend=self._otcs_frontend,
                )
            # Avoid out of cycle message for bulkDatasources if it is
            # passed in the payload:
            elif payload_section["name"] != "bulkDatasources":
                self.logger.info(
                    "Payload section -> '%s' does not require a restart of OTCS services.",
                    payload_section["name"],
                )

    with tracer.start_as_current_span("process_payload_users"):
        if self._users and self._user_customization:
            self._log_header_callback("Process User Profile Photos")
            self.process_user_photos()
            if self._m365 and isinstance(self._m365, M365):
                self._log_header_callback("Process M365 User Profile Photos")
                self.process_user_photos_m365()
            if self._salesforce and isinstance(self._salesforce, Salesforce):
                self._log_header_callback("Process Salesforce User Profile Photos")
                self.process_user_photos_salesforce()
            if self._core_share and isinstance(self._core_share, CoreShare):
                self._log_header_callback("Process Core Share User Profile Photos")
                self.process_user_photos_core_share()
            self._log_header_callback("Process User Favorites and Profiles")
            self.process_user_favorites_and_profiles()
            self._log_header_callback("Process User Security")
            self.process_user_security()

process_permission(node_id, node_name, permission, apply_to, workspace_id=None)

Process a single permission payload item for a given node.

Parameters:

Name Type Description Default
node_id int

The ID of the node.

required
node_name str

The name of the node.

required
permission dict

The permission payload.

required
apply_to int
  • 0 = Apply to this item only
  • 1 = Apply to sub-items only
  • 2 = Apply to this item and its sub-items (default)
  • 3 = Apply to this item and its immediate sub-items
required
workspace_id int | None

If role permissions should be set we also need the workspace_id. Use None if node is not part of a workspace or no role permissions should be set.

None

Returns:

Name Type Description
bool bool

True = success, False = Error

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_permission")
def process_permission(
    self,
    node_id: int,
    node_name: str,
    permission: dict,
    apply_to: int,
    workspace_id: int | None = None,
) -> bool:
    """Process a single permission payload item for a given node.

    Args:
        node_id (int):
            The ID of the node.
        node_name (str):
            The name of the node.
        permission (dict):
            The permission payload.
        apply_to (int):
            - 0 = Apply to this item only
            - 1 = Apply to sub-items only
            - 2 = Apply to this item and its sub-items (default)
            - 3 = Apply to this item and its immediate sub-items
        workspace_id (int | None):
            If role permissions should be set we also need the workspace_id.
            Use None if node is not part of a workspace or no role permissions
            should be set.

    Returns:
        bool:
            True = success, False = Error

    """

    # 1. Process Owner Permissions
    if "owner_permissions" in permission:
        owner_permissions = permission["owner_permissions"]
        self.logger.info(
            "Update owner permissions for item -> '%s' (%d) to -> %s.",
            node_name,
            node_id,
            str(owner_permissions),
        )
        response = self._otcs.assign_permission(
            node_id=int(node_id),
            assignee_type="owner",
            assignee=0,
            permissions=owner_permissions,
            apply_to=apply_to,
        )
        if not response:
            self.logger.error(
                "Failed to update owner permissions for item -> '%s' (%d)!",
                node_name,
                node_id,
            )
            return False

    # 2. Process Owner Group Permissions
    if "owner_group_permissions" in permission:
        owner_group_permissions = permission["owner_group_permissions"]
        self.logger.info(
            "Update owner group permissions for item -> '%s' (%d) to -> %s.",
            node_name,
            node_id,
            str(owner_group_permissions),
        )
        response = self._otcs.assign_permission(
            node_id=int(node_id),
            assignee_type="group",
            assignee=0,
            permissions=owner_group_permissions,
            apply_to=apply_to,
        )
        if not response:
            self.logger.error(
                "Failed to update group permissions for item -> '%s' (%d)!",
                node_name,
                node_id,
            )
            return False

    # 3. Process Public Permissions
    if "public_permissions" in permission:
        public_permissions = permission["public_permissions"]
        self.logger.info(
            "Update public permissions for item -> '%s' (%d) to -> %s.",
            node_name,
            node_id,
            str(public_permissions),
        )
        response = self._otcs.assign_permission(
            node_id=int(node_id),
            assignee_type="public",
            assignee=0,
            permissions=public_permissions,
            apply_to=apply_to,
        )
        if not response:
            self.logger.error(
                "Failed to update public permissions for item -> '%s' (%d)!",
                node_name,
                node_id,
            )
            return False
    # end if "public_permissions" in permission

    # 4. Process Assigned User Permissions (if specified and not empty)
    users = permission.get("users", [])
    for user in users:
        if "name" not in user or "permissions" not in user:
            self.logger.error(
                "Missing user name in user permission specificiation. Cannot set user permissions. Skipping...",
            )
            return False
        user_name = user["name"]
        if "permissions" not in user:
            self.logger.error(
                "Missing permissions in user -> '%s' permission specificiation. Cannot set user permissions. Skipping...",
                user_name,
            )
            return False
        user_permissions = user["permissions"]
        response = self._otcs.get_user(name=user_name, show_error=True)
        user_id = self._otcs.get_result_value(response=response, key="id")
        if not user_id:
            self.logger.error(
                "Cannot find user -> '%s'; cannot set user permissions. Skipping user...",
                user_name,
            )
            return False
        user["id"] = user_id  # write ID back into payload

        self.logger.info(
            "Update permission of user -> '%s' for item -> '%s' (%d) to -> %s.",
            user_name,
            node_name,
            node_id,
            str(user_permissions),
        )
        response = self._otcs.assign_permission(
            node_id=int(node_id),
            assignee_type="custom",
            assignee=user_id,
            permissions=user_permissions,
            apply_to=apply_to,
        )
        if not response:
            self.logger.error(
                "Failed to update assigned user permissions for item -> %s!",
                node_id,
            )
            return False
    # end for user in users

    # 5. Process Assigned Group Permissions (if specified and not empty)
    groups = permission.get("groups", [])
    for group in groups:
        if "name" not in group:
            self.logger.error(
                "Missing group name in group permission specificiation. Cannot set group permissions. Skipping...",
            )
            return False
        group_name = group["name"]
        if "permissions" not in group:
            self.logger.error(
                "Missing permissions in group -> '%s' permission specificiation. Cannot set group permissions. Skipping...",
                group_name,
            )
            return False
        group_permissions = group["permissions"]
        self.logger.info(
            "Update permissions of group -> '%s' for item -> '%s' (%d) to -> %s.",
            group_name,
            node_name,
            node_id,
            str(group_permissions),
        )
        otcs_group = self._otcs.get_group(name=group_name, show_error=True)
        group_id = self._otcs.get_result_value(response=otcs_group, key="id")
        if not group_id:
            self.logger.error(
                "Cannot find group -> '%s'; cannot set group permissions. Skipping group...",
                group_name,
            )
            return False
        group["id"] = group_id  # write ID back into payload
        response = self._otcs.assign_permission(
            node_id=int(node_id),
            assignee_type="custom",
            assignee=group_id,
            permissions=group_permissions,
            apply_to=apply_to,
        )
        if not response:
            self.logger.error(
                "Failed to update assigned group permissions for item -> '%s' (%d)!",
                node_name,
                node_id,
            )
            return False
    # end for group in groups

    # 6. Process Workspace Role Permissions (if specified and not empty)
    roles = permission.get("roles", [])
    if roles and not workspace_id:
        self.logger.error(
            "Cannot set workspace roles if no workspace ID is provided!",
        )
        return False
    for role in roles:
        if "name" not in role:
            self.logger.error(
                "Missing role name in role permission specificiation. Cannot set role permissions. Skipping...",
            )
            return False
        role_name = role["name"]
        if "permissions" not in role:
            self.logger.error(
                "Missing permissions in role -> '%s' permission specificiation. Cannot set role permissions. Skipping...",
                group_name,
            )
            return False
        role_permissions = role["permissions"]
        self.logger.info(
            "Update permissions of role -> '%s' for workspace item -> '%s' (%d) in workspace with ID -> %d to -> %s.",
            role_name,
            node_name,
            node_id,
            workspace_id,
            str(role_permissions),
        )
        response = self._otcs.get_workspace_roles(workspace_id=workspace_id)
        role_id = self._otcs.lookup_result_value(
            response=response,
            key="name",
            value=role_name,
            return_key="id",
        )
        if not role_id:
            self.logger.error(
                "Cannot find role -> '%s'; cannot set role permissions.",
                role_name,
            )
            return False
        response = self._otcs.assign_permission(
            node_id=int(node_id),
            assignee_type="custom",
            assignee=role_id,
            permissions=role_permissions,
            apply_to=apply_to,
        )
        if not response:
            self.logger.error(
                "Failed to update role permissions for item -> '%s' (%d)!",
                node_name,
                node_id,
            )
            return False
    # end for role in roles

    return True

process_permissions(permissions, section_name='permissions')

Process items specified in payload and upadate permissions.

Parameters:

Name Type Description Default
permissions list

A list of items to apply permissions to. Each list item in the payload is a dict with this structure: { nodeid = "..." volume = "..." nickname = "..." public_permissions = ["see", "see_content", ...] owner_permissions = [] owner_group_permissions = [] groups = [ { name = "..." permissions = [] } ] users = [ { name = "..." permissions = [] } ] apply_to = 2 }

required
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'permissions'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_permissions")
def process_permissions(
    self,
    permissions: list,
    section_name: str = "permissions",
) -> bool:
    """Process items specified in payload and upadate permissions.

    Args:
        permissions (list):
            A list of items to apply permissions to.
            Each list item in the payload is a dict with this structure:
            {
                nodeid = "..."
                volume = "..."
                nickname = "..."
                public_permissions = ["see", "see_content", ...]
                owner_permissions = []
                owner_group_permissions = []
                groups = [
                    {
                        name = "..."
                        permissions = []
                    }
                ]
                users = [
                    {
                        name = "..."
                        permissions = []
                    }
                ]
                apply_to = 2
            }
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not permissions:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for permission in permissions:
        if "path" not in permission and "volume" not in permission and "nickname" not in permission:
            self.logger.error(
                "Item to change permission is not specified (needs path, volume, or nickname). Skipping...",
            )
            success = False
            continue

        # Check if permission has been explicitly disabled in payload
        # (enabled = false). In this case we skip this payload element:
        if not permission.get("enabled", True):
            self.logger.info("Payload for Permission is disabled. Skipping...")
            continue

        node_id = 0

        # Check if "volume" is in payload and not empty string
        # we try to get the node ID from the volume type:
        if permission.get("volume"):
            volume_type = permission["volume"]
            self.logger.info(
                "Found volume type -> '%s' in permission payload. Determine volume ID...",
                volume_type,
            )
            node = self._otcs.get_volume(volume_type=volume_type)
            node_id = self._otcs.get_result_value(response=node, key="id")
            if not node_id:
                self.logger.error(
                    "Illegal volume -> '%s' in permission payload. Skipping...",
                    volume_type,
                )
                success = False
                continue
        else:
            # the following path block requires a value for the volume - if it is
            # not specified we take the Enterprise Workspace:
            volume_type = self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE

        # Check if "path" is in payload and not empty list
        # (path can be combined with volume so we need to take volume into account):
        if permission.get("path"):
            path = permission["path"]
            self.logger.info(
                "Found path -> '%s' in permission payload. Determine node ID...",
                path,
            )
            node = self._otcs.get_node_by_volume_and_path(
                volume_type=volume_type,
                path=path,
            )
            node_id = self._otcs.get_result_value(response=node, key="id")
            if not node_id:
                self.logger.error("Path -> '%s' does not exist. Skipping...", path)
                success = False
                continue

        # Check if "nickname" is in payload and not empty string:
        if permission.get("nickname"):
            nickname = permission["nickname"]
            self.logger.info(
                "Found nickname -> '%s' in permission payload. Determine node ID...",
                nickname,
            )
            node = self._otcs.get_node_from_nickname(nickname=nickname)
            node_id = self._otcs.get_result_value(response=node, key="id")
            if not node_id:
                self.logger.error(
                    "Nickname -> '%s' does not exist. Skipping...",
                    nickname,
                )
                success = False
                continue

        # Now we should have a value for node_id:
        if not node_id:
            self.logger.error("No node ID found! Skipping permission...")
            success = False
            continue

        node_name = self._otcs.get_result_value(response=node, key="name")
        self.logger.info(
            "Found node -> '%s' with ID -> %s to apply permission to.",
            node_name,
            node_id,
        )
        # write node information back into payload
        # for better debugging
        permission["node_name"] = node_name
        permission["node_id"] = node_id

        # Make item + sub-items (2) the default:
        apply_to = permission.get("apply_to", 2)

        # Prcess a single permission payload item:
        if not self.process_permission(
            node_id=node_id,
            node_name=node_name,
            permission=permission,
            apply_to=apply_to,
        ):
            success = False
            continue
    # end for permission in permissions

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=permissions,
    )

    return success

process_records_management_settings(section_name='recordsManagementSettings')

Process Records Management Settings for Content Server.

The setting files need to be placed in the OTCS file system file via a transport into the Support Asset Volume.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'recordsManagementSettings'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_records_management_settings")
def process_records_management_settings(
    self,
    section_name: str = "recordsManagementSettings",
) -> bool:
    """Process Records Management Settings for Content Server.

    The setting files need to be placed in the OTCS file system file via
    a transport into the Support Asset Volume.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._records_management_settings:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    if (
        "records_management_system_settings" in self._records_management_settings
        and self._records_management_settings["records_management_system_settings"] != ""
    ):
        filename = (
            self._custom_settings_dir + self._records_management_settings["records_management_system_settings"]
        )
        response = self._otcs.import_records_management_settings(file_path=filename)
        if not response:
            success = False

    if (
        "records_management_codes" in self._records_management_settings
        and self._records_management_settings["records_management_codes"] != ""
    ):
        filename = self._custom_settings_dir + self._records_management_settings["records_management_codes"]
        response = self._otcs.import_records_management_codes(file_path=filename)
        if not response:
            success = False

    if (
        "records_management_rsis" in self._records_management_settings
        and self._records_management_settings["records_management_rsis"] != ""
    ):
        filename = self._custom_settings_dir + self._records_management_settings["records_management_rsis"]
        response = self._otcs.import_records_management_rsis(file_path=filename)
        if not response:
            success = False

    if (
        "physical_objects_system_settings" in self._records_management_settings
        and self._records_management_settings["physical_objects_system_settings"] != ""
    ):
        filename = self._custom_settings_dir + self._records_management_settings["physical_objects_system_settings"]
        response = self._otcs.import_physical_objects_settings(file_path=filename)
        if not response:
            success = False

    if (
        "physical_objects_codes" in self._records_management_settings
        and self._records_management_settings["physical_objects_codes"] != ""
    ):
        filename = self._custom_settings_dir + self._records_management_settings["physical_objects_codes"]
        response = self._otcs.import_physical_objects_codes(file_path=filename)
        if not response:
            success = False

    if (
        "physical_objects_locators" in self._records_management_settings
        and self._records_management_settings["physical_objects_locators"] != ""
    ):
        filename = self._custom_settings_dir + self._records_management_settings["physical_objects_locators"]
        response = self._otcs.import_physical_objects_locators(file_path=filename)
        if not response:
            success = False

    if (
        "security_clearance_codes" in self._records_management_settings
        and self._records_management_settings["security_clearance_codes"] != ""
    ):
        filename = self._custom_settings_dir + self._records_management_settings["security_clearance_codes"]
        response = self._otcs.import_security_clearance_codes(file_path=filename)
        if not response:
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._records_management_settings,
    )

    return success

process_renamings(section_name='renamings')

Process renamings specified in payload and rename existing Content Server items.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'renamings'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_renamings")
def process_renamings(self, section_name: str = "renamings") -> bool:
    """Process renamings specified in payload and rename existing Content Server items.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._renamings:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for renaming in self._renamings:
        if "name" not in renaming:
            self.logger.error("Renamings require the new name!")
            continue
        if "nodeid" in renaming:
            node_id = renaming["nodeid"]
        elif "volume" in renaming:
            path = renaming.get("path")
            volume = renaming.get("volume")
            if path:
                self.logger.info(
                    "Found path -> '%s' in renaming payload. Determine node ID by volume and path...",
                    path,
                )
                node = self._otcs.get_node_by_volume_and_path(
                    volume_type=volume,
                    path=path,
                )
            else:
                # Determine object ID of volume:
                node = self._otcs.get_volume(volume_type=volume)
            node_id = self._otcs.get_result_value(response=node, key="id")
        elif "nickname" in renaming:
            nickname = renaming["nickname"]
            self.logger.info(
                "Found nickname -> '%s' in renaming payload. Determine node ID by nickname...",
                nickname,
            )
            node = self._otcs.get_node_from_nickname(nickname=nickname)
            node_id = self._otcs.get_result_value(response=node, key="id")
        else:
            self.logger.error(
                "Renamings require either a node ID or a volume (with an optional path) or a nickname! Skipping to next renaming...",
            )
            continue

        # Check if renaming has been explicitly disabled in payload
        # (enabled = false). In this case we skip this payload element:
        if not renaming.get("enabled", True):
            self.logger.info("Payload for renaming is disabled. Skipping...")
            continue

        response = self._otcs.rename_node(
            node_id=int(node_id),
            name=renaming["name"],
            description=renaming.get("description", ""),
        )
        if not response:
            self.logger.error(
                "Failed to rename node ID -> '%s' to new name -> '%s'!",
                node_id,
                renaming["name"],
            )
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._renamings,
    )

    return success

process_resources(section_name='resources')

Process OTDS resources in payload and create them in OTDS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'resources'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_resources")
def process_resources(self, section_name: str = "resources") -> bool:
    """Process OTDS resources in payload and create them in OTDS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._resources:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for resource in self._resources:
        resource_name = resource.get("name")
        if not resource_name:
            self.logger.error("OTDS resource payload does not have a name! Skipping...")
            success = False
            continue

        # Check if element has been disabled in payload (enabled = false).
        # In this case we skip the element (default is enabled):
        if not resource.get("enabled", True):
            self.logger.info(
                "Payload for OTDS Resource -> '%s' is disabled. Skipping...",
                resource_name,
            )
            continue

        resource_description = resource.get("description", "")
        display_name = resource.get("display_name", "")
        additional_payload = resource.get("additional_payload", {})
        activate_resource = resource.get("activate", True)
        resource_id = resource.get("resource_id", None)
        allow_impersonation = resource.get("allow_impersonation", True)
        secret = resource.get("secret", None)

        # Check if Partition does already exist
        # (in an attempt to make the code idem-potent)
        self.logger.info(
            "Check if OTDS resource -> '%s' does already exist...",
            resource_name,
        )
        response = self._otds.get_resource(name=resource_name, show_error=False)
        if response:
            self.logger.info(
                "OTDS Resource -> '%s' does already exist. Skipping...",
                resource_name,
            )
            continue

        # Only continue if Partition does not exist already
        self.logger.info(
            "Resource -> '%s' does not exist. Creating...",
            resource_name,
        )

        response = self._otds.add_resource(
            name=resource_name,
            description=resource_description,
            display_name=display_name,
            allow_impersonation=allow_impersonation,
            resource_id=resource_id,
            secret=secret,
            additional_payload=additional_payload,
        )
        if response:
            self.logger.info("Added OTDS resource -> '%s'.", resource_name)
        else:
            self.logger.error("Failed to add OTDS resource -> '%s'!", resource_name)
            success = False
            continue

        # If resource_id and secret are provided then the resource will
        # automatically be activated.
        if activate_resource and not secret:
            resource_id = response["resourceID"]
            self.logger.info(
                "Activate OTDS resource -> '%s' with ID -> %s...",
                resource_name,
                resource_id,
            )
            response = self._otds.activate_resource(resource_id=resource_id)

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._resources,
    )

    return success

process_sap_rfcs(sap_rfcs, section_name='sapRFCs')

Process SAP RFCs in payload and run them in SAP S/4HANA.

Parameters:

Name Type Description Default
sap_rfcs list

The payload list of SAP RFCs. As we have two different list (pre and post) we need to pass the actual list as parameter.

required
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'sapRFCs'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_sap_rfcs")
def process_sap_rfcs(self, sap_rfcs: list, section_name: str = "sapRFCs") -> bool:
    """Process SAP RFCs in payload and run them in SAP S/4HANA.

    Args:
        sap_rfcs (list):
            The payload list of SAP RFCs. As we have two different list (pre and post)
            we need to pass the actual list as parameter.
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not self._sap:
        self.logger.info("SAP object is undefined. Cannot call RFCs. Bailing out.")
        return False

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for sap_rfc in sap_rfcs:
        rfc_name = sap_rfc.get("name")
        if not rfc_name:
            self.logger.error("SAP RFC needs a name! Skipping...")
            success = False
            continue

        # Check if SAP RFC has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not sap_rfc.get("enabled", True):
            self.logger.info(
                "Payload for SAP RFC -> '%s' is disabled. Skipping...",
                rfc_name,
            )
            continue

        rfc_description = sap_rfc.get("description", "")

        # Check if we have SAP RFC call parameters in the payload. They are optional:
        rfc_params = sap_rfc.get("parameters", {})
        # Check if we have SAP RFC call options in the payload. They are optional:
        rfc_call_options = sap_rfc.get("call_options", {})

        if rfc_params:
            self.logger.info(
                "Calling SAP RFC -> '%s' (%s) with parameters -> %s%s...",
                rfc_name,
                rfc_description,
                str(rfc_params),
                " and options -> {}".format(rfc_call_options) if rfc_call_options else "",
            )
        else:
            self.logger.info(
                "Calling SAP RFC -> '%s' (%s)%s...",
                rfc_name,
                rfc_description,
                " with options -> {}".format(rfc_call_options) if rfc_call_options else "",
            )

        if rfc_call_options:
            self.logger.debug("Using call options -> '%s' ...", rfc_call_options)

        result = self._sap.call(rfc_name, rfc_call_options, rfc_params)
        if result is None:
            self.logger.error("Failed to call SAP RFC -> '%s'!", rfc_name)
            success = False
        elif result.get("RESULT") == "WARNING":
            self.logger.warning(
                "Result of SAP RFC -> '%s' has a warning -> '%s'.",
                rfc_name,
                str(result.get("COMMENT")).strip() if result.get("COMMENT") else str(result),
            )
        elif result.get("RESULT") != "OK":
            self.logger.error(
                "Result of SAP RFC -> '%s' is not OK, %sresult -> %s",
                rfc_name,
                "it returned -> '{}' failed items in".format(result.get("FAILED")) if result.get("FAILED") else "",
                str(result),
            )
            success = False
        else:
            self.logger.info(
                "Successfully called RFC -> '%s'. Result -> %s.",
                rfc_name,
                str(result),
            )
            # Save result for status file content
            sap_rfc["result"] = result

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=sap_rfcs,
    )

    return success

process_security_clearances(section_name='securityClearances')

Process Security Clearances for Content Server.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'securityClearances'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_security_clearances")
def process_security_clearances(
    self,
    section_name: str = "securityClearances",
) -> bool:
    """Process Security Clearances for Content Server.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._security_clearances:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for security_clearance in self._security_clearances:
        clearance_level = security_clearance.get("level")
        if not clearance_level:
            self.logger.error(
                "Security clearance requires a level in the payload. Skipping.",
            )
            continue
        clearance_name = security_clearance.get("name")
        if not clearance_name:
            self.logger.error(
                "Security clearance requires a name in the payload. Skipping.",
            )
            continue

        # Check if security clearance has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not security_clearance.get("enabled", True):
            self.logger.info(
                "Payload for security clearance -> '%s' is disabled. Skipping...",
                clearance_name,
            )
            continue

        clearance_description = security_clearance.get("description", "")
        if clearance_level and clearance_name:
            self.logger.info(
                "Creating security clearance -> '%s' : %s%s...",
                clearance_level,
                clearance_name,
                " ({})".format(clearance_description) if clearance_description else "",
            )
            self._otcs.run_web_report(
                nickname="web_report_security_clearance",
                web_report_parameters=security_clearance,
            )
        else:
            self.logger.error(
                "Cannot create security clearance - either level or name is missing!",
            )
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._security_clearances,
    )

    return success

process_sites_m365(section_name='sitesM365')

Process M365 groups in payload and configure SharePoint sites in Microsoft 365.

These are NOT the SharePoint sites for Business Workspaces which are created by Scheduled Bots (Jobs) from OTCS via the creation of MS teams (each MS Team has a SharePoint site behind it)!

These are the SharePoint sites for the departmental groups such as "Sales", "Procurement", "Enterprise Asset Management", ... Only departmental group that have a top-level folder with the exact same name as the Department are configured.

For each departmental group: 1. Determine a departmental folder in the Enterprise Workspace 2. Determine the M365 Group 3. Determine the SharePoint Site (based on the M365 group ID) 4. Determine the Page in the SharePoint site 5. Determine or create the SharePoint webpart for the OTCS browser 6. Create URL object pointing to SharePoint site inside top level department folder

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'sitesM365'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_sites_m365")
def process_sites_m365(self, section_name: str = "sitesM365") -> bool:
    """Process M365 groups in payload and configure SharePoint sites in Microsoft 365.

    These are NOT the SharePoint sites for Business Workspaces which are created
    by Scheduled Bots (Jobs) from OTCS via the creation of MS teams
    (each MS Team has a SharePoint site behind it)!

    These are the SharePoint sites for the departmental groups such as "Sales",
    "Procurement", "Enterprise Asset Management", ...
    Only departmental group that have a top-level folder with the exact same
    name as the Department are configured.

    For each departmental group:
    1. Determine a departmental folder in the Enterprise Workspace
    2. Determine the M365 Group
    3. Determine the SharePoint Site (based on the M365 group ID)
    4. Determine the Page in the SharePoint site
    5. Determine or create the SharePoint webpart for the OTCS browser
    6. Create URL object pointing to SharePoint site inside top level department folder

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not isinstance(self._m365, M365):
        self.logger.error(
            "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
            section_name,
        )
        return False

    if not self._groups:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for group in self._groups:
        if "name" not in group:
            self.logger.error("Group needs a name. Cannot configure SharePoint site for it. Skipping...")
            success = False
            continue
        group_name = group["name"]

        # Check if group has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not group.get("enabled", True):
            self.logger.info(
                "Payload for group -> '%s' is disabled. Cannot configure SharePoint site. Skipping...",
                group_name,
            )
            continue

        if "enable_o365" not in group or not group["enable_o365"]:
            self.logger.info(
                "Microsoft 365 is not enabled in payload for group -> '%s'. Skipping...",
                group_name,
            )
            continue

        #
        # 1. Determine a departmental folder in the Enterprise Workspace:
        #

        if group_name == "Innovate":
            folder_id = 2000
        else:
            folder = self._otcs.get_node_by_volume_and_path(
                volume_type=self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE,
                path=[group_name],
                show_error=False,
            )
            folder_id = self._otcs.get_result_value(response=folder, key="id")
        if not folder_id:
            self.logger.warning(
                "Group -> '%s' has no folder associated. Cannot configure SharePoint site. Skipping...",
                group_name,
            )
            continue

        #
        # 2. Determine the M365 Group
        #

        # Check if the M365 group does not exist (this should actually never happen at this point)
        m365_group_id = self.determine_group_id_m365(group=group)
        if not m365_group_id:
            # The "m365_id" value is set by the method process_groups_m365()
            self.logger.error(
                "No M365 group exist for group -> '%s' (M365 group creation may have failed). Skipping...",
                group_name,
            )
            success = False
            continue

        #
        # 3. Determine the SharePoint Site:
        #

        site = self._m365.get_sharepoint_site_for_group(group_id=m365_group_id)
        site_id = self._m365.get_result_value(response=site, key="id")
        if not site_id:
            self.logger.info(
                "M365 group -> '%s' has no M365 SharePoint site connected. Skipping...",
                group_name,
            )
            success = False
            continue
        site_name = self._m365.get_result_value(response=site, key="name")

        # Store the SharePoint site ID in the payload.
        group["m365_site_id"] = site_id
        group["m365_site_name"] = site_name
        group["m365_folder_id"] = folder_id

        self.logger.info(
            "Configure M365 SharePoint site -> '%s' (%s) for M365 group -> '%s'...",
            site_name,
            site_id,
            group_name,
        )

        #
        # 4. Determine the Page in the SharePoint site:
        #

        site_page_name = group.get("o365_site_page_name", "OpenText Content Management")

        site_page_id = None
        site_pages = self._m365.get_sharepoint_pages(site_id=site_id)
        if site_pages:
            site_page_id = self._m365.lookup_result_value(
                response=site_pages,
                key="title",
                value=site_page_name,
                return_key="id",
            )
            if site_page_id:
                self.logger.info(
                    "Found existing page -> '%s' (%s) for SharePoint site -> '%s' (%s).",
                    site_page_name,
                    site_page_id,
                    site_name,
                    site_id,
                )
        if not site_page_id:
            site_page = self._m365.add_sharepoint_page(site_id=site_id, page_name=site_page_name)
            if site_page:
                site_page_id = self._m365.get_result_value(response=site_page, key="id")
                self.logger.info(
                    "Successfully created page -> '%s' (%s) for SharePoint site -> '%s' (%s).",
                    site_page_name,
                    site_page_id,
                    site_name,
                    site_id,
                )
            else:
                self.logger.error(
                    "Failed to create page -> '%s' for SharePoint site -> '%s' (%s)!",
                    site_page_name,
                    site_name,
                    site_id,
                )
                success = False
                continue

        #
        # 5. Determine or create the SharePoint webpart for the OTCS browser:
        #

        # Get all webparts on the page:
        site_webparts = self._m365.get_sharepoint_webparts(site_id=site_id, page_id=site_page_id)
        # Check if there's already a webpart for the OTCS browser:
        site_webpart_id = self._m365.lookup_result_value(
            response=site_webparts,
            key="webPartType",
            value="cecfdba4-2e82-4538-9436-dbd1c4c01a80",  # OTCS Browser Type
            return_key="id",
        )
        if site_webpart_id:
            self.logger.info(
                "Found existing OTCS Browser webpart -> '%s'. Updating with folder ID -> %s...",
                site_webpart_id,
                str(folder_id),
            )
            # Update the webpart with the new ID (which has changed after redeployment):
            update_data = {
                "properties": {
                    "ContentServerFolderParentWP": "2000",
                    "ContentServerFolderSelectedWP": str(folder_id),
                },
            }
            response = self._m365.update_sharepoint_webpart(
                site_id=site_id,
                page_id=site_page_id,
                webpart_id=site_webpart_id,
                update_data=update_data,
            )
            if response:
                self.logger.info(
                    "Successfully updated OTCS Browser webpart -> '%s' with new folder ID -> %s.",
                    site_webpart_id,
                    str(folder_id),
                )
            else:
                self.logger.error(
                    "Failed to update OTCS Browser webpart -> '%s' with new folder ID -> %s!",
                    site_webpart_id,
                    str(folder_id),
                )
                success = False
                continue
        else:
            # Check if the section we want for the webpart does already
            # exist. Otherwise create it. Per default we use horizontal
            # section 2. This allows to have a banner as first section
            # that is not affected by the customizer and can be configured
            # manually:
            site_page_section_id = group.get("o365_site_page_section_id", 1)
            site_page_section_type = group.get("o365_site_page_section_type", "horizontalSections")
            self.logger.info(
                "Check if %s section -> %s on page -> '%s' (%s) of SharePoint site -> '%s' (%s) does already exist...",
                "horizontal" if site_page_section_type == "horizontalSections" else "vertical",
                site_page_section_id,
                site_page_name,
                site_page_id,
                site_name,
                site_id,
            )
            site_page_section = self._m365.get_sharepoint_section(
                site_id=site_id,
                page_id=site_page_id,
                section_type=site_page_section_type,
                section_id=site_page_section_id,
                show_error=False,
            )
            if not site_page_section:
                site_page_section = self._m365.add_sharepoint_section(
                    site_id=site_id,
                    page_id=site_page_id,
                    section_type=site_page_section_type,
                    section_id=site_page_section_id,
                )
                if not site_page_section:
                    success = False
                    continue
                self.logger.info(
                    "Created %s section -> %s on page -> '%s' (%s) of SharePoint site -> '%s' (%s)",
                    "horizontal" if site_page_section_type == "horizontalSections" else "vertical",
                    site_page_section_id,
                    site_page_name,
                    site_page_id,
                    site_name,
                    site_id,
                )
            else:
                self.logger.info(
                    "Using existing %s section -> %s on page -> '%s' (%s) of SharePoint site -> '%s' (%s)",
                    "horizontal" if site_page_section_type == "horizontalSections" else "vertical",
                    site_page_section_id,
                    site_page_name,
                    site_page_id,
                    site_name,
                    site_id,
                )

            # If the sharepointAppRootSite is not configured
            # we try to extract the site URL from the site ID
            # which typically has a format like this:
            # ideateqa.sharepoint.com,2c59000d-f3e7-44d1-9a8e-e5df82b8ab01,34b48533-af41-4743-8b41-185a21f0b80f
            site_url = (
                self._m365.config()["sharepointAppRootSite"]
                if self._m365.config()["sharepointAppRootSite"]
                else "https://" + site_id.split(",", 1)[0]
            )
            # Build the web part create data:
            create_data = {
                "@odata.type": "microsoft.graph.webPartData",
                "audiences": [],
                "dataVersion": "1.0",
                "description": "Browse, access and work with documents from OpenText Content Server.",
                "title": "Content Server Browser",
                "properties": {
                    "AzureAppId": self._m365.config()["clientId"],
                    "ContentServerURLWP": "",
                    "URLPrefixWP": "",
                    "SSOEnabledWP": "",
                    "ShowPersonalWorkspaceWP": "",
                    "ShowNavigationBreadcrumbWP": "",
                    "PageSizeWP": "",
                    "ContentServerFolderParentWP": "2000",
                    "ContentServerFolderSelectedWP": str(folder_id),
                    "ContentServerFolderDisplayWP": group_name,
                    "SettingStorageURLSite": site_url,
                    "ContentServerURLSite": self._otcs.config()["csPublicUrl"],
                    "URLPrefixSite": "/cssupport",
                    "ShowPersonalWorkspaceSite": "",
                    "ShowNavigationBreadcrumbSite": "",
                    "PageSizeSite": "",
                    "ContentServerFolderParentSite": "",
                    "ContentServerFolderSelectedSite": "",
                    "ContentServerFolderDisplaySite": "",
                    "SSOEnabledSite": "",
                    "SettingStorageURLGL": "{}/sites/appcatalog".format(
                        site_url,
                    ),
                    "ContentServerURLGL": self._otcs.config()["csPublicUrl"],
                    "URLPrefixGL": "/cssupport",
                    "ShowPersonalWorkspaceGL": "No",
                    "ShowNavigationBreadcrumbGL": "No",
                    "PageSizeGL": "",
                    "ContentServerFolderParentGL": "",
                    "ContentServerFolderSelectedGL": "",
                    "ContentServerFolderDisplayGL": "",
                    "SSOEnabledGL": "No",
                    "TargetPlatform": "SPOnline",
                    "ConfigurationSiteUrl": "",
                    "WebPartVersion": "23.4.0.0",
                    "ContentServerURLAltGL": "",
                    "SavedQueryIdGL": "2344",
                    "SavedQueryParentIdGL": "2329",
                    "SavedQueryNameGL": "Full Text Business Workspaces Search",
                    "isCurrentUserSiteAdmin": True,
                    "isCurrentUserGlobalAdmin": True,
                    "isTeamsContext": False,
                    "ContentServerUrlLocal": self._otcs.config()["csPublicUrl"],
                },
            }

            response = self._m365.add_sharepoint_webpart(
                site_id=site_id,
                page_id=site_page_id,
                webpart_type_id="cecfdba4-2e82-4538-9436-dbd1c4c01a80",
                section_id=site_page_section_id,
                create_data=create_data,
            )
            if response:
                self.logger.info(
                    "Successfully added OTCS browser webpart -> '%s' to page -> '%s' (%s) on site ->'%s' (%s).",
                    self._m365.get_result_value(response=response, key="id"),
                    site_page_name,
                    site_page_id,
                    site_name,
                    site_id,
                )
            else:
                self.logger.error(
                    "Failed to add OTCS browser webpart to page -> '%s' (%s) on site ->'%s' (%s)!",
                    site_page_name,
                    site_page_id,
                    site_name,
                    site_id,
                )
                success = False
            # end else
        # end else

        #
        # 6. Create URL object pointing to SharePoint site inside top level department folder
        #

        item_name = (
            "SharePoint site for {} department".format(group_name)
            if folder_id != 2000
            else "SharePoint site for Innovate"
        )
        response = self._otcs.get_node_by_parent_and_name(parent_id=folder_id, name=item_name)
        item_id = self._otcs.get_result_value(response=response, key="id")
        if not item_id:
            response = self._otcs.create_item(
                parent_id=folder_id,
                item_type=self._otcs.ITEM_TYPE_URL,
                item_name=item_name,
                url=site["webUrl"],
            )
            self.logger.info(
                "Created URL item -> '%s' (%s).",
                item_name,
                site["webUrl"],
            )
        else:
            self.logger.info(
                "URL item -> '%s' (%s) does already exist.",
                item_name,
                site["webUrl"],
            )
    # end for group in self._groups:

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._groups,
    )

    return success

process_supplemental_markings(section_name='supplementalMarkings')

Process Supplemental Markings for Content Server.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'supplementalMarkings'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_supplemental_markings")
def process_supplemental_markings(
    self,
    section_name: str = "supplementalMarkings",
) -> bool:
    """Process Supplemental Markings for Content Server.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._supplemental_markings:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for supplemental_marking in self._supplemental_markings:
        code = supplemental_marking.get("code")

        # Check if supplemental marking has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not supplemental_marking.get("enabled", True):
            self.logger.info(
                "Payload for supplemental marking -> '%s' is disabled. Skipping...",
                code,
            )
            continue

        description = supplemental_marking.get("description", "")

        if code:
            self.logger.info(
                "Creating supplemental marking -> '%s'%s...",
                code,
                " ({})".format(description) if description else "",
            )
            self._otcs.run_web_report(
                nickname="web_report_supplemental_marking",
                web_report_parameters=supplemental_marking,
            )
        else:
            self.logger.error(
                "Cannot create supplemental marking - either code or description is missing!",
            )
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._supplemental_markings,
    )

    return success

process_synchronized_partitions(section_name='synchronizedPartitions')

Process OTDS synchronized partitions in payload and create them in OTDS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'synchronizedPartitions'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_synchronized_partitions")
def process_synchronized_partitions(
    self,
    section_name: str = "synchronizedPartitions",
) -> bool:
    """Process OTDS synchronized partitions in payload and create them in OTDS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    # check if section present, if not return True
    if not self._synchronized_partitions:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True
    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success = True

    for partition in self._synchronized_partitions:
        partition_name = partition["spec"].get("profileName") if "spec" in partition else None
        if not partition_name:
            self.logger.error(
                "Synchronized partition does not have a profile name! Skipping...",
            )
            success = False
            continue

        # Check if partition has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not partition.get("enabled", True):
            self.logger.info(
                "Payload for synchronized partition -> '%s' is disabled. Skipping...",
                partition_name,
            )
            continue

        partition_description = partition["spec"].get("description", "")

        # Check if Partition does already exist
        # (in an attempt to make the code idem-potent)
        self.logger.info(
            "Check if OTDS synchronized partition -> '%s' does already exist...",
            partition_name,
        )
        response = self._otds.get_partition(name=partition_name, show_error=False)
        if response:
            self.logger.info(
                "Synchronized partition -> '%s' does already exist.",
                partition_name,
            )
        else:
            # Only continue if synchronized Partition does not exist already
            self.logger.info(
                "Synchronized partition -> '%s' does not yet exist. Creating...",
                partition_name,
            )

            response = self._otds.add_synchronized_partition(
                name=partition_name,
                description=partition_description,
                data=partition["spec"],
            )
            if response:
                self.logger.info(
                    "Added synchronized partition -> '%s' to OTDS.",
                    partition_name,
                )
            else:
                self.logger.error(
                    "Failed to add synchronized partition -> '%s' to OTDS!",
                    partition_name,
                )
                success = False
                continue

        result = self._otds.import_synchronized_partition_members(
            name=partition_name,
        )
        if result:
            self.logger.info(
                "Successfully imported members to OTDS synchronized partition -> '%s'.",
                partition_name,
            )
        else:
            self.logger.error(
                "Failed to import members to OTDS synchronized partition -> '%s'!",
                partition_name,
            )
            success = False

        access_role = partition.get("access_role")
        if access_role:
            response = self._otds.add_partition_to_access_role(
                access_role=access_role,
                partition=partition_name,
            )
            if response:
                self.logger.info(
                    "Added OTDS synchronized partition -> '%s' to access role -> '%s'.",
                    partition_name,
                    access_role,
                )
            else:
                self.logger.error(
                    "Failed to add OTDS synchronized partition -> '%s' to access role -> '%s'!",
                    partition_name,
                    access_role,
                )
                success = False
        # end if access_role

        # Partions may have an optional list of licenses in
        # the payload. Assign the partition to all these licenses:
        partition_specific_licenses = partition.get("licenses")
        if partition_specific_licenses:
            # We assume these licenses are Extended ECM licenses!
            otcs_resource_name = self._otcs.config()["resource"]
            otcs_resource = self._otds.get_resource(name=otcs_resource_name)
            if not otcs_resource:
                self.logger.error(
                    "Cannot find OTCS resource -> '%s'! Skipping...",
                    otcs_resource_name,
                )
                success = False
                continue
            otcs_resource_id = otcs_resource["resourceID"]
            otcs_license_name = "EXTENDED_ECM"
            for license_item in partition_specific_licenses:
                if isinstance(license_item, dict):
                    license_feature = license_item.get("feature")
                    license_name = license_item.get("name", "EXTENDED_ECM")
                    if "enabled" in license_item and not license_item["enabled"]:
                        self.logger.info(
                            "Payload for License '%s' -> '%s' is disabled. Skipping...",
                            license_name,
                            license_feature,
                        )
                        continue
                    if "resource" in license_item:
                        try:
                            resource_id = self._otds.get_resource(name=license_item["resource"])["resourceID"]
                        except Exception:
                            self.logger.error(
                                "Error getting resource ID from resource -> %s", license_item["resource"]
                            )
                    else:
                        resource_id = otcs_resource_id

                elif isinstance(license_item, str):
                    license_feature = license_item
                    resource_id = otcs_resource_id
                    license_name = otcs_license_name
                else:
                    self.logger.error("Invalid License feature specified -> %s!", license_item)
                    success = False
                    continue

                if self._otds.is_partition_licensed(
                    partition_name=partition_name,
                    resource_id=resource_id,
                    license_feature=license_feature,
                    license_name=license_name,
                ):
                    self.logger.info(
                        "Partition -> '%s' is already licensed for -> '%s' (%s).",
                        partition_name,
                        license_name,
                        license_feature,
                    )
                    continue

                assigned_license = self._otds.assign_partition_to_license(
                    partition_name=partition_name,
                    resource_id=resource_id,
                    license_feature=license_feature,
                    license_name=license_name,
                )

                if not assigned_license:
                    self.logger.error(
                        "Failed to assign synchronized partition -> '%s' to license feature -> '%s' of license -> '%s'!",
                        partition_name,
                        license_feature,
                        license_name,
                    )
                    success = False
                else:
                    self.logger.info(
                        "Successfully assigned synchronized partition -> '%s' to license feature -> '%s' of license -> '%s'.",
                        partition_name,
                        license_feature,
                        license_name,
                    )
        # end if partition_specific_licenses

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._synchronized_partitions,
    )

    return success

process_system_attributes(section_name='systemAttributes')

Process OTDS system attributes in payload and create them in OTDS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'systemAttributes'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_system_attributes")
def process_system_attributes(self, section_name: str = "systemAttributes") -> bool:
    """Process OTDS system attributes in payload and create them in OTDS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._system_attributes:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for system_attribute in self._system_attributes:
        attribute_name = system_attribute.get("name")
        if not attribute_name:
            self.logger.error("OTDS system attribute needs a name in payload! Skipping...")
            success = False
            continue

        # Check if system attribute has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not system_attribute.get("enabled", True):
            self.logger.info(
                "Payload for OTDS system attribute -> '%s' is disabled. Skipping...",
                attribute_name,
            )
            continue

        attribute_value = system_attribute.get("value")
        if not attribute_value:
            self.logger.error(
                "OTDS system attribute -> '%s' needs a value in payload! Skipping...",
                attribute_name,
            )
            continue

        attribute_description = system_attribute.get("description", "")

        # Add the system attribute to OTDS:
        response = self._otds.add_system_attribute(
            name=attribute_name,
            value=attribute_value,
            description=attribute_description,
        )
        if response:
            self.logger.info(
                "Added OTDS system attribute -> '%s' with value -> '%s'.",
                attribute_name,
                attribute_value,
            )
        else:
            self.logger.error(
                "Failed to add OTDS system attribute -> '%s' with value -> '%s'!",
                attribute_name,
                attribute_value,
            )
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._system_attributes,
    )

    return success

process_teams_m365(section_name='teamsM365')

Process groups in payload and create matching Teams in Microsoft 365.

We need to do this after the creation of the M365 users as we require Group Owners to create teams. These are NOT the teams for OTCS workspaces! Those are created by Scheduled Bots (Jobs) from OTCS!

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'teamsM365'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_teams_m365")
def process_teams_m365(self, section_name: str = "teamsM365") -> bool:
    """Process groups in payload and create matching Teams in Microsoft 365.

    We need to do this after the creation of the M365 users as we require
    Group Owners to create teams. These are NOT the teams for OTCS
    workspaces! Those are created by Scheduled Bots (Jobs) from OTCS!

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not isinstance(self._m365, M365):
        self.logger.error(
            "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
            section_name,
        )
        return False

    if not self._groups:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for group in self._groups:
        if "name" not in group:
            self.logger.error("Team needs a name. Skipping...")
            success = False
            continue
        group_name = group["name"]

        # Check if group has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not group.get("enabled", True):
            self.logger.info(
                "Payload for group -> '%s' is disabled. Skipping...",
                group_name,
            )
            continue

        if "enable_o365" not in group or not group["enable_o365"]:
            self.logger.info(
                "Microsoft 365 is not enabled in payload for group -> '%s'. Skipping...",
                group_name,
            )
            continue

        # Check if the M365 group does not exist (this should actually never happen at this point)
        m365_group_id = self.determine_group_id_m365(group=group)
        if not m365_group_id:
            # The "m365_id" value is set by the method process_groups_m365()
            self.logger.error(
                "No M365 group exist for group -> '%s' (M365 group creation may have failed). Skipping...",
                group_name,
            )
            success = False
            continue

        if self._m365.has_team(group_name=group_name):
            self.logger.info(
                "M365 group -> '%s' already has an MS Team connected. Skipping...",
                group_name,
            )
            continue

        self.logger.info(
            "Create M365 team -> '%s' for existing M365 group -> '%s'...",
            group_name,
            group_name,
        )
        # Now "upgrading" this group to a MS Team:
        new_team = self._m365.add_team(name=group_name)
        if not new_team:
            success = False
            continue

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._groups,
    )

    return success

process_teams_m365_apps(section_name='teamsM365Apps', tab_name='Extended ECM')

Process groups in payload and configure Extended ECM Teams Apps.

The app is exposed as a tab called "Extended ECM" in the "General" channel of the M365 Team.

We need to do this after the transports as we need top level folders we can point the Extended ECM teams app to.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'teamsM365Apps'
tab_name str

Name of the Extended ECM tab. Default is "Extended ECM".

'Extended ECM'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_teams_m365_apps")
def process_teams_m365_apps(
    self,
    section_name: str = "teamsM365Apps",
    tab_name: str = "Extended ECM",
) -> bool:
    """Process groups in payload and configure Extended ECM Teams Apps.

    The app is exposed as a tab called "Extended ECM" in the "General"
    channel of the M365 Team.

    We need to do this after the transports as we need top level folders
    we can point the Extended ECM teams app to.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.
        tab_name (str, optional):
            Name of the Extended ECM tab. Default is "Extended ECM".

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not isinstance(self._m365, M365):
        self.logger.error(
            "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
            section_name,
        )
        return False

    if not self._groups:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # Determine the ID of the Extended ECM App:
    app_name = self._m365.config()["teamsAppName"]
    app_internal_id = self._m365.config()["teamsAppInternalId"]
    if not app_internal_id:
        response = self._m365.get_teams_apps(
            filter_expression="contains(displayName, '{}')".format(app_name),
        )
        # Get the App catalog ID:
        app_internal_id = self._m365.get_result_value(
            response=response,
            key="id",
            index=0,
        )
    if not app_internal_id:
        self.logger.error("M365 Teams app -> '%s' not found in catalog!", app_name)
        return False

    for group in self._groups:
        if "name" not in group:
            self.logger.error("Team needs a name. Skipping...")
            success = False
            continue
        group_name = group["name"]

        # Check if group has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not group.get("enabled", True):
            self.logger.info(
                "Payload for group -> '%s' is disabled. Skipping...",
                group_name,
            )
            continue

        if "enable_o365" not in group or not group["enable_o365"]:
            self.logger.info(
                "Microsoft 365 is not enabled in payload for group -> '%s'. Skipping...",
                group_name,
            )
            continue

        #
        # Now we create a tab in the "General" channel for the Extended ECM Teams App
        #

        # 1. Check if the tab is already assigned to the General channel.
        # This determines if we need to create or update the tab / app:
        app_name = self._m365.config()["teamsAppName"]
        response = self._m365.get_team_channel_tabs(
            team_name=group_name,
            channel_name="General",
        )
        # Check if tab is already there:
        if self._m365.exist_result_item(
            response=response,
            key="displayName",
            value=tab_name,
        ):
            self.logger.info(
                "M365 Teams app -> '%s' is already configured for M365 Team -> '%s' (tab -> '%s' does already exist). Updating it with new URLs and IDs...",
                app_name,
                group_name,
                tab_name,
            )
            update = True  # update existing tab
        else:
            self.logger.info(
                "Add tab -> '%s' with app -> '%s' to channel -> 'General' of M365 Team -> '%s' ",
                tab_name,
                app_name,
                group_name,
            )
            update = False  # create new tab

        # 2. Determine the M365 Team ID. If the team is not found then skip:
        response = self._m365.get_team(name=group_name)
        team_id = self._m365.get_result_value(response=response, key="id", index=0)
        if not team_id:
            self.logger.error("M365 Team -> '%s' not found!", group_name)
            success = False
            continue

        # 3. Install the App for the particular M365 Team (if it is not yet installed):
        response = self._m365.get_teams_apps_of_team(
            team_id=team_id,
            filter_expression="contains(teamsAppDefinition/displayName, '{}')".format(
                app_name,
            ),
        )
        if self._m365.exist_result_item(
            response=response,
            key="displayName",
            value=app_name,
            sub_dict_name="teamsAppDefinition",
        ):
            self.logger.info(
                "M365 Teams app -> '%s' is already installed for M365 Team -> '%s' (%s). Trying to upgrade app...",
                app_name,
                group_name,
                team_id,
            )
            response = self._m365.upgrade_teams_app_of_team(
                team_id=team_id,
                app_name=app_name,
            )
            if not response:
                self.logger.error(
                    "Failed to upgrade the existing app -> '%s' for the M365 Team -> '%s'!",
                    app_name,
                    group_name,
                )
                success = False
                continue
        else:
            self.logger.info(
                "Install M365 Teams app -> '%s' for M365 team -> '%s'...",
                app_name,
                group_name,
            )
            response = self._m365.assign_teams_app_to_team(
                team_id=team_id,
                app_id=app_internal_id,
            )
            if not response:
                self.logger.error(
                    "Failed to install app -> '%s' (%s) for M365 Team -> '%s'!",
                    app_name,
                    app_internal_id,
                    group_name,
                )
                success = False
                continue

        # 4. Create a Tab in the "General" channel of the M365 Team:
        if group_name == "Innovate":
            # Use the Enterprise Workspace for the
            # top-level group "Innovate":
            node_id = 2000
        else:
            # We assume the departmental group names are identical to
            # top-level folders in the Enterprise volume
            node = self._otcs.get_node_by_parent_and_name(
                parent_id=2000,
                name=group_name,
            )
            node_id = self._otcs.get_result_value(response=node, key="id")
            if not node_id:
                self.logger.info(
                    "Cannot find a top-level folder for group -> '%s'. Cannot configure M365 Teams app. Skipping...",
                    group_name,
                )
                continue

        app_url = self._otcs_frontend.cs_support_public_url()  # it is important to use the frontend pod URL here
        app_url += "/xecmoffice/teamsapp.html?nodeId="
        app_url += str(node_id) + "&type=container&parentId=2000&target=content&csurl="
        app_url += self._otcs_frontend.cs_public_url()
        app_url += "&appId=" + app_internal_id

        if update:
            # App / Tab exist but needs to be updated with new
            # IDs for the new deployment of Extended ECM
            # as the M365 Teams survive between Terrarium deployments:

            self.logger.info(
                "Updating tab -> '%s' of M365 Team channel -> 'General' for app -> '%s' (%s) with new URLs and node IDs...",
                tab_name,
                app_name,
                app_internal_id,
            )

            response = self._m365.update_teams_app_of_channel(
                team_name=group_name,
                channel_name="General",
                tab_name=tab_name,
                app_url=app_url,
                cs_node_id=node_id,
            )
        else:
            # Tab does not exist in "General" channel so we
            # add / configure it freshly:

            self.logger.info(
                "Adding tab -> '%s' with app -> '%s' (%s) in M365 Team channel -> 'General'...",
                tab_name,
                app_name,
                app_internal_id,
            )

            response = self._m365.add_teams_app_to_channel(
                team_name=group_name,
                channel_name="General",
                app_id=app_internal_id,
                tab_name=tab_name,
                app_url=app_url,
                cs_node_id=node_id,
            )
            if not response:
                self.logger.error(
                    "Failed to add tab -> '%s' with app -> '%s' (%s) to M365 Team channel -> 'General'!",
                    tab_name,
                    app_name,
                    app_internal_id,
                )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._groups,
    )

    return success

process_transport_packages(transport_packages, section_name='transportPackages')

Process transport packages in payload and import them to Content Server.

Parameters:

Name Type Description Default
transport_packages list

A list of transport packages. As we have three different lists (transport, content_transport, transport_post) we need a parameter to pass it.

required
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'transportPackages'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_transport_packages")
def process_transport_packages(
    self,
    transport_packages: list,
    section_name: str = "transportPackages",
) -> bool:
    """Process transport packages in payload and import them to Content Server.

    Args:
        transport_packages (list):
            A list of transport packages. As we have three different lists (transport,
            content_transport, transport_post) we need a parameter to pass it.
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not transport_packages:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for transport_package in transport_packages:
        name = transport_package.get("name")
        if not name:
            self.logger.error(
                "Transport package needs a name! Skipping to next transport...",
            )
            success = False
            continue

        # Check if transport package has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not transport_package.get("enabled", True):
            self.logger.info(
                "Payload for transport package -> '%s' is disabled. Skipping...",
                name,
            )
            continue

        url = transport_package.get("url")
        if not url:
            self.logger.error(
                "Transport package -> '%s' needs a URL! Skipping to next transport...",
                name,
            )
            success = False
            continue

        description = transport_package.get("description", "")
        if not description:
            self.logger.warning(
                "Transport package -> '%s' is missing a description.",
                name,
            )

        # if the transport is not in the local file system
        # but given by an URL we download it first:
        if url.startswith("http"):
            package_path = self.download_transport_package(package_url=url)
            if not package_path:
                success = False
                continue
            url = package_path

        # For some transports there can be string replacements
        # configured:
        if "replacements" in transport_package:
            replacements = transport_package["replacements"]
            self.logger.info(
                "Transport -> '%s' has replacements -> %s.",
                name,
                str(replacements),
            )
        else:
            replacements = None

        # For some transports there can be data extractions
        # configured:
        if "extractions" in transport_package:
            extractions = transport_package["extractions"]
            self.logger.info(
                "Transport -> '%s' has extractions -> %s.",
                name,
                str(extractions),
            )
        else:
            extractions = None

        if description:
            self.logger.info("Deploy transport -> '%s' ('%s')...", name, description)
        else:
            self.logger.info("Deploy transport -> '%s'...", name)

        response = self._otcs.deploy_transport(
            package_url=url,
            package_name=name,
            package_description=description,
            replacements=replacements,
            extractions=extractions,
        )
        if response is None:
            self.logger.error("Failed to deploy transport -> '%s'!", name)
            success = False
            if self._stop_on_error:
                break  # stop the for loop
        else:
            self.logger.info("Successfully deployed transport -> '%s'.", name)
            # Save the extractions for later processing, e.g. in process_business_object_types()
            if extractions:
                self.add_transport_extractions(extractions=extractions)
    # end for transports

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=transport_packages,
    )
    self.write_status_file(
        success=success,
        payload_section_name=section_name + "Extractions",
        payload_section=self._transport_extractions,
    )

    # If stop_on_error is enabled we want to completely
    # stop the execution of the customizer and bail out:
    if not success and self._stop_on_error:
        raise StopOnError(message="STOP_ON_ERROR enabled -> Stopping execution")

    return success

process_trusted_sites(section_name='trustedSites')

Process OTDS trusted sites in payload and create them in OTDS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'trustedSites'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_trusted_sites")
def process_trusted_sites(self, section_name: str = "trustedSites") -> bool:
    """Process OTDS trusted sites in payload and create them in OTDS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise

    """

    if not self._trusted_sites:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for trusted_site in self._trusted_sites:
        # old payload may still have trusted sites as list of string
        # we changed also the trusted sites to dict with 23.3
        # We want to be backwards compatible so we handle both cases:
        url = None
        if isinstance(trusted_site, dict):
            url = trusted_site.get("url")
        elif isinstance(trusted_site, str):
            url = trusted_site

        # Check if element has been disabled in payload (enabled = false).
        # In this case we skip the element:
        if isinstance(trusted_site, dict) and "enabled" in trusted_site and not trusted_site["enabled"]:
            self.logger.info(
                "Payload for OTDS Trusted Site -> '%s' is disabled. Skipping...",
                url if url else "<undefined>",
            )
            continue

        if not url:
            self.logger.error("OTDS Trusted site does not have a URL! Skipping...")
            success = False
            continue

        response = self._otds.add_trusted_site(trusted_site=url)
        if response:
            self.logger.info("Added OTDS trusted site -> '%s'.", url)
        else:
            self.logger.error("Failed to add trusted site -> '%s'!", url)
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._trusted_sites,
    )

    return success

process_user_favorites_and_profiles(section_name='userFavoritesAndProfiles')

Process user favorites in payload and create them in Content Server.

This method also simulates browsing the favorites to populate the widgets on the landing pages and sets personal preferences.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'userFavoritesAndProfiles'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_favorites_and_profiles")
def process_user_favorites_and_profiles(
    self,
    section_name: str = "userFavoritesAndProfiles",
) -> bool:
    """Process user favorites in payload and create them in Content Server.

    This method also simulates browsing the favorites to populate the
    widgets on the landing pages and sets personal preferences.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    for dic in self._payload_sections:
        if dic.get("name") == "users":
            users_payload = dic
            break
    smartui_theme = "jato" if users_payload.get("jato_enabled", True) else "cf"

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # We can only set favorites if we impersonate / authenticate as the user.
    # The following code (for loop) will change the authenticated user - we need to
    # switch it back to admin user later so we safe the admin credentials for this:

    for user in self._users:
        with tracer.start_as_current_span("process_user_favorites_and_profiles-user") as t:
            t.set_attribute("class", "otcs")
            user_name = user.get("name")
            if not user_name:
                self.logger.error(
                    "Missing user name - cannot configure user favorites and profile. Skipping user...",
                )
                continue

            self._log_header_callback(
                text="Process Favorites and Profile for user -> '{}'".format(user_name),
                char="-",
            )

            # Check if user has been explicitly disabled in payload
            # (enabled = false). In this case we skip the element:
            if not user.get("enabled", True):
                self.logger.info(
                    "Payload for user -> '%s' is disabled. Skipping...",
                    user_name,
                )
                continue

            # We skip also user of type "ServiceUser":
            if user.get("type", "User") == "ServiceUser":
                self.logger.info("Skipping service user -> '%s'...", user_name)
                continue

            # Impersonate as the user:
            self.logger.info("Impersonate user -> '%s'...", user_name)
            result = self.start_impersonation(username=user_name)
            if not result:
                self.logger.error("Couldn't impersonate user -> '%s'!", user_name)
                success = False
                continue

            # Configure default Theme to be Jato if configured
            user_smartui_theme = user.get("smartui_theme", smartui_theme)
            if user_smartui_theme != "cf":
                response = self._otcs.update_user_profile(
                    config_section="SmartUI",
                    field="theme",
                    value=user_smartui_theme,
                )
                if response is None:
                    self.logger.warning(
                        "Profile for user -> '%s' couldn't be updated to Smart View theme -> '%s'!",
                        user_name,
                        user_smartui_theme,
                    )
                else:
                    self.logger.info(
                        "Profile for user -> '%s' has been updated to Smart View theme -> '%s'.",
                        user_name,
                        user_smartui_theme,
                    )

            # Update the user profile to activate responsive (dynamic) containers:
            response = self._otcs.update_user_profile(
                field="responsiveContainerMode",
                value=True,
                config_section="SmartUI",
            )
            if response is None:
                self.logger.warning(
                    "Profile for user -> '%s' couldn't be updated with responsive container mode!",
                    user_name,
                )
            else:
                self.logger.info(
                    "Profile for user -> '%s' has been updated to enable responsive container mode.",
                    user_name,
                )
            response = self._otcs.update_user_profile(
                field="responsiveContainerMessageMode",
                value=True,
                config_section="SmartUI",
            )
            if response is None:
                self.logger.warning(
                    "Profile for user -> '%s' couldn't be updated with responsive container message mode!",
                    user_name,
                )
            else:
                self.logger.info(
                    "Profile for user -> '%s' has been updated to enable messages for responsive container mode.",
                    user_name,
                )

            restrict_personal_workspace = user.get("restrict_personal_workspace", False)
            if restrict_personal_workspace:
                # Let the user restrict itself to have read-only access to its
                # personal workspace:
                node = self._otcs.get_node_by_volume_and_path(
                    volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
                    path=[],
                )
                node_id = self._otcs.get_result_value(response=node, key="id")
                if node_id:
                    self.logger.info(
                        "Restricting Personal Workspace of user -> '%s' to read-only.",
                        user_name,
                    )
                    response = self._otcs.assign_permission(
                        node_id=int(node_id),
                        assignee_type="owner",
                        assignee=0,
                        permissions=["see", "see_contents"],
                        apply_to=2,
                    )

            # We work through the list of favorites defined for the user:
            favorites = user.get("favorites", [])
            for favorite in favorites:
                # We try three things to determine the favorite node:
                # 1. If the favorite is a path (a python list) then we try to resolve
                #    this path in the Enterprise Workspace.
                # 2. We try to find the item (string) as a logical
                #    workspace ID inside the payload.
                # 3. We try to find the item (string) as a nickname in OTCS.
                favorite_id = None
                is_workspace = False
                # 1. Check if the favorite item is itself a list,
                #    then we try to interpret it as a path in
                #    the enterprise workspace:
                if isinstance(favorite, list):
                    favorite_item = self._otcs.get_node_by_volume_and_path(
                        volume_type=self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE,
                        path=favorite,
                    )
                    if not favorite_item:
                        self.logger.error("Cannot find path -> %s for favorite item!", str(favorite))
                        success = False
                        continue
                    favorite_id = self._otcs.get_result_value(
                        response=favorite_item,
                        key="id",
                    )
                    favorite_name = self._otcs.get_result_value(
                        response=favorite_item,
                        key="name",
                    )
                    favorite_type = self._otcs.get_result_value(
                        response=favorite_item,
                        key="type",
                    )
                    if favorite_type == self._otcs.ITEM_TYPE_BUSINESS_WORKSPACE:
                        is_workspace = True
                # 2. Check if it a logical workspace identifier in the payload:
                if not favorite_id:
                    # check if favorite is a logical workspace name
                    favorite_item = next(
                        (item for item in self._workspaces if item["id"] == favorite),
                        None,
                    )
                    if favorite_item:
                        if favorite_item.get("enabled", True):
                            self.logger.debug(
                                "Found favorite item (workspace) -> '%s' in payload and it is enabled.",
                                favorite_item["name"],
                            )
                        else:
                            self.logger.info(
                                "Found favorite item (workspace) -> '%s' in payload but it is not enabled. Skipping...",
                                favorite_item["name"],
                            )
                            continue
                        favorite_id = self.determine_workspace_id(workspace=favorite_item)
                        if not favorite_id:
                            self.logger.warning(
                                "Workspace of type -> '%s' and name -> '%s' does not exist. Cannot create favorite. Skipping...",
                                favorite_item["type_name"],
                                favorite_item["name"],
                            )
                            continue
                        is_workspace = True
                        favorite_name = favorite_item["name"]
                # 3. Check if it is a nickname:
                if not favorite_id:
                    favorite_item = self._otcs.get_node_from_nickname(nickname=favorite)
                    favorite_id = self._otcs.get_result_value(
                        response=favorite_item,
                        key="id",
                    )
                    favorite_name = self._otcs.get_result_value(
                        response=favorite_item,
                        key="name",
                    )
                    favorite_type = self._otcs.get_result_value(
                        response=favorite_item,
                        key="type",
                    )
                    if favorite_type == self._otcs.ITEM_TYPE_BUSINESS_WORKSPACE:
                        is_workspace = True

                # If nothing has worked then skip this payload favorite.
                if not favorite_id:
                    self.logger.warning(
                        "Favorite -> '%s' neither found as workspace payload ID, not as a path, nor as nickname. Skipping to next favorite...",
                        favorite,
                    )
                    continue

                response = self._otcs.add_favorite(node_id=favorite_id)
                if response is None:
                    self.logger.warning(
                        "Favorite -> '%s' (%s) couldn't be added for user -> '%s'!",
                        favorite_name,
                        favorite_id,
                        user_name,
                    )
                else:
                    self.logger.info(
                        "Added favorite -> '%s' (%s) for user -> '%s'.",
                        favorite_name,
                        favorite_id,
                        user_name,
                    )
                    self.logger.info(
                        "Simulate user -> '%s' browsing node -> '%s' (%s)...",
                        user_name,
                        favorite_name,
                        favorite_id,
                    )
                    # simulate a browse by the user to populate recently accessed items
                    response = (
                        self._otcs.get_workspace(node_id=favorite_id, fields="properties{id}")
                        if is_workspace
                        else self._otcs.get_node(node_id=favorite_id, fields="properties{id}")
                    )
            # end for favorite in favorites

            # we work through the list of proxies defined for the user
            # (we need to consider that not all users have the proxies element):
            proxies = user.get("proxies", [])

            for proxy in proxies:
                proxy_user = next(
                    (item for item in self._users if item["name"] == proxy),
                    None,
                )
                if not proxy_user or "id" not in proxy_user:
                    self.logger.error(
                        "The proxy -> '%s' for user -> '%s' does not exist! Skipping proxy...",
                        proxy,
                        user_name,
                    )
                    success = False
                    continue
                proxy_user_id = proxy_user["id"]

                # Check if the proxy is already set:
                if not self._otcs.is_proxy(user_name=proxy):
                    self.logger.info(
                        "Set user -> '%s' (%s) as proxy for user -> '%s'.",
                        proxy,
                        proxy_user_id,
                        user_name,
                    )
                    # set the user proxy - currently we don't support time based proxies in payload.
                    # The called method is ready to support this.
                    response = self._otcs.add_user_proxy(proxy_user_id=proxy_user_id)
                else:
                    self.logger.info(
                        "User -> '%s' (%s) is already proxy for user -> '%s'. Skipping...",
                        proxy,
                        proxy_user_id,
                        user_name,
                    )
        # end for user in self._users

    if self._users:
        # Impersonate as the admin user:
        self.logger.info(
            "Impersonate as admin user -> '%s'...",
            self._otcs.credentials()["username"],
        )
        # Stop the impersonation as a user:
        result = self.stop_impersonation()
        if not result:
            self.logger.error(
                "Couldn't impersonate as admin user -> '%s'!",
                self._otcs.credentials()["username"],
            )
            success = False

    # Also for the admin user we want to update the user profile to activate responsive (dynamic) containers:
    response = self._otcs.update_user_profile(
        field="responsiveContainerMode",
        value=True,
        config_section="SmartUI",
    )
    if response is None:
        self.logger.warning(
            "Profile for 'admin' user couldn't be updated with responsive container mode!",
        )
    else:
        self.logger.info(
            "Profile for 'admin' user has been updated to enable responsive container mode.",
        )
    response = self._otcs.update_user_profile(
        field="responsiveContainerMessageMode",
        value=True,
        config_section="SmartUI",
    )
    if response is None:
        self.logger.warning(
            "Profile for 'admin' user couldn't be updated with responsive container message mode!",
        )
    else:
        self.logger.info(
            "Profile for 'admin' user has been updated to enable messages for responsive container mode.",
        )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_user_licenses(resource_name, license_feature, license_name='EXTENDED_ECM', user_specific_payload_field='licenses', section_name='userLicenses')

Assign a specific OTDS license feature to all Content Server users.

This method is used for OTIV and OTCS licenses.

Parameters:

Name Type Description Default
resource_name str

The name of the OTDS resource.

required
license_feature str

The license feature to assign to the user (product specific).

required
license_name str

The name of the license Key (e.g. "EXTENDED_ECM" or "INTELLIGENT_VIEWING").

'EXTENDED_ECM'
user_specific_payload_field str

The name of the user specific field in payload (if empty it will be ignored).

'licenses'
section_name str

The name of the section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'userLicenses'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_licenses")
def process_user_licenses(
    self,
    resource_name: str,
    license_feature: str,
    license_name: str = "EXTENDED_ECM",
    user_specific_payload_field: str = "licenses",
    section_name: str = "userLicenses",
) -> bool:
    """Assign a specific OTDS license feature to all Content Server users.

    This method is used for OTIV and OTCS licenses.

    Args:
        resource_name (str):
            The name of the OTDS resource.
        license_feature (str):
            The license feature to assign to the user (product specific).
        license_name (str):
            The name of the license Key (e.g. "EXTENDED_ECM" or "INTELLIGENT_VIEWING").
        user_specific_payload_field (str, optional):
            The name of the user specific field in payload
            (if empty it will be ignored).
        section_name (str, optional):
            The name of the section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    otds_resource = self._otds.get_resource(resource_name)
    if not otds_resource:
        self.logger.error(
            "OTDS Resource -> '%s' not found. Cannot assign licenses to users.",
            resource_name,
        )
        return False

    user_partition = self._otcs.config()["partition"]
    if not user_partition:
        self.logger.error("OTCS user partition not found in OTDS!")
        return False

    for user in self._users:
        user_name = user.get("name")
        if not user_name:
            self.logger.error(
                "Missing user name - cannot assign license to user. Skipping user...",
            )
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        if user_specific_payload_field and user_specific_payload_field in user:
            self.logger.info(
                "Found specific license feature -> %s for user -> '%s'. Overwriting default license feature -> '%s'.",
                user[user_specific_payload_field],
                user_name,
                license_feature,
            )
            user_license_features = user[user_specific_payload_field]
        else:  # use the default feature from the actual parameter
            user_license_features = [license_feature]

        for user_license_feature in user_license_features:
            if isinstance(user_license_feature, dict):
                user_license_feature_dict = user_license_feature
                user_license_feature = user_license_feature_dict.get("feature", license_feature)
                license_name = user_license_feature_dict.get("name", license_name)
                if "enabled" in user_license_feature_dict and not user_license_feature_dict["enabled"]:
                    self.logger.info(
                        "Payload for License '%s' -> '%s' is disabled. Skipping...",
                        license_name,
                        license_feature,
                    )
                    continue

                if "resource" in user_license_feature_dict:
                    try:
                        resource_id = self._otds.get_resource(name=user_license_feature_dict["resource"])[
                            "resourceID"
                        ]
                    except Exception:
                        self.logger.error(
                            "Error retrieving resourceID for -> %s", user_license_feature_dict["resource"]
                        )
                        continue
                        success = False
                else:
                    resource_id = otds_resource["resourceID"]

            elif isinstance(user_license_feature, str):
                resource_id = otds_resource["resourceID"]

            else:
                self.logger.error("Invalid License feature specified -> %s", user_license_feature)
                continue

            if self._otds.is_user_licensed(
                user_name=user_name,
                resource_id=resource_id,
                license_feature=user_license_feature,
                license_name=license_name,
            ):
                self.logger.info(
                    "User -> '%s' is already licensed for -> '%s' (%s). Skipping...",
                    user_name,
                    license_name,
                    user_license_feature,
                )
                continue
            assigned_license = self._otds.assign_user_to_license(
                partition=user_partition,
                user_id=user_name,  # we want the plain login name here
                resource_id=resource_id,
                license_feature=user_license_feature,
                license_name=license_name,
            )

            if not assigned_license:
                self.logger.error(
                    "Failed to assign license feature -> '%s' to user -> %s!",
                    user_license_feature,
                    user_name,
                )
                success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_user_photos(section_name='userPhotos')

Process user photos in payload and assign them to Content Server users.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'userPhotos'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_photos")
def process_user_photos(self, section_name: str = "userPhotos") -> bool:
    """Process user photos in payload and assign them to Content Server users.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # we assume the nickname of the photo item equals the login name of the user
    # we also assume that the photos have been uploaded / transported into the target system
    for user in self._users:
        user_name = user.get("name")
        if not user_name:
            self.logger.error("User is missing a login. Skipping to next user...")
            success = False
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        # We skip also user of type "ServiceUser":
        if user.get("type", "User") == "ServiceUser":
            self.logger.info("Skipping service user -> '%s'...", user_name)
            continue

        user_id = user.get("id")
        if not user_id:
            self.logger.error(
                "User -> '%s' does not have an ID. The user creation may have failed before. Skipping...",
                user_name,
            )
            success = False
            continue

        response = self._otcs.get_node_from_nickname(nickname=user_name)
        if response is None:
            self.logger.warning(
                "Missing photo for user -> '%s' - nickname not found. Skipping...",
                user_name,
            )
            continue
        photo_id = self._otcs.get_result_value(response=response, key="id")
        response = self._otcs.update_user_photo(user_id=user_id, photo_id=photo_id)
        if not response:
            self.logger.error("Failed to add photo for user -> '%s'!", user_name)
            success = False
        else:
            self.logger.info("Successfully added photo for user -> '%s'.", user_name)

    # Check if Admin has a photo as well (nickname needs to be "admin"):
    response = self._otcs.get_node_from_nickname(nickname="admin")
    if response is None:
        self.logger.warning(
            "Missing photo for admin - nickname not found. Skipping...",
        )
    else:
        photo_id = self._otcs.get_result_value(response=response, key="id")
        response = self._otcs.update_user_photo(user_id=1000, photo_id=photo_id)
        if response is None:
            self.logger.warning("Failed to add photo for user -> 'admin'!")
        else:
            self.logger.info("Successfully added photo for user -> 'admin'.")

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_user_photos_core_share(section_name='userPhotosCoreShare')

Process user photos in payload and assign them to Core Share users.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'userPhotosCoreShare'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_photos_core_share")
def process_user_photos_core_share(
    self,
    section_name: str = "userPhotosCoreShare",
) -> bool:
    """Process user photos in payload and assign them to Core Share users.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not isinstance(self._core_share, CoreShare):
        self.logger.error(
            "Core Share connection not setup properly. Skipping payload section -> '%s'...",
            section_name,
        )
        return False

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # we assume the nickname of the photo item equals the login name of the user
    # we also assume that the photos have been uploaded / transported into the target system
    for user in self._users:
        if "name" not in user:
            self.logger.error(
                "User is missing login name. Skipping to next user...",
            )
            success = False
            continue
        user_login = user["name"]
        user_last_name = user.get("lastname", "")
        user_first_name = user.get("firstname", "")
        user_name = "{} {}".format(user_first_name, user_last_name).strip()

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_login,
            )
            continue

        # Check if the user is enabled for Core Share.
        # Core Share is disabled per default for the users.
        # There needs to be "enable_core_share" in user payload
        # and it needs to be True:
        if not user.get("enable_core_share", False):
            self.logger.info(
                "User -> '%s' is not enabled for Core Share. Skipping...",
                user_login,
            )
            continue

        core_share_user_id = self.determine_user_id_core_share(user=user)

        if core_share_user_id is None:
            self.logger.error(
                "Failed to get ID of Core Share user -> '%s'!",
                user_name,
            )
            success = False
            continue

        response = self._otcs.get_node_from_nickname(nickname=user_login)
        if response is None:
            self.logger.warning(
                "Missing photo for user -> '%s' - nickname not found. Skipping...",
                user_login,
            )
            continue
        photo_id = self._otcs.get_result_value(response=response, key="id")
        photo_name = self._otcs.get_result_value(response=response, key="name")
        photo_path = os.path.join(tempfile.gettempdir(), photo_name)

        # Check if it is not yet downloaded:
        if not os.path.isfile(photo_path):
            # download the profile picture into the tmp directory:
            result = self._otcs.download_document(
                node_id=photo_id,
                file_path=photo_path,
            )
            # download_document() delivers a boolean result:
            if result is False:
                self.logger.warning(
                    "Failed to download photo for user -> '%s' from Content Server to file -> '%s'!",
                    user_name,
                    photo_path,
                )
                success = False
                continue
            self.logger.info(
                "Successfully downloaded photo for user -> '%s' from Content Server to file -> '%s'.",
                user_name,
                photo_path,
            )
        else:
            self.logger.info(
                "Reusing downloaded photo -> '%s' for Core Share user -> '%s' (%s).",
                photo_path,
                user_name,
                core_share_user_id,
            )

        response = self._core_share.update_user_photo(
            user_id=core_share_user_id,
            photo_path=photo_path,
        )
        if response:
            self.logger.info(
                "Successfully updated profile photo of Core Share user -> '%s' (%s).",
                user_name,
                core_share_user_id,
            )
        else:
            self.logger.error(
                "Failed to update profile photo of Core Share user -> '%s' (%s)! Skipping...",
                user_name,
                core_share_user_id,
            )
            success = False
            continue

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_user_photos_m365(section_name='userPhotosM365')

Process user photos in payload and assign them to Microsoft 365 users.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'userPhotosM365'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_photos_m365")
def process_user_photos_m365(self, section_name: str = "userPhotosM365") -> bool:
    """Process user photos in payload and assign them to Microsoft 365 users.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not isinstance(self._m365, M365):
        self.logger.error(
            "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
            section_name,
        )
        return False

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # we assume the nickname of the photo item equals the login name of the user
    # we also assume that the photos have been uploaded / transported into the target system
    for user in self._users:
        user_name = user.get("name")
        if not user_name:
            self.logger.error("User is missing a login. Skipping to next user...")
            success = False
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        if "id" not in user:
            self.logger.error(
                "User -> '%s' does not have an ID. The user creation may have failed before. Skipping...",
                user_name,
            )
            success = False
            continue

        if "enable_o365" not in user or not user["enable_o365"]:
            self.logger.info(
                "Microsoft 365 is not enabled in payload for user -> '%s'. Skipping...",
                user_name,
            )
            continue

        # If the customizer pod is restarted it may be that
        # the M365 user exists even if the M365 user ID is
        # not yet written back into the payload. So we use the
        # determine_user_id_m365() method that handles both cases
        # and updates the payload if the user exists in M365 already.
        user_m365_id = self.determine_user_id_m365(user=user)
        if not user_m365_id:
            self.logger.error(
                "M365 user -> '%s' does not exist. Skipping...",
                user_name,
            )
            success = False
            continue

        if self._m365.get_user_photo(user_id=user_m365_id, show_error=False):
            self.logger.info(
                "User -> '%s' (%s) has already a photo in Microsoft 365. Skipping...",
                user_name,
                user_m365_id,
            )
            continue
        self.logger.info(
            "User -> '%s' (%s) has not yet a photo in Microsoft 365. Uploading...",
            user_name,
            user_m365_id,
        )

        response = self._otcs.get_node_from_nickname(nickname=user_name)
        if response is None:
            self.logger.warning(
                "Missing photo for user -> '%s' - nickname not found. Skipping...",
                user_name,
            )
            continue
        photo_id = self._otcs.get_result_value(response=response, key="id")
        photo_name = self._otcs.get_result_value(response=response, key="name")

        photo_path = os.path.join(tempfile.gettempdir(), photo_name)
        result = self._otcs.download_document(
            node_id=photo_id,
            file_path=photo_path,
        )
        # download_document() delivers a boolean result:
        if result is False:
            self.logger.warning(
                "Failed to download photo for user -> '%s' from Content Server!",
                user_name,
            )
            success = False
            continue
        self.logger.info(
            "Successfully downloaded photo for user -> '%s' from Content Server to file -> '%s'.",
            user_name,
            photo_path,
        )

        # Upload photo to M365:
        response = self._m365.update_user_photo(user_m365_id, photo_path)
        if response is None:
            self.logger.error(
                "Failed to upload photo for user -> '%s' to Microsoft 365!",
                user_name,
            )
            success = False
        else:
            self.logger.info(
                "Successfully uploaded photo for user -> '%s' to Microsoft 365.",
                user_name,
            )
    # end for loop

    # Check if Admin has a photo as well (nickname needs to be "admin")
    # Then we want this to be applied in M365 as well:
    response = self._otcs.get_node_from_nickname(nickname="admin")
    if response is None:
        self.logger.warning(
            "Missing photo for admin - nickname not found. Skipping...",
        )
    else:
        photo_id = self._otcs.get_result_value(response=response, key="id")
        photo_name = self._otcs.get_result_value(response=response, key="name")
        photo_path = os.path.join(tempfile.gettempdir(), photo_name)
        result = self._otcs.download_document(
            node_id=photo_id,
            file_path=photo_path,
        )
        # download_document() delivers a boolean result:
        if result is False:
            self.logger.warning(
                "Failed to download photo for user -> 'admin' from Content Server!",
            )
            success = False
        else:
            self.logger.info(
                "Successfully downloaded photo for user -> 'admin' from Content Server to file -> '%s'.",
                photo_path,
            )
            m365_admin_email = "admin@" + self._m365.config()["domain"]
            response = self._m365.update_user_photo(
                user_id=m365_admin_email,
                photo_path=photo_path,
            )
            if response is None:
                self.logger.warning("Failed to add photo for Microsoft 365 user -> '%s'!", m365_admin_email)
            else:
                self.logger.info(
                    "Successfully added photo for Microsoft 365 user -> '%s'.",
                    m365_admin_email,
                )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_user_photos_salesforce(section_name='userPhotosSalesforce')

Process user photos in payload and assign them to Salesforce users.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'userPhotosSalesforce'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_photos_salesforce")
def process_user_photos_salesforce(
    self,
    section_name: str = "userPhotosSalesforce",
) -> bool:
    """Process user photos in payload and assign them to Salesforce users.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not isinstance(self._salesforce, Salesforce):
        self.logger.error(
            "Salesforce connection not setup properly. Skipping payload section -> '%s'...",
            section_name,
        )
        return False

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # we assume the nickname of the photo item equals the login name of the user
    # we also assume that the photos have been uploaded / transported into the target system
    for user in self._users:
        user_name = user.get("name")
        if not user_name:
            self.logger.error("User is missing a login. Skipping to next user...")
            success = False
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        # Check if the user is explicitly enabled for Salesforce:
        if not user.get("enable_salesforce", False):
            self.logger.info(
                "User -> '%s' is not enabled for Salesforce. Skipping...",
                user_name,
            )
            continue

        extra_attributes = user.get("extra_attributes", None)
        if not extra_attributes or len(extra_attributes) == 0:
            self.logger.info(
                "User -> '%s' does not have the extra attributes for Salesforce. Skipping...",
                user_name,
            )
            continue
        user_login = extra_attributes[0].get("value", "")
        if not user_login:
            self.logger.info(
                "User -> '%s' does not have the extra attributes value for Salesforce. Skipping...",
                user_name,
            )
            continue

        user_id = self._salesforce.get_user_id(username=user_login)
        if user_id is None:
            self.logger.error(
                "Failed to get Salesforce user ID of user -> '%s'!",
                user_login,
            )
            success = False
            continue

        response = self._otcs.get_node_from_nickname(nickname=user_name)
        if response is None:
            self.logger.warning(
                "Missing photo for user -> '%s' - nickname not found. Skipping...",
                user_name,
            )
            continue
        photo_id = self._otcs.get_result_value(response=response, key="id")
        photo_name = self._otcs.get_result_value(response=response, key="name")
        photo_path = os.path.join(tempfile.gettempdir(), photo_name)

        # Check if it is not yet downloaded:
        if not os.path.isfile(photo_path):
            # download the profile picture into the tmp directory:
            result = self._otcs.download_document(
                node_id=photo_id,
                file_path=photo_path,
            )
            # download_document() delivers a boolean result:
            if result is False:
                self.logger.warning(
                    "Failed to download photo for user -> '%s' from Content Server to file -> '%s'!",
                    user_name,
                    photo_path,
                )
                success = False
                continue
            self.logger.info(
                "Successfully downloaded photo for user -> '%s' from Content Server to file -> '%s'.",
                user_name,
                photo_path,
            )
        else:
            self.logger.info(
                "Reusing downloaded photo -> '%s' for Salesforce user -> '%s' (%s).",
                photo_path,
                user_name,
                user_id,
            )

        response = self._salesforce.update_user_photo(
            user_id=user_id,
            photo_path=photo_path,
        )
        if response:
            self.logger.info(
                "Successfully updated profile photo of Salesforce user -> '%s' (%s).",
                user_login,
                user_id,
            )
        else:
            self.logger.error(
                "Failed to update profile photo of Salesforce user -> '%s' (%s)! Skipping...",
                user_login,
                user_id,
            )
            success = False
            continue

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_user_placeholders()

Replace a user placeholder (sourrounded by %%...%%) with the ID of the Content Server user.

For this we prepare a lookup dict. The dict self._placeholder_values already includes lookups for the OTCS and OTAWP OTDS resource IDs (see customizer.py).

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_placeholders")
def process_user_placeholders(self) -> None:
    """Replace a user placeholder (sourrounded by %%...%%) with the ID of the Content Server user.

    For this we prepare a lookup dict. The dict self._placeholder_values already includes
    lookups for the OTCS and OTAWP OTDS resource IDs (see customizer.py).
    """

    for user in self._users:
        user_name = user.get("name")
        if not user_name:
            self.logger.error(
                "User needs a name for placeholder definition in payload! Skipping...",
            )
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the payload element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        # Now we determine the ID. Either it is in the payload section from
        # the current customizer run or we try to look it up in the system.
        # The latter case may happen if the customizer pod got restarted.
        user_id = self.determine_user_id(user=user)
        if not user_id:
            self.logger.warning(
                "User needs an ID for placeholder definition. Skipping...",
            )
            continue

        # Add user with its ID to the dict self._placeholder_values:
        self._placeholder_values["OTCS_USER_ID_%s", user_name.upper()] = str(
            user_id,
        )

    self.logger.debug(
        "Placeholder values after user processing = %s",
        str(self._placeholder_values),
    )

process_user_security(section_name='userSecurity')

Process Security Clearance and Supplemental Markings for Content Server users.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'userSecurity'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_security")
def process_user_security(self, section_name: str = "userSecurity") -> bool:
    """Process Security Clearance and Supplemental Markings for Content Server users.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for user in self._users:
        user_name = user.get("name")

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        user_id = user.get("id")
        if not user_id:
            self.logger.error(
                "User is missing an ID. It was likely not created. Skipping to next user...",
            )
            success = False
            continue

        # Read security clearance from user payload (it is optional!)
        user_security_clearance = user.get("security_clearance")
        if user_id and user_security_clearance:
            self._otcs.assign_user_security_clearance(
                user_id=user_id,
                security_clearance=user_security_clearance,
            )

        # Read supplemental markings from user payload (it is optional!)
        user_supplemental_markings = user.get("supplemental_markings")
        if user_id and user_supplemental_markings:
            self._otcs.assign_user_supplemental_markings(
                user_id=user_id,
                supplemental_markings=user_supplemental_markings,
            )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_user_settings(section_name='userSettings')

Process user settings in payload and apply them in OTDS.

This includes password settings and user display settings.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'userSettings'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_user_settings")
def process_user_settings(self, section_name: str = "userSettings") -> bool:
    """Process user settings in payload and apply them in OTDS.

    This includes password settings and user display settings.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for user in self._users:
        user_name = user.get("name")
        if not user_name:
            self.logger.error(
                "Missing user name - cannot apply user settings. Skipping user...",
            )
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        self._log_header_callback(
            text="Process settings for user -> '{}'".format(user_name),
            char="-",
        )

        user_partition = self._otcs.config().get("partition", None)
        if not user_partition:
            self.logger.error("User partition not found!")
            success = False
            continue

        # Set the OTDS display name. Content Server does not use this but
        # it makes AppWorks display users correctly (and it doesn't hurt)
        # We only set this if firstname _and_ last name are in the payload:
        if "firstname" in user and "lastname" in user:
            user_display_name = user["firstname"] + " " + user["lastname"]
            response = self._otds.update_user(
                partition=user_partition,
                user_id=user_name,
                attribute_name="displayName",
                attribute_value=user_display_name,
            )
            if response:
                self.logger.info(
                    "Successfully updated display name of user -> '%s' to -> '%s'.",
                    user_name,
                    user_display_name,
                )
            else:
                self.logger.error(
                    "Failed to update display name of user -> '%s' to -> '%s'!",
                    user_name,
                    user_display_name,
                )
                success = False

        # Don't enforce the user to reset password at first login (settings in OTDS):
        self.logger.info(
            "Don't enforce password change for user -> '%s'...",
            user_name,
        )
        response = self._otds.update_user(
            partition=user_partition,
            user_id=user_name,
            attribute_name="UserMustChangePasswordAtNextSignIn",
            attribute_value="False",
        )
        if not response:
            success = False

        response = self._otds.update_user(
            partition=user_partition,
            user_id=user_name,
            attribute_name="UserCannotChangePassword",
            attribute_value="True",
        )
        if not response:
            success = False

        # Set user password to never expire
        response = self._otds.update_user(
            partition=user_partition,
            user_id=user_name,
            attribute_name="PasswordNeverExpires",
            attribute_value="True",
        )
        if not response:
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_users(section_name='users')

Process users in payload and create them in Content Server.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'users'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Side Effects

The user items are modified by adding an "id" dict element that includes the technical ID of the user in Content Server

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users")
def process_users(self, section_name: str = "users") -> bool:
    """Process users in payload and create them in Content Server.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    Side Effects:
        The user items are modified by adding an "id" dict element that
        includes the technical ID of the user in Content Server

    """

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # Even if this payload section has been processed successfully before we
    # to process it once more. So we are NOT checking the status file.

    success: bool = True

    # Add all users in payload and establish membership in
    # specified groups:
    for user in self._users:
        user_name = user.get("name")
        if not user_name:
            self.logger.error("User is missing a login. Skipping to next user...")
            success = False
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        # Check if the user does already exist (e.g. if job is restarted)
        # determine_user_id() also writes back the user ID into the payload
        # if it has gathered it from OTCS.
        user_id = self.determine_user_id(user=user)
        if user_id:
            self.logger.info(
                "Found existing user -> '%s' (%s). Skipping to next user...",
                user_name,
                user_id,
            )
            continue
        self.logger.info(
            "Cannot find an existing user -> '%s' - creating a new user...",
            user_name,
        )

        # Sanity checks:
        if "password" not in user or user["password"] is None or user["password"] == "":
            self.logger.info(
                "User -> '%s' no password defined in payload, generating random password...",
                user_name,
            )
            user["password"] = self.generate_password(
                length=10,
                use_special_chars=True,
            )

            description_attribue = {
                "name": "description",
                "value": "initial password: " + user["password"],
            }

            try:
                user["extra_attributes"].append(description_attribue)
            except KeyError:
                user["extra_attributes"] = [description_attribue]

        # Sanity checks:
        if "base_group" not in user or not user["base_group"]:
            self.logger.warning(
                "User -> '%s' is missing a base group - setting to default group.",
                user_name,
            )
            user["base_group"] = "DefaultGroup"

        # Find the base group of the user. Assume 'Default Group' (= 1001) if not found:
        base_group = next(
            (item["id"] for item in self._groups if item["name"] == user["base_group"] and item.get("id")),
            1001,
        )

        # Get type to be able to create ServiceUsers, default to User -> 0
        user_type = 17 if user.get("type", "User") == "ServiceUser" else 0

        # Now we know it is a new user...
        new_user = self._otcs.add_user(
            name=user_name,
            password=user["password"],
            first_name=user.get("firstname", ""),  # be careful - can be empty
            last_name=user.get("lastname", ""),  # be careful - can be empty
            email=user.get("email", ""),  # be careful - can be empty
            title=user.get("title", ""),  # be careful - can be empty
            base_group=base_group,
            phone=user.get("phone", ""),
            privileges=user.get("privileges", ["Login", "Public Access"]),
            user_type=user_type,
        )

        new_user_id = self._otcs.get_result_value(response=new_user, key="id")
        if not new_user_id:
            self.logger.error(
                "Failed to create new user -> '%s'!",
                user_name,
            )
            success = False
            continue

        self.logger.info(
            "Successfully created user -> '%s' with ID -> %s.",
            user_name,
            new_user_id,
        )
        # Write back user ID into payload
        user["id"] = new_user_id

        # Assign usage privileges to the new user:
        usage_privileges = user.get("usage_privileges", [])
        for usage_privilege in usage_privileges:
            response = self._otcs.assign_usage_privilege(
                usage_privilege=usage_privilege,
                member_id=new_user_id,
            )
            if response:
                self.logger.info(
                    "Assigned usage privilege -> '%s' to user -> '%s' (%s).",
                    usage_privilege,
                    user_name,
                    new_user_id,
                )
            else:
                self.logger.info(
                    "Failed to assign usage privilege -> '%s' to user -> '%s' (%s)!",
                    usage_privilege,
                    user_name,
                    new_user_id,
                )

        # Assign usage privileges to the new user:
        object_privileges = user.get("object_privileges", [])
        for object_type in object_privileges:
            response = self._otcs.assign_object_privilege(
                object_type=object_type,
                member_id=new_user_id,
            )
            if response:
                self.logger.info(
                    "Assigned object privilege -> '%s' to user -> '%s' (%s).",
                    object_type,
                    user_name,
                    new_user_id,
                )
            else:
                self.logger.info(
                    "Failed to assign object privilege -> '%s' to user -> '%s' (%s)!",
                    object_type,
                    user_name,
                    new_user_id,
                )

        # Process group memberships of new user:
        user_groups = user.get("groups", [])  # list of groups the user is in
        for user_group in user_groups:
            # Try to find the group dictionary item in the payload
            # for user group name:
            group = next(
                (item for item in self._groups if item["name"] == user_group),
                None,
            )
            if group:
                group_id = group.get("id")  # Careful ID may not exist
                group_name = group["name"]
            else:
                # if group is not in payload try to find group in OTCS
                # in case it is a pre-existing group:
                group = self._otcs.get_group(name=user_group)
                group_id = self._otcs.get_result_value(response=group, key="id")
                if group_id is None:
                    self.logger.error(
                        "Group -> '%s' not found. Skipping...",
                        user_group,
                    )
                    success = False
                    continue
                group_name = self._otcs.get_result_value(
                    response=group,
                    key="name",
                )

            if group_id is None:
                self.logger.error(
                    "Group -> '%s' does not have an ID. Cannot add user -> '%s' to this group. Skipping...",
                    group_name,
                    user["name"],
                )
                success = False
                continue

            self.logger.info(
                "Add user -> '%s' (%s) to group -> '%s' (%s)...",
                user["name"],
                user["id"],
                group_name,
                group_id,
            )
            response = self._otcs.add_group_member(
                member_id=user["id"],
                group_id=group_id,
            )
            if not response:
                success = False
        # end for user_group in user_groups:

        # For some unclear reason the user is not added to its base group in OTDS
        # so we do this explicitly:
        self.logger.info(
            "Add user -> '%s' to its base group -> '%s'...",
            user["name"],
            user["base_group"],
        )
        response = self._otds.add_user_to_group(
            user["name"],
            user["base_group"],
        )
        if not response:
            success = False

        # Extra OTDS attributes for the user can be provided in "extra_attributes"
        # as part of the user payload.
        if "extra_attributes" in user:
            extra_attributes = user["extra_attributes"]
            for extra_attribute in extra_attributes:
                attribute_name = extra_attribute.get("name")
                attribute_value = extra_attribute.get("value")
                if not attribute_name or not attribute_value:
                    self.logger.error(
                        "User attribute is missing a name or value. Skipping...",
                    )
                    success = False
                    continue
                if "password" in attribute_value:
                    self.logger.info(
                        "Set user attribute -> '%s' to -> '*******' (sensitive password)",
                        attribute_name,
                    )
                else:
                    self.logger.info(
                        "Set user attribute -> '%s' to -> '%s'.",
                        attribute_name,
                        attribute_value,
                    )
                user_partition = self._otcs.config()["partition"]
                if not user_partition:
                    self.logger.error("User partition not found!")
                    success = False
                    continue
                self._otds.update_user(
                    user_partition,
                    user["name"],
                    attribute_name,
                    attribute_value,
                )
        # end if "extra_attributes" in user

        # Assign application roles to the new user:
        application_roles = user.get("application_roles", [])

        for role in application_roles:
            user_partition = self._otcs.config()["partition"]
            if not user_partition:
                self.logger.error("User partition not found!")
                success = False
                continue

            # Split role at the @ sign to identify Partitions
            role_parts = role.split("@", 1)
            role_name = role_parts[0]
            role_partition = role_parts[1] if len(role_parts) > 1 else "OAuthClients"

            response = self._otds.assign_user_to_application_role(
                user_id=user["name"],
                user_partition=user_partition,
                role_name=role_name,
                role_partition=role_partition,
            )

            if response:
                self.logger.info(
                    "Successfully assigned application role -> '%s' (%s) to user -> '%s' (%s).",
                    role_name,
                    role_partition,
                    user_name,
                    user_partition,
                )
            else:
                self.logger.info(
                    "Failed to assign application role -> '%s' (%s) to user -> '%s' (%s) in OTDS!",
                    role_name,
                    role_partition,
                    user_name,
                    user_partition,
                )

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_users_core_share(section_name='usersCoreShare')

Process users in payload and sync them with Core Share (passwords and email).

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'usersCoreShare'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Side Effects

The user items are modified by adding "core_share_user_id" dict element that includes the ID of the user in Core Share. This will be written into the status file.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users_core_share")
def process_users_core_share(
    self,
    section_name: str = "usersCoreShare",
) -> bool:
    """Process users in payload and sync them with Core Share (passwords and email).

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    Side Effects:
        The user items are modified by adding "core_share_user_id"
        dict element that includes the ID of the user in Core Share. This will be written
        into the status file.

    """

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    if not self._core_share:
        self.logger.error(
            "Core Share connection is not initialized. Bailing out...",
        )
        return False

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # traverse all users in payload:
    for user in self._users:
        user_last_name = user.get("lastname", "")  # Default is important here
        user_first_name = user.get("firstname", "")
        user_name = " ".join(filter(None, [user_first_name, user_last_name]))
        user_enabled = user.get("enabled", True) and user.get("enable_core_share", False)
        if not user_name and user_enabled:  # Avoid a warning if not enbaled
            self.logger.error(
                "User is missing last name and first name. Skipping to next user...",
            )
            success = False
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        # Check if the user is enabled for Core Share.
        # Core Share is disabled per default for the users.
        # There needs to be "enable_core_share" in user payload
        # and it needs to be True:
        if not user.get("enable_core_share", False):
            self.logger.info(
                "User -> '%s' is not enabled for Core Share. Skipping...",
                user_name,
            )
            continue

        user_email = user.get("email", "")
        user_password = user.get("password", "")

        # Initialize variables:
        need_email_verification = False
        subject = None
        url_search_pattern = None

        #
        # 1. Check if user does already exist in Core Share:
        #

        core_share_user_id = self.determine_user_id_core_share(user=user)

        #
        # 2: Create or Update user in Core Share:
        #

        # Check if we need to create a new Core Share user:
        if core_share_user_id is None:
            self.logger.info(
                "Core Share user -> '%s' does not exist. Creating a new Core Share user...",
                user_name,
            )
            response = self._core_share.add_user(
                first_name=user_first_name,
                last_name=user_last_name,
                email=user_email,
                title=user.get("title", None),
                company=user.get("company", "Innovate"),
                password=user.get("password", None),
            )
            core_share_user_id = self._core_share.get_result_value(
                response=response,
                key="id",
            )
            if not core_share_user_id:
                self.logger.error(
                    "Failed to create Core Share user -> '%s'! Skipping...",
                    user_name,
                )
                success = False
                continue
            self.logger.info(
                "Successfully created Core Share user -> '%s' (%s).",
                user_name,
                core_share_user_id,
            )

            # Check if the user is still in pending state:
            is_confirmed = self._core_share.get_result_value(
                response=response,
                key="isConfirmed",
            )
            # we need to differentiate False an None here - don't simplify to "if not is_confirmed"!
            if is_confirmed is False:
                self.logger.info(
                    "New Core Share user -> '%s' is not yet confirmed and in a 'pending' state!",
                    user_name,
                )
            elif is_confirmed is True:
                self.logger.info(
                    "New Core Share user -> '%s' is already confirmed!",
                    user_name,
                )

            # We write the user password in addition to the "Other" Address field
            # to determine in a subsequent deployment the "old" password:
            update_data = {
                "addresses": [
                    {"type": "other", "value": user.get("password", None)},
                ],
            }
            response = self._core_share.update_user(
                user_id=core_share_user_id,
                update_data=update_data,
            )

            # We need email verification for new users:
            need_email_verification = True
            url_search_pattern = "verify-email"
            subject = "Welcome to OpenText Core Share"

            # For new users the old password is equal to the new password:
            old_password = user.get("password", None)
        # The user does already exist in Core Share:
        else:
            update_data = {
                "firstName": user.get("firstname", ""),
                "lastName": user.get("lastname", ""),
                "title": user.get("title", ""),
                "company": user.get("company", ""),
            }
            self.logger.info(
                "Core Share user -> '%s' does already exist. Updating Core Share user with -> %s...",
                user_name,
                str(update_data),
            )

            # Fetch the existing user:
            core_share_user = self._core_share.get_user_by_id(
                user_id=core_share_user_id,
            )

            # Check if the user is still in pending state:
            is_confirmed = self._core_share.get_result_value(
                response=core_share_user,
                key="isConfirmed",
            )
            # we need to differentiate False an None here - don't simplify to "if not is_confirmed"!
            if is_confirmed is False:
                self.logger.warning(
                    "Core Share user -> '%s' has not yet confirmed the email invitation and is in 'pending' state! Resend invite...",
                    user_name,
                )
                # We try the email verification once more...
                self._core_share.resend_user_invite(core_share_user_id)
                need_email_verification = True
                url_search_pattern = "confirm-account"
                subject = "Invitation to OpenTextâ„¢ Core Share"

            # Check if we have the old password of the user in the "Other" address field:
            core_share_user_addresses = self._core_share.get_result_value(
                core_share_user,
                "addresses",
            )
            if core_share_user_addresses and len(core_share_user_addresses) > 0:
                old_password = core_share_user_addresses[0]["value"]
                self.logger.info(
                    "Found old password for Core Share user -> '%s' (%s).",
                    user_name,
                    core_share_user_id,
                )
            else:
                self.logger.info(
                    "No old password found for Core Share user -> '%s'. Cannot set a new password.",
                    user_name,
                )
                old_password = ""

            # We store the current password into the address field (this adds to the update dictionary
            # defined above and used below): THIS IS CURRENTLY NOT WORKING!
            update_data["addresses"] = [{"type": "other", "value": user_password}]

            # Check if the mail address has really changed. Otherwise we
            # don't need to set it again and can avoid email verification:
            core_share_user_email = self._core_share.get_result_value(
                core_share_user,
                "email",
            )
            if user_email != core_share_user_email:
                self.logger.info(
                    "Email for Core Share user -> '%s' has changed from -> '%s' to -> '%s'. We need to verify this via email.",
                    user_name,
                    core_share_user_email,
                    user_email,
                )
                # Additional email payload for user update:
                update_data["email"] = user_email
                # If email is changed this needs to be confirmed by passing
                # the current (old) password:
                update_data["password"] = old_password if old_password else user_password
                # As email has changed - we need the email verification below...
                need_email_verification = True
                url_search_pattern = "verify-email"
                subject = "OpenText Core Share: Email Updated"

            # Update the existing Core Share user with new / changed data:
            response = self._core_share.update_user(
                user_id=core_share_user_id,
                update_data=update_data,
            )
            if not response:
                self.logger.error(
                    "Failed to update Core Share user -> '%s'! Skipping...",
                    user_name,
                )
                success = False
                continue
            self.logger.info(
                "Successfully updated Core Share user -> '%s'.",
                user_name,
            )

            # Now update the password:
            if user_password and old_password and user_password != old_password:
                response = self._core_share.update_user_password(
                    user_id=core_share_user_id,
                    password=old_password,
                    new_password=user_password,
                )
                if response:
                    self.logger.info(
                        "Successfully updated password of Core Share user -> '%s' (%s).",
                        user_name,
                        core_share_user_id,
                    )
                else:
                    self.logger.error(
                        "Failed to update Core Share password for user -> '%s' (%s)! Skipping...",
                        user_name,
                        core_share_user_id,
                    )
                    success = False
                    continue
            elif not old_password:
                self.logger.warning(
                    "Cannot change Core Share user password for -> '%s' (%s). Need both, old and new passwords.",
                    user_name,
                    core_share_user_id,
                )
            else:
                self.logger.info(
                    "Core Share user password for -> '%s' (%s) is unchanged.",
                    user_name,
                    core_share_user_id,
                )

            # For existing users we want to cleanup possible left-overs form old deployments
            self.logger.info(
                "Cleanup existing file shares of Core Share user -> '%s' (%s)...",
                user_name,
                core_share_user_id,
            )
            response = self._core_share.cleanup_user_files(
                user_id=core_share_user_id,
                user_login=core_share_user_email,
                user_password=user_password,
            )
            if not response:
                self.logger.error(
                    "Failed to cleanup user files for user -> '%s' (%s)!", user_name, core_share_user_id
                )

        # Save result for status file content
        user["core_share_user_id"] = core_share_user_id

        #
        # 3: Handle Email verification:
        #

        # We now need to wait for the verification mail from Core Share,
        # get it from the M365 Outlook inbox of the user (or the admin
        # if the user does not have its own inbox) and click the
        # verification link...

        if need_email_verification and user.get("enable_o365", False):
            self.logger.info(
                "Processing Email verification for user -> '%s' (%s). Wait 20 seconds to make sure verification mail in user's inbox...",
                user_name,
                user_email,
            )
            time.sleep(20)

            # Process verification mail sent by Core Share.
            # TODO: This has some hard-coded value. We may want to optimize it further in the future:
            result = self._m365.email_verification(
                user_email=user_email,
                sender="noreply@opentext.cloud",
                subject=subject,
                url_search_pattern=url_search_pattern,
                line_end_marker="=",
                multi_line=True,
                multi_line_end_marker="%3D",
                replacements=None,
                max_retries=6,
                use_browser_automation=True,
                password=user_password,
                password_field_id="passwordInput",
                password_confirmation_field_id="confirmResetPassword",
                password_submit_xpath="//button[@type='submit']",
                terms_of_service_xpath="//div[@id = 'termsOfService']//button[@type='submit']",
            )
            if not result:
                # Email verification was not successful
                self.logger.warning(
                    "Core Share email verification failed. No verification mail received in user's inbox.",
                )
                # don't treat as error nor do "continue" here - we still want to process the user groups...
            else:
                self.logger.info(
                    "Successfully verified new email address -> '%s'.",
                    user_email,
                )
        # end if need_email_verification

        #
        # 4: Add users into groups in Core Share:
        #

        self.logger.info(
            "Processing group memberships of Core Share user -> '%s' (%s)...",
            user_name,
            user_email,
        )
        user_groups = user.get("groups", [])
        base_group = user.get("base_group", None)
        if base_group and base_group not in user_groups:
            user_groups.append(base_group)  # list of groups the user is in

        for user_group in user_groups:
            # "Business Administrators" is a OTCS generated group that we won't find
            # in payload - skip this group.
            if user_group == "Business Administrators":
                # Users that are Business Administrators in Content Server
                # become Content Manager (role = 5) in Core Share:
                self.logger.info(
                    "User -> '%s' is a business administrator in Content Server and becomes a 'Content Manager' (access role 5) in Core Share",
                    user_name,
                )
                self._core_share.add_user_access_role(
                    user_id=core_share_user_id,
                    role_id=5,
                )
                continue
            # Try to find the group dictionary item in the payload
            # for user group name:
            group = next(
                (item for item in self._groups if item["name"] == user_group),
                None,
            )
            if not group:
                self.logger.error(
                    "Cannot find group -> '%s'. Cannot establish membership in Core Share. Skipping to next group...",
                    user_group,
                )
                success = False
                continue

            group_name = group["name"]
            group_id = self.determine_group_id_core_share(
                group=group,
            )  # Careful ID may not exist
            if group_id is None:
                self.logger.info(
                    "Group -> '%s' does not have a Core Share ID. Cannot add user -> '%s' to this Core Share group (group may not be enabled for Core Share). Skipping...",
                    group_name,
                    user_name,
                )
                # We don't treat this as an error - there may be payload groups which are not enabled for Core Share!
                continue

            existing_members = self._core_share.get_group_members(group_id)

            # Only add user as new member if not yet a member or a 'pending' member:
            is_member = self._core_share.exist_result_item(
                response=existing_members,
                key="id",
                value=core_share_user_id,
                results_marker="groupMembers",
            )
            is_pending_member = self._core_share.exist_result_item(
                response=existing_members,
                key="email",
                value=user_email,
                results_marker="pending",
            )

            if not is_member and not is_pending_member:
                self.logger.info(
                    "Add Core Share user -> '%s' (%s) to Core Share group -> '%s' (%s)...",
                    user_name,
                    core_share_user_id,
                    group_name,
                    group_id,
                )
                # We make users that have this group as base_group
                # to Admins of the Core Share group:
                is_group_admin = user_group == base_group
                response = self._core_share.add_group_member(
                    group_id=group_id,
                    user_id=core_share_user_id,
                    is_group_admin=is_group_admin,
                )
                # the add_group_member() has a special return value
                # which is a list (not a dict). It has mostly 1 element
                # which is a dict with a "success" item. This (and not response.ok)
                # determines if the call was successful!
                success: bool = self._core_share.get_result_value(
                    response,
                    "success",
                )
                if not success:
                    errors = self._core_share.get_result_value(
                        response=response,
                        key="errors",
                    )
                    self.logger.error(
                        "Failed to add Core Share user -> '%s' (%s) as member to Core Share group -> '%s' (%s)! Error -> %s",
                        user_name,
                        core_share_user_id,
                        group_name,
                        group_id,
                        str(errors),
                    )
                    success = False
                    continue
            else:
                self.logger.info(
                    "Core Share user -> '%s' (%s) is already a %s of Core Share group -> '%s' (%s). Skipping...",
                    user_name,
                    core_share_user_id,
                    "member" if is_member else "pending member",
                    group_name,
                    group_id,
                )
        # end for loop user groups
    # end for loop users

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_users_m365(section_name='usersM365')

Process users in payload and create them in Microsoft 365 via MS Graph API.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'usersM365'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users_m365")
def process_users_m365(self, section_name: str = "usersM365") -> bool:
    """Process users in payload and create them in Microsoft 365 via MS Graph API.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not isinstance(self._m365, M365):
        self.logger.error(
            "Microsoft 365 connection not setup properly. Skipping payload section -> '%s'...",
            section_name,
        )
        return False

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # Add all users in payload and establish membership in
    # specified groups:
    for user in self._users:
        user_name = user.get("name")
        if not user_name:
            self.logger.error("User is missing a login. Skipping to next user...")
            success = False
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        # Check if the user is enabled for Microsoft 365.
        # M365 is disabled per default for the users.
        # There needs to be "enable_o365" in user payload
        # and it needs to be True:
        if not user.get("enable_o365", False):
            self.logger.info(
                "Microsoft 365 is not enabled in payload for user -> '%s'. Skipping...",
                user_name,
            )
            continue

        # Sanity checks:
        if "password" not in user:
            self.logger.error(
                "User -> '%s' is missing a password. Skipping to next user...",
                user_name,
            )
            success = False
            continue
        user_password = user["password"]
        # be careful with the following fields - they could be empty
        user_department = user.get("base_group", "")
        user_first_name = user.get("firstname", "")
        user_last_name = user.get("lastname", "")
        user_location = user.get("location", "US")
        user_email = user.get("email", user_name)

        # Check if the user does already exist in M365 (e.g. if job is restarted)
        m365_user_id = self.determine_user_id_m365(user=user)
        if not m365_user_id:
            self.logger.info(
                "Did not find existing Microsoft 365 user - creating user -> '%s'...",
                user_email,
            )

            # Now we know it is a new user...
            # We are not 1:1 using the email address from the
            # payload as this could by an alias address using the "+" syntax:
            m365_user_name = user_name + "@" + self._m365.config()["domain"]

            new_user = self._m365.add_user(
                email=m365_user_name,
                password=user_password,
                first_name=user_first_name,
                last_name=user_last_name,
                location=user_location,
                department=user_department,
            )
            if new_user is not None:
                # Store the Microsoft 365 user ID in payload:
                user["m365_id"] = new_user["id"]
                m365_user_id = new_user["id"]
                self.logger.info(
                    "New Microsoft 365 user -> '%s' with ID -> %s has been created.",
                    user_name,
                    m365_user_id,
                )
            else:
                self.logger.error(
                    "Failed to create new Microsoft 365 user -> '%s'! Skipping...",
                    user_name,
                )
                success = False
                continue
        else:
            # if the user exists we just set the password according
            # the the payload definition to allow to bulk
            # update existing M365 users with new passwords:
            self.logger.info(
                "Found existing Microsoft 365 user -> '%s' - updating password...",
                user_name,
            )
            new_password_settings = {
                "passwordProfile": {
                    "forceChangePasswordNextSignIn": False,
                    "password": user_password,
                },
            }
            response = self._m365.update_user(
                user_id=m365_user_id,
                updated_settings=new_password_settings,
            )
            if not response:
                self.logger.error(
                    "Failed to update password of M365 user -> '%s' (%s)!",
                    user_name,
                    m365_user_id,
                )

        # Now we assign a license to the new M365 user.
        # First we see if there's a M365 SKU list in user
        # payload - if not we wrap the default SKU configured
        # for the m365 object into a single item list:
        existing_user_licenses = self._m365.get_user_licenses(user_id=m365_user_id)
        sku_list = user.get("m365_skus", [self._m365.config()["skuId"]])
        for sku_id in sku_list:
            # Check if the M365 user already has this license:
            if not self._m365.exist_result_item(
                existing_user_licenses,
                "skuId",
                sku_id,
            ):
                response = self._m365.assign_license_to_user(m365_user_id, sku_id)
                if not response:
                    self.logger.error(
                        "Failed to assign license -> '%s' to Microsoft 365 user -> '%s'!",
                        sku_id,
                        user_name,
                    )
                    success = False
                else:
                    if (
                        "m365_skus" not in user
                    ):  # this is only True if the default license from the m365 object is taken
                        user["m365_skus"] = [sku_id]
                    self.logger.info(
                        "License -> '%s' has been assigned to Microsoft 365 user -> '%s'.",
                        sku_id,
                        user_name,
                    )
            else:
                self.logger.info(
                    "Microsoft 365 user -> '%s' already has the license -> '%s'.",
                    user_name,
                    sku_id,
                )

        # Now we assign the Content Server Teams App to the new M365 user.
        # First we check if the app is already assigned to the user.
        # If not we install / assign the app. If the user already has
        # the Content Server app we try to upgrade it:
        app_name = self._m365.config()["teamsAppName"]
        app_external_id = self._m365.config()["teamsAppExternalId"]
        app_internal_id = self._m365.config().get("teamsAppInternalId", None)
        response = self._m365.get_teams_apps_of_user(
            user_id=m365_user_id,
            filter_expression="contains(teamsAppDefinition/displayName, '{}')".format(
                app_name,
            ),
        )
        if self._m365.exist_result_item(
            response=response,
            key="displayName",
            value=app_name,
            sub_dict_name="teamsAppDefinition",
        ):
            self.logger.info(
                "M365 Teams app -> '%s' is already assigned to M365 user -> '%s' (%s). Trying to upgrade app...",
                app_name,
                user_name,
                m365_user_id,
            )
            response = self._m365.upgrade_teams_app_of_user(
                user_id=m365_user_id,
                app_name=app_name,
            )
        else:
            self.logger.info(
                "Assign M365 Teams app -> '%s' (%s) to M365 user -> '%s' (%s)...",
                app_name,
                app_external_id,
                user_name,
                m365_user_id,
            )
            # This can produce errors because the app may be assigned organization-wide.
            # So we don't treat it as an error and just show a warning.
            # We also try to use the internal app id instead of the name:
            if app_internal_id:
                response = self._m365.assign_teams_app_to_user(
                    user_id=m365_user_id,
                    app_name=app_name,
                    app_internal_id=app_internal_id,
                    show_error=False,
                )
            else:
                response = self._m365.assign_teams_app_to_user(
                    user_id=m365_user_id,
                    app_name=app_name,
                    show_error=False,
                )

        # Process Microsoft 365 group memberships of new user:
        # don't forget the base group (department) if it is not yet in groups!
        group_names = user.get("groups", [])
        if user_department and user_department not in group_names:
            group_names.append(user_department)
        self.logger.info(
            "User -> '%s' has these groups in payload -> %s (including base group -> %s). Checking if they are Microsoft 365 groups...",
            user_name,
            group_names,
            user_department,
        )

        # Go through all group names:
        for group_name in group_names:
            # Find the group payload item to the parent group name:
            group = next(
                (item for item in self._groups if item["name"] == group_name),
                None,
            )
            if not group:
                # if group is not in payload then this membership
                # is not relevant for Microsoft 365. This could be system generated
                # groups like "PageEdit" or "Business Administrators".
                # In this case we do "continue" as we can't process parent groups
                # either:
                self.logger.debug(
                    "No payload found for group -> '%s'. Skipping...",
                    group_name,
                )
                continue
            if "enable_o365" not in group or not group["enable_o365"]:
                # If Microsoft 365 is not enabled for this group in
                # the payload we don't create a M365 but we do NOT continue
                # as there may still be parent groups that are M365 enabled
                # we want to put the user in (see below):
                self.logger.info(
                    "Payload group -> '%s' is not enabled for M365.",
                    group_name,
                )
            else:
                response = self._m365.get_group(group_name=group_name)
                if response is None or "value" not in response or not response["value"]:
                    self.logger.error(
                        "Microsoft 365 group -> '%s' not found. Skipping...",
                        group_name,
                    )
                    success = False
                else:
                    group_id = response["value"][0]["id"]

                    # Check if user is already a member. We don't want
                    # to throw an error if the user is not found as a member
                    # so we pass show_error=False:
                    if self._m365.is_member(
                        group_id,
                        m365_user_id,
                        show_error=False,
                    ):
                        self.logger.info(
                            "Microsoft 365 user -> '%s' (%s) is already in Microsoft 365 group -> '%s' (%s).",
                            user["name"],
                            m365_user_id,
                            group_name,
                            group_id,
                        )
                    else:
                        self.logger.info(
                            "Add Microsoft 365 user -> '%s' (%s) to Microsoft 365 group -> '%s' (%s)...",
                            user["name"],
                            m365_user_id,
                            group_name,
                            group_id,
                        )
                        response = self._m365.add_group_member(
                            group_id=group_id,
                            member_id=m365_user_id,
                        )
                        if not response:
                            self.logger.error(
                                "Failed to add Microsoft 365 user -> '%s' (%s) to Microsoft 365 group -> '%s' (%s)!",
                                user["name"],
                                m365_user_id,
                                group_name,
                                group_id,
                            )
                            success = False

                        # As each group should have at least one owner in M365
                        # we set all users also as owners for now. Later we
                        # may want to configure this via payload:
                        self.logger.info(
                            "Make Microsoft 365 user -> '%s' (%s) owner of Microsoft 365 group -> '%s' (%s)...",
                            user["name"],
                            m365_user_id,
                            group_name,
                            group_id,
                        )
                        response = self._m365.add_group_owner(
                            group_id,
                            m365_user_id,
                        )
                        if not response:
                            self.logger.error(
                                "Failed to make Microsoft 365 user -> '%s' (%s) owner of Microsoft 365 group -> '%s' (%s)!",
                                user["name"],
                                m365_user_id,
                                group_name,
                                group_id,
                            )
                            success = False

            # As M365 groups are flat (not nested), we also add the
            # user as member to the parent groups of the current group
            # if the parent group is enabled for M365:
            parent_group_names = group.get("parent_groups")
            self.logger.info(
                "Group -> '%s' has the following parent groups -> %s",
                group_name,
                parent_group_names,
            )
            for parent_group_name in parent_group_names:
                # Find the group dictionary item to the parent group name:
                parent_group = next(
                    (item for item in self._groups if item["name"] == parent_group_name),
                    None,
                )
                if parent_group is None or "enable_o365" not in parent_group or not parent_group["enable_o365"]:
                    # if parent group is not in payload then this membership
                    # is not relevant for Microsoft 365.
                    # If Microsoft 365 is not enabled for this parent group in
                    # the payload we can also skip:
                    self.logger.info(
                        "Parent group -> '%s' is not enabled for M365. Skipping...",
                        group_name,
                    )
                    continue

                response = self._m365.get_group(group_name=parent_group_name)
                if response is None or "value" not in response or not response["value"]:
                    self.logger.error(
                        "Microsoft 365 group -> '%s' not found. Skipping...",
                        group_name,
                    )
                    success = False
                    continue
                parent_group_id = response["value"][0]["id"]

                # Check if user is already a member. We don't want
                # to throw an error if the user is not found as a member:
                if self._m365.is_member(
                    group_id=parent_group_id,
                    member_id=m365_user_id,
                    show_error=False,
                ):
                    self.logger.info(
                        "Microsoft 365 user -> '%s' (%s) is already in Microsoft 365 group -> '%s' (%s).",
                        user["name"],
                        m365_user_id,
                        parent_group_name,
                        parent_group_id,
                    )
                else:
                    self.logger.info(
                        "Add Microsoft 365 user -> '%s' (%s) to Microsoft 365 group -> '%s' (%s)...",
                        user["name"],
                        m365_user_id,
                        parent_group_name,
                        parent_group_id,
                    )
                    self._m365.add_group_member(
                        group_id=parent_group_id,
                        member_id=m365_user_id,
                    )
            # end for parent_group_name

            # Make this user follow the SharePoint site of his/her department.
            # We only do this for users that have a valid M365 license (SKU):
            if group_name == user_department and user["m365_skus"]:
                group = self._m365.get_group(group_name=group_name)
                group_id = self._m365.get_result_value(response=group, key="id")
                if group_id:
                    group_site = self._m365.get_sharepoint_site_for_group(group_id=group_id)
                    group_site_id = self._m365.get_result_value(response=group_site, key="id")
                    if group_site_id:
                        group_site_name = self._m365.get_result_value(response=group_site, key="name")
                        # Make sure the user's mysite (drive) has been provisioned.
                        # This is a prerequisite for a user being able to follow a
                        # SharePoint site. For this to work we need "Files.ReadWrite"
                        # as a delegated permission in the Azure AppRegistration!
                        self.logger.info(
                            "Make sure user -> '%s' has a drive (mySite) provisioned...",
                            user["email"],
                        )
                        # We need to have 'delegated' permissions for this so we authenticate
                        # as the user... The scope is important here - the user's drive can only
                        # be provisioned if "Files.ReadWrite" scope is provided:
                        response = self._m365.authenticate_user(
                            username=user["email"], password=user["password"], scope="Files.ReadWrite"
                        )
                        if not response:
                            self.logger.error(
                                "Couldn't authenticate as M365 user -> '%s' to provision user's drive!",
                                username=user["email"],
                            )
                            success = False
                            continue
                        # Retrieve the drive endpoint to trigger the drive provisioning. It is important
                        # to use the 'me=True' to make sure the request is done with the user credentials,
                        # not the app credentials (using purely client_id / client_secret):
                        response = self._m365.get_user_drive(user_id=user["email"], me=True)
                        if not response:
                            self.logger.error("Couldn't get M365 drive of user -> '%s'!", user["email"])
                            success = False
                            continue
                        self.logger.info(
                            "Make user -> '%s' follow departmental SharePoint site -> '%s'...",
                            user["email"],
                            group_site_name,
                        )
                        response = self._m365.follow_sharepoint_site(site_id=group_site_id, user_id=m365_user_id)
                        if not response:
                            self.logger.warning(
                                "User -> '%s' cannot follow SharePoint site -> '%s'!",
                                user["email"],
                                group_site_name,
                            )
                            success = False
                    # end if group_site_id:
                # end if group_id:
            # end if group_name == user_department and user["m365_skus"]:
        # end for group name in group_names:
    # end for user

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_users_salesforce(section_name='usersSalesforce')

Process users in payload and sync them with Salesforce (passwords and email).

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'usersSalesforce'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Side Effects

The user items are modified by adding "salesforce_user_id", "salesforce_user_login" dict element that includes the ID of the user in Salesforce. This will be written into the status file.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users_salesforce")
def process_users_salesforce(
    self,
    section_name: str = "usersSalesforce",
) -> bool:
    """Process users in payload and sync them with Salesforce (passwords and email).

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    Side Effects:
        The user items are modified by adding "salesforce_user_id", "salesforce_user_login"
        dict element that includes the ID of the user in Salesforce. This will be written
        into the status file.

    """

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    if not self._salesforce:
        self.logger.error(
            "Salesforce connection is not initialized. Bailing out...",
        )
        return False

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # traverse all users in payload:
    for user in self._users:
        user_name = user.get("name")
        if not user_name:
            self.logger.error("User is missing a login. Skipping to next user...")
            success = False
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        # Check if the user is explicitly enabled for Salesforce:
        if not user.get("enable_salesforce", False):
            self.logger.info(
                "User -> '%s' is not enabled for Salesforce. Skipping...",
                user_name,
            )
            continue

        extra_attributes = user.get("extra_attributes", None)
        if not extra_attributes or len(extra_attributes) == 0:
            self.logger.info(
                "User -> '%s' does not have the extra attributes for Salesforce. Cannot determine the Salesforce login for user. Skipping...",
                user_name,
            )
            continue
        user_login = extra_attributes[0].get("value", "")
        if not user_login:
            self.logger.info(
                "User -> '%s' does not have the extra attributes value for Salesforce. Skipping...",
                user_name,
            )
            continue

        user_email = user.get("email", "")
        need_email_verification = False

        #
        # 1. Check if user does already exist in Salesforce:
        #

        salesforce_user_id = self._salesforce.get_user_id(username=user_login)

        #
        # 2: Create or Update user in Salesforce:
        #

        if salesforce_user_id is None:
            self.logger.info(
                "Salesforce user -> '%s' does not exist. Creating a new Salesforce user...",
                user_name,
            )
            response = self._salesforce.add_user(
                username=user_login,
                email=user.get("email", ""),
                firstname=user.get("firstname", ""),
                lastname=user.get("lastname", ""),
                department=user.get("base_group", ""),
                title=user.get("title", ""),
                company_name=user.get("company", "Innovate"),
            )
            salesforce_user_id = self._salesforce.get_result_value(
                response=response,
                key="id",
            )
            if not salesforce_user_id:
                self.logger.error(
                    "Failed to create Salesforce user -> '%s'! Skipping...",
                    user_name,
                )
                success = False
                continue
            self.logger.info(
                "Successfully created Salesforce user -> '%s' with ID -> %s.",
                user_name,
                salesforce_user_id,
            )
            # We need email verification for new users (unclear if this is really the case...)
            need_email_verification = True
        # The user does already exist in Salesforce and we just update it:
        else:
            update_data = {
                "FirstName": user.get("firstname", ""),
                "LastName": user.get("lastname", ""),
                "Department": user.get("base_group", ""),
                "Title": user.get("title", ""),
                "CompanyName": user.get("company", ""),
            }
            self.logger.info(
                "Salesforce user -> '%s' does already exist. Updating Salesforce user with -> %s...",
                user_name,
                str(update_data),
            )

            # Check if the mail address has really changed. Otherwise we
            # don't need to set it again and can avoid email verification:
            salesforce_user = self._salesforce.get_user(user_id=salesforce_user_id)
            salesforce_user_email = self._salesforce.get_result_value(
                response=salesforce_user,
                key="Email",
            )
            if user_email != salesforce_user_email:
                self.logger.info(
                    "Email for Salesforce user -> '%s' has changed from -> '%s' to -> '%s'.",
                    user_name,
                    salesforce_user_email,
                    user_email,
                )
                # Additional email payload for user update:
                update_data["Email"] = user_email
                # OK, email has changed - we need the email verification below...
                need_email_verification = True

            # Update the existing Salesforce user with new / changed data:
            response = self._salesforce.update_user(
                user_id=salesforce_user_id,
                update_data=update_data,
            )
            if not response:
                self.logger.error(
                    "Failed to update Salesforce user -> '%s'! Skipping...",
                    user_login,
                )
                success = False
                continue
            self.logger.info(
                "Successfully updated Salesforce user -> '%s'.",
                user_login,
            )

        # Save result for status file content
        user["salesforce_user_id"] = salesforce_user_id
        user["salesforce_user_login"] = user_login

        #
        # 3: Update user password in Salesforce (we need to do this also for new users!):
        #

        # Lookup password in payload:
        user_password = user.get("password", "")

        if user_password:
            response = self._salesforce.update_user_password(
                user_id=salesforce_user_id,
                password=user_password,
            )
            if response:
                self.logger.info(
                    "Successfully updated password of Salesforce user -> '%s' (%s).",
                    user_login,
                    salesforce_user_id,
                )
            else:
                self.logger.error(
                    "Failed to update Salesforce password for user -> '%s' (%s)! Skipping...",
                    user_login,
                    salesforce_user_id,
                )
                success = False
                continue

        #
        # 4: Handle Email verification:
        #

        # We now need to wait for the verification mail from Salesforce,
        # get it from the M365 Outlook inbox of the user (or the admin
        # if the user does not have its own inbox) and click the
        # verification link...

        if need_email_verification and user.get("enable_o365", False):
            self.logger.info(
                "Processing Email verification for user -> '%s' (%s). Wait a few seconds to make sure verification mail in user's inbox...",
                user_name,
                user_email,
            )
            time.sleep(20)

            # Process verification mail sent by Salesforce.
            # This has some hard-coded value. We may want to optimize it further in the future:
            result = self._m365.email_verification(
                user_email=user_email,
                sender="QA_SUPPORT@salesforce.com",
                subject="Finish changing your Salesforce",
                url_search_pattern="setup/emailverif",
            )
            if not result:
                # Email verification was not successful
                self.logger.warning(
                    "Salesforce email verification failed. No verification mail received in user's inbox using email address -> '%s'!",
                    user_email,
                )
                # don't treat as error nor do "continue" here - we still want to process the user groups...
            else:
                self.logger.info(
                    "Successfully verified new email address -> '%s'.",
                    user_email,
                )
        # end if need_email_verification

        #
        # 5: Add users into groups in Salesforce:
        #

        self.logger.info(
            "Processing group memberships of Salesforce user -> '%s' (%s)...",
            user_name,
            user_email,
        )
        user_groups = user.get("groups", [])
        base_group = user.get("base_group", None)
        if base_group and base_group not in user_groups:
            user_groups.append(base_group)  # list of groups the user is in

        for user_group in user_groups:
            # "Business Administrators" is a OTCS generated group that we won't find
            # in payload - skip this group.
            if user_group == "Business Administrators":
                continue
            # Try to find the group dictionary item in the payload
            # for user group name:
            group = next(
                (item for item in self._groups if item["name"] == user_group),
                None,
            )
            if not group:
                self.logger.error(
                    "Cannot find group -> '%s'. Cannot establish membership in Salesforce. Skipping to next group...",
                    user_group,
                )
                success = False
                continue

            group_id = group.get("salesforce_id")  # Careful ID may not exist
            group_name = group["name"]
            if group_id is None:
                self.logger.info(
                    "Group -> '%s' does not have a Salesforce ID. Cannot add user -> '%s' to this Salesforce group (group may not be enabled for Salesforce). Skipping...",
                    group_name,
                    user_name,
                )
                # We don't treat this as an error - there may be payload groups which are not enabled for Salesforce!
                continue

            existing_members = self._salesforce.get_group_members(group_id=group_id)
            if not existing_members or not self._salesforce.exist_result_item(
                response=existing_members,
                key="UserOrGroupId",
                value=salesforce_user_id,
            ):
                self.logger.info(
                    "Add Salesforce user -> '%s' (%s) to Salesforce group -> '%s' (%s)...",
                    user_name,
                    salesforce_user_id,
                    group_name,
                    group_id,
                )
                response = self._salesforce.add_group_member(
                    group_id=group_id,
                    member_id=salesforce_user_id,
                )
                member_id = self._salesforce.get_result_value(
                    response=response,
                    key="id",
                )
                if not member_id:
                    self.logger.error(
                        "Failed to add Salesforce user -> '%s' (%s) as member to Salesforce group -> '%s' (%s)!",
                        user_name,
                        salesforce_user_id,
                        group_name,
                        group_id,
                    )
                    success = False
                    continue
            else:
                self.logger.info(
                    "Salesforce user -> '%s' (%s) does already exist in Salesforce group -> '%s' (%s). Skipping...",
                    user_name,
                    salesforce_user_id,
                    group_name,
                    group_id,
                )
        # end for loop user groups
    # end for loop users

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_users_sap(section_name='usersSAP')

Process users in payload and sync them with SAP (passwords only for now).

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'usersSAP'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise

Side Effects

The user items are modified by adding an "sap_sync_result" dict element that documents if the user password was properly synced with SAP.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users_sap")
def process_users_sap(self, section_name: str = "usersSAP") -> bool:
    """Process users in payload and sync them with SAP (passwords only for now).

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool: True if payload has been processed without errors, False otherwise

    Side Effects:
        The user items are modified by adding an "sap_sync_result" dict element
        that documents if the user password was properly synced with SAP.

    """

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    if not self._sap:
        self.logger.error("SAP connection is not initialized. Bailing out...")
        return False

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    rfc_name = "ZFM_TERRA_RFC_CHNG_USR_PW"
    rfc_description = "RFC to update the SAP user password"
    rfc_call_options = ()

    # Update SAP password for all users in payload:
    for user in self._users:
        user_name = user.get("name")
        if not user_name:
            self.logger.error("User is missing a login. Skipping to next user...")
            success = False
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        # Check if the user is explicitly enabled for SAP:
        if not user.get("enable_sap", False):
            self.logger.info(
                "User -> '%s' is not enabled for SAP. Skipping...",
                user_name,
            )
            continue

        # Lookup mandatory password in payload:
        user_password = user.get("password")
        if not user_password:
            self.logger.error(
                "User -> '%s' is missing a password. Cannot sync with SAP. Skipping to next user...",
                user_name,
            )
            success = False
            continue

        rfc_params = {
            "USERNAME": user_name,
            "PASSWORD": user_password,
        }

        self.logger.info(
            "Updating password of user -> '%s' in SAP. Calling SAP RFC -> '%s' (%s) with parameters -> %s ...",
            user_name,
            rfc_name,
            rfc_description,
            str(rfc_params),
        )

        result = self._sap.call(
            rfc_name=rfc_name,
            options=rfc_call_options,
            rfc_parameters=rfc_params,
        )
        if result is None:
            self.logger.error(
                "Failed to call SAP RFC -> '%s' to update password of user -> '%s'!",
                rfc_name,
                user_name,
            )
            success = False
        elif result.get("RESULT") != "OK":
            self.logger.error(
                "Result of SAP RFC -> '%s' is not OK, it returned -> %s failed items in result -> %s",
                rfc_name,
                str(result.get("FAILED")),
                str(result),
            )
            success = False
            # Save result for status file content
            user["sap_sync_result"] = result
        else:
            self.logger.info(
                "Successfully called RFC -> '%s'. Result -> %s.",
                rfc_name,
                str(result),
            )
            # Save result for status file content
            user["sap_sync_result"] = result

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_users_successfactors(section_name='usersSuccessFactors')

Process users in payload and sync them with SuccessFactors (passwords and email).

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'usersSuccessFactors'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Side Effects

The user items are modified by adding an "successfactors_user_id" dict element that includes the personIdExternal of the user in SuccessFactors

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_users_successfactors")
def process_users_successfactors(
    self,
    section_name: str = "usersSuccessFactors",
) -> bool:
    """Process users in payload and sync them with SuccessFactors (passwords and email).

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    Side Effects:
        The user items are modified by adding an "successfactors_user_id" dict element that
        includes the personIdExternal of the user in SuccessFactors

    """

    if not self._users:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    if not self._successfactors:
        self.logger.error(
            "SuccessFactors connection is not initialized. Bailing out...",
        )
        return False

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # traverse all users in payload:
    for user in self._users:
        user_name = user.get("name")
        if not user_name:
            self.logger.error("User is missing a login. Skipping to next user...")
            success = False
            continue

        # Check if user has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not user.get("enabled", True):
            self.logger.info(
                "Payload for user -> '%s' is disabled. Skipping...",
                user_name,
            )
            continue

        # Check if the user is explicitly enabled for SuccessFactors:
        if not user.get("enable_successfactors", False):
            self.logger.info(
                "User -> '%s' is not enabled for SuccessFactors. Skipping...",
                user_name,
            )
            continue

        # Lookup password and email in payload:
        user_password = user.get("password", "")
        user_email = user.get("email", "")

        # first we need to get the SuccessFactors user account object
        # to determine the personIdExternal that we need to update the
        # SuccessFactors user.
        response = self._successfactors.get_user_account(username=user_name)
        user_id = self._successfactors.get_result_value(
            response=response,
            key="personIdExternal",
        )
        if user_id is None:
            self.logger.error(
                "Failed to get personIDExternal of SuccessFactors user -> '%s'!",
                user_name,
            )
            success = False
            continue
        self.logger.info(
            "SuccessFactors user -> '%s' has external user ID -> %s.",
            user_name,
            str(user_id),
        )

        # Now let's update the user password and email address:
        update_data = {}
        if user_password:
            self.logger.info(
                "Updating password of SuccessFactors user -> '%s' (%s)...",
                user_name,
                str(user_id),
            )
            update_data["password"] = user_password
        if user_email:
            update_data["email"] = user_email

        response = self._successfactors.update_user(
            user_id=user_id,
            update_data=update_data,
        )
        if response:
            self.logger.info(
                "Successfully updated SuccessFactors user -> '%s'.",
                str(user_name),
            )
            # Save result for status file content
            user["successfactors_user_id"] = user_id
        else:
            self.logger.error(
                "Failed to update SuccessFactors user -> '%s'! Skipping...",
                user_name,
            )
            success = False
            continue

        if not user_email:
            continue

        self.logger.info(
            "Updating email of SuccessFactors user -> '%s' to -> %s...",
            user_name,
            user_email,
        )
        response = self._successfactors.update_user_email(
            user_id=user_id,
            email_address=user_email,
        )
        if response:
            self.logger.info(
                "Successfully updated email address of SuccessFactors user -> '%s' to -> '%s'.",
                user_name,
                user_email,
            )
        else:
            self.logger.error(
                "Failed to update email address of SuccessFactors user -> '%s' to -> '%s'!",
                user_name,
                user_email,
            )
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._users,
    )

    return success

process_web_hooks(webhooks, section_name='webHooks')

Process Web Hooks in payload and do HTTP requests.

Parameters:

Name Type Description Default
webhooks list

The list of web hook payload settings.

required
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'webHooks'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_web_hooks")
def process_web_hooks(self, webhooks: list, section_name: str = "webHooks") -> bool:
    """Process Web Hooks in payload and do HTTP requests.

    Args:
        webhooks (list):
            The list of web hook payload settings.
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not webhooks:
        self.logger.info(
            "Payload section -> %s is empty. Skipping...",
            section_name,
        )
        return True

    # Even if this payload section has been processed successfully before we
    # want to call the webhook.

    success: bool = True

    for webhook in webhooks:
        url = webhook.get("url")

        # Check if element has been disabled in payload (enabled = false).
        # In this case we skip the element:
        enabled = webhook.get("enabled", True)

        if not enabled and not url:
            self.logger.info("Payload for Web Hook is disabled. Skipping...")
            continue
        if not url:
            self.logger.info("Web Hook does not have a url. Skipping...")
            success = False
            continue
        if not enabled:
            self.logger.info(
                "Payload for Web Hook -> '%s' is disabled. Skipping...",
                url,
            )
            continue

        description = webhook.get("description")

        method = webhook.get("method", "POST")

        payload = webhook.get("payload", {})

        headers = webhook.get("headers", {})

        if description:
            self.logger.info(
                "Calling Web Hook -> %s: %s (%s)...",
                method,
                url,
                description,
            )
        else:
            self.logger.info("Calling Web Hook -> %s: %s...", method, url)

        response = self._http_object.http_request(
            url=url,
            method=method,
            payload=payload,
            headers=headers,
            retries=webhook.get("retries", 0),
            wait_time=webhook.get("wait_time", 0),
        )
        if not response or not response.ok:
            success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=webhooks,
    )

    return success

process_web_reports(web_reports, section_name='webReports')

Process web reports in payload and run them in Content Server.

Parameters:

Name Type Description Default
web_reports list

The payload list of web reports. As we have two different list (pre and post) we need to pass the actual list as parameter.

required
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'webReports'

Returns:

Name Type Description
bool bool

True, if a restart of the OTCS pods is required. False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_web_reports")
def process_web_reports(
    self,
    web_reports: list,
    section_name: str = "webReports",
) -> bool:
    """Process web reports in payload and run them in Content Server.

    Args:
        web_reports (list):
            The payload list of web reports. As we have two different list (pre and post)
            we need to pass the actual list as parameter.
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if a restart of the OTCS pods is required. False otherwise.

    """

    if not web_reports:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return False  # important to return False here as otherwise we are triggering a restart of services!!

    # If this payload section has been processed successfully before we
    # can return False and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return False  # important to return False here as otherwise we are triggering a restart of services!!

    restart_required: bool = False
    success: bool = True

    for web_report in web_reports:
        nickname = web_report.get("nickname")
        if not nickname:
            self.logger.error("Web Report payload needs a nickname! Skipping...")
            continue

        # Check if web report has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not web_report.get("enabled", True):
            self.logger.info(
                "Payload for Web Report -> '%s' is disabled. Skipping...",
                nickname,
            )
            continue

        description = web_report.get("description", "")
        restart = web_report.get("restart", False)

        if not self._otcs.get_node_from_nickname(nickname=nickname):
            self.logger.error(
                "Web Report with nickname -> '%s' does not exist! Skipping...",
                nickname,
            )
            success = False
            continue

        # be careful to avoid key errors as Web Report parameters are optional:
        actual_params = web_report["parameters"] if web_report.get("parameters") else {}
        formal_params = self._otcs.get_web_report_parameters(nickname=nickname)
        if actual_params:
            self.logger.info(
                "Running Web Report -> '%s' (%s) with parameters -> %s ...",
                nickname,
                description,
                actual_params,
            )
            # Do some sanity checks to see if the formal and actual parameters are matching...
            # Check 1: are there formal parameters at all?
            if not formal_params:
                self.logger.error(
                    "Web Report -> '%s' is called with actual parameters but it does not expect parameters! Skipping...",
                    nickname,
                )
                success = False
                continue
            lets_continue = False
            # Check 2: Iterate through the actual parameters given in the payload
            # and see if there's a matching formal parameter expected by the Web Report:
            for key, value in actual_params.items():
                # Check if there's a matching formal parameter defined on the Web Report node:
                formal_param = next(
                    (item for item in formal_params if item["parm_name"] == key),
                    None,
                )
                if formal_param is None:
                    self.logger.error(
                        "Web Report -> '%s' is called with parameter -> '%s' that is not expected! Value: %s) Skipping...",
                        nickname,
                        key,
                        value,
                    )
                    success = False
                    lets_continue = True  # we cannot do a "continue" here directly as we are in an inner loop
            # Check 3: Iterate through the formal parameters and validate there's a matching
            # actual parameter defined in the payload for each mandatory formal parameter
            # that does not have a default value:
            for formal_param in formal_params:
                if (
                    (formal_param["mandatory"] is True)
                    and (formal_param["default_value"] is None)
                    and not actual_params.get(formal_param["parm_name"])
                ):
                    self.logger.error(
                        "Web Report -> '%s' is called without mandatory parameter -> %s! Skipping...",
                        nickname,
                        formal_param["parm_name"],
                    )
                    success = False
                    lets_continue = True  # we cannot do a "continue" here directly as we are in an inner loop
            # Did any of the checks fail?
            if lets_continue:
                continue
            # Actual parameters are validated, we can run the Web Report:
            response = self._otcs.run_web_report(
                nickname=nickname,
                web_report_parameters=actual_params,
            )
        # end if actual_params
        else:
            self.logger.info(
                "Running Web Report -> '%s' (%s) without parameters...",
                nickname,
                description,
            )
            # Check if there's a formal parameter that is mandatory but
            # does not have a default value:
            if formal_params:
                required_param = next(
                    (item for item in formal_params if (item["mandatory"] is True) and (not item["default_value"])),
                    None,
                )
                if required_param:
                    self.logger.error(
                        "Web Report -> '%s' is called without parameters but has a mandatory parameter -> '%s' without a default value! Skipping...",
                        nickname,
                        required_param["parm_name"],
                    )
                    success = False
                    continue
                # we are good to proceed!
                self.logger.debug(
                    "Web Report -> '%s' does not have a mandatory parameter without a default value!",
                    nickname,
                )
            response = self._otcs.run_web_report(nickname=nickname)
        # end else
        if response is None:
            self.logger.error(
                "Failed to run web report with nickname -> '%s'!",
                nickname,
            )
            success = False

        if restart:
            restart_required = True

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=web_reports,
    )

    return restart_required

process_workflow_attributes(attributes, workflow_attribute_definition)

Process the attributes in the workflow steps.

This method adds the IDs for the attribute to the payload dicts. The IDs are needed for the workflow REST API calls.

Parameters:

Name Type Description Default
attributes list

The list of attributes (payload) processed in the workflow step.

required
workflow_attribute_definition dict

The workflow attribute definition.

required

Returns:

Type Description
None

None. The mutable dictionary in the workflow_step is updated with the IDs.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workflow_attributes")
def process_workflow_attributes(
    self,
    attributes: list,
    workflow_attribute_definition: dict,
) -> None:
    """Process the attributes in the workflow steps.

    This method adds the IDs for the attribute to the payload dicts.
    The IDs are needed for the workflow REST API calls.

    Args:
        attributes (list):
            The list of attributes (payload) processed in the workflow step.
        workflow_attribute_definition (dict):
            The workflow attribute definition.

    Returns:
        None. The mutable dictionary in the workflow_step is updated with the IDs.

    """

    # now we need to get the technical attribute IDs from
    # the workflow definition and map them
    # with the attribute names from the payload:
    for attribute in attributes:
        attribute_name = attribute["name"]
        attribute_value = attribute["value"]
        attribute_type = attribute.get("type", None)

        # Special treatment for type user: determine the ID for the login name.
        # the ID is the actual value we have to put in the attribute:
        if attribute_type and attribute_type.lower() == "user":
            user = self._otcs.get_user(name=attribute_value, show_error=True)
            user_id = self._otcs.get_result_value(response=user, key="id")
            if not user_id:
                self.logger.error(
                    "Cannot find user with login name -> '%s'. Skipping...",
                    attribute_value,
                )
                continue
            attribute_value = user_id
            attribute["value"] = user_id

        attribute_definition = workflow_attribute_definition.get(attribute_name)
        if not attribute_definition:
            self.logger.error(
                "Cannot find the attribute -> '%s' in the workflow definition. Skipping...",
            )
            continue
        # Enrich the attribute dictionary with the attribute ID from the workflow definition:
        attribute["id"] = attribute_definition["id"]
        # Enrich the attribute dictionary with the attribute form ID from the workflow definition:
        attribute["form_id"] = attribute_definition["form_id"]

    if attributes:
        self.logger.info(
            "Updated workflow step attributes with IDs -> %s.",
            str(attributes),
        )

process_workflow_step(workflow_id, workflow_step, workflow_attribute_definition, documents=None, process_id=None)

Process a workflow step of a workflow.

Parameters:

Name Type Description Default
workflow_id int

The node ID of the workflow (the workflow map).

required
workflow_step dict

Payload dictionary for a single workflow step. Keys: "action", "exec_as_user", "attributes"

required
workflow_attribute_definition dict

The workflow attribute definition.

required
documents list | None

The list of workflow documents (attachments9).

None
process_id int | None

The process ID of the workflow.

None

Returns:

Name Type Description
bool bool

True, if the workflow step has been processed successfully, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workflow_step")
def process_workflow_step(
    self,
    workflow_id: int,
    workflow_step: dict,
    workflow_attribute_definition: dict,
    documents: list | None = None,
    process_id: int | None = None,
) -> bool:
    """Process a workflow step of a workflow.

    Args:
        workflow_id (int):
            The node ID of the workflow (the workflow map).
        workflow_step (dict):
            Payload dictionary for a single workflow step.
            Keys: "action", "exec_as_user", "attributes"
        workflow_attribute_definition (dict):
            The workflow attribute definition.
        documents (list | None, optional):
            The list of workflow documents (attachments9).
        process_id (int | None, optional):
            The process ID of the workflow.

    Returns:
        bool:
            True, if the workflow step has been processed successfully, False otherwise.

    """

    if "action" not in workflow_step:
        self.logger.error("Missing workflow action in workflow step.")
        return False
    action = workflow_step["action"]

    if "exec_as_user" not in workflow_step:
        self.logger.error("Missing workflow user in workflow step.")
        return False
    exec_as_user = workflow_step["exec_as_user"]

    # Find the user in the users payload:
    exec_user = next(
        (item for item in self._users if item["name"] == exec_as_user),
        None,
    )
    # Have we found the user in the payload?
    if exec_user is None:
        self.logger.error(
            "Cannot find user with login name -> '%s' for workflow processing.",
            exec_as_user,
        )
        return False

    self.logger.info("Executing workflow step as user -> '%s'...", exec_as_user)

    # Impersonate as the user:
    self.logger.info("Impersonate user -> '%s'...", exec_as_user)
    # True = force new login with new user
    result = self.start_impersonation(username=exec_as_user)
    if not result:
        self.logger.error("Couldn't impersonate user -> '%s'", exec_as_user)
        return False

    # "attributes" is optional:
    if "attributes" not in workflow_step:
        self.logger.warning(
            "No workflow attributes specified in the payload for this workflow step.",
        )
        attributes = []
        workflow_step_values = None
    else:
        attributes = workflow_step["attributes"]
        self.logger.info(
            "Workflow step has attributes -> %s. Adding attribute IDs to the payload names...",
            str(attributes),
        )
        # Update / enrich the attributes in the workflow step with the IDs
        # from the workflow definition (this CHANGES the attributes!)
        self.process_workflow_attributes(
            attributes=attributes,
            workflow_attribute_definition=workflow_attribute_definition,
        )
        # Prepare the data for the REST call to
        # update the process:
        workflow_step_values = {
            attr["form_id"]: attr["value"] for attr in attributes if "form_id" in attr and "value" in attr
        }

    if action == "Initiate":
        # Create a draft process in preparation for the workflow initiation:
        response = self._otcs.create_draft_process(
            workflow_id=workflow_id,
            documents=documents,
            attach_documents=True,
        )
        draftprocess_id = self._otcs.get_result_value(
            response=response,
            key="draftprocess_id",
            property_name="",
        )
        if not draftprocess_id:
            self.logger.error(
                "Failed to create draft process for workflow ID -> %s as user -> '%s'!",
                str(workflow_id),
                exec_as_user,
            )
            # Stop the impersonation as a user:
            result = self.stop_impersonation()
            return False
        else:
            self.logger.info(
                "Successfully generated draft process with ID -> %s%s.",
                str(draftprocess_id),
                " attching document IDs -> " + str(documents) if documents else "",
            )
        workflow_step["draftprocess_id"] = draftprocess_id

        # Check if a due date is specified. The payload has
        # a relative offset in number of days that we add to
        # the current date:
        due_in_days = workflow_step.get("due_in_days")
        if due_in_days:
            due_date = datetime.now(UTC) + timedelta(days=int(due_in_days))
            due_date = due_date.strftime("%Y-%m-%d")
        else:
            due_date = None
        # Record the due date in the workflow step dictionary
        workflow_step["due_date"] = due_date

        # Update the draft process with title, due date
        # and workflow attribute values from the payload:
        response = self._otcs.update_draft_process(
            draftprocess_id=draftprocess_id,
            title=workflow_step.get("title"),
            due_date=due_date,
            values=workflow_step_values,
        )

        # Initiate the draft process. This creates
        # the running workflow instance:
        response = self._otcs.initiate_draft_process(
            draftprocess_id=draftprocess_id,
            comment=workflow_step.get("comment"),
        )
        process_id = self._otcs.get_result_value(
            response=response,
            key="process_id",
            property_name="",
        )
        if not process_id:
            self.logger.error(
                "Failed to initiate process for workflow with ID -> %s as user -> '%s'!",
                str(workflow_id),
                exec_as_user,
            )
            # Stop the impersonation as a user:
            result = self.stop_impersonation()
            return False
        self.logger.info(
            "Successfully initiated process with ID -> %s for workflow with ID -> %s as user -> '%s'.",
            str(process_id),
            str(workflow_id),
            exec_as_user,
        )
        workflow_step["process_id"] = process_id
    # end if action == "Initiate"
    else:
        if not process_id:
            self.logger.error(
                "Workflow step cannot be executed as process is not initiated (process ID not set)",
            )
            # Stop the impersonation as a user:
            result = self.stop_impersonation()
            return False
        response = self._otcs.get_process_task(
            process_id=process_id,
        )
        # Are there any workflow attributes to update with new values?
        if attributes:
            response = self._otcs.update_process_task(
                process_id=process_id,
                values=workflow_step_values,
            )
        # Execute the step action defined in the payload
        response = self._otcs.update_process_task(
            process_id=process_id,
            action=action,
        )

    # Impersonate as the admin user:
    self.logger.info(
        "Impersonate as admin user -> '%s'...",
        self._otcs.credentials()["username"],
    )
    # Stop the impersonation as a user:
    result = self.stop_impersonation()

    return True

process_workflows(section_name='workflows')

Initiate and process workflows for a defined workspace type and folder path.

Parameters:

Name Type Description Default
section_name str

The name of the section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'workflows'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workflows")
def process_workflows(self, section_name: str = "workflows") -> bool:
    """Initiate and process workflows for a defined workspace type and folder path.

    Args:
        section_name (str, optional):
            The name of the section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise.

    """

    if not self._workflows:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    # save admin credentials for later switch back to admin user:
    admin_credentials = self._otcs.credentials()

    for workflow in self._workflows:
        workflow_nickname = workflow.get("workflow_nickname")
        if not workflow_nickname:
            self.logger.error(
                "To initiate and process workflows for documents in workspaces the workflow nickname needs to be specified in the payload! Skipping to next workflow initiation...",
            )
            success = False
            continue
        workflow_node = self._otcs.get_node_from_nickname(
            nickname=workflow_nickname,
        )
        workflow_id = self._otcs.get_result_value(response=workflow_node, key="id")
        workflow_name = self._otcs.get_result_value(
            response=workflow_node,
            key="name",
        )
        if not workflow_id:
            self.logger.error(
                "Cannot find workflow by nickname -> '%s'! Skipping to next workflow...",
                workflow_nickname,
            )
            success = False
            continue

        workspace_type = workflow.get("workspace_type")
        if not workspace_type:
            self.logger.error(
                "To process workflow -> '%s' for documents in workspaces the workspace type needs to be specified in the payload! Skipping to next workflow...",
                workflow_name,
            )
            success = False
            continue

        # Check if workflow has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not workflow.get("enabled", True):
            self.logger.info(
                "Payload for workflow -> '%s' of workspace type -> '%s' is disabled. Skipping...",
                workflow_name,
                workspace_type,
            )
            continue

        # Find the workspace type with the name given in the _workspace_types
        # datastructure that has been generated by process_workspace_types() method before:
        workspace_type_id = next(
            (item["id"] for item in self._workspace_types if item["name"] == workspace_type),
            None,
        )

        workspace_folder_path = workflow.get("workspace_folder_path", [])
        if not workspace_folder_path:
            self.logger.info(
                "No workspace folder path defined for workspaces of type -> '%s'. Workflows will be started for documents in workspace root.",
                workspace_type,
            )

        workflow_steps = workflow.get("steps")
        if not workflow_steps:
            self.logger.error(
                "To process workflow -> '%s', workflow steps ('steps') needs to be specified in the payload! Skipping to next workflow initiation...",
                workflow_name,
            )
            success = False
            continue

        # Get the attribute details (name, ID, type) from the workflow definition:
        workflow_attribute_definition = self._otcs.get_workflow_attributes(
            workflow_id=workflow_id,
        )

        # The workspace type name is used as a "starts with" search on the
        # workspace type name. This can cause issues, so we prefer to use the type ID
        # if we have it. Otherwise the REST API prefers the name over the type:
        workspace_instances = self._otcs.get_workspace_instances_iterator(
            type_name=workspace_type if not workspace_type_id else None,
            type_id=workspace_type_id,
        )
        for workspace_instance in workspace_instances:
            workspace = workspace_instance.get("data").get("properties")
            workspace_id = workspace["id"]
            workspace_name = workspace["name"]

            if workspace_folder_path:
                workspace_folder = self._otcs.get_node_by_workspace_and_path(
                    workspace_id=workspace_id,
                    path=workspace_folder_path,
                )
                if workspace_folder:
                    workspace_folder_id = self._otcs.get_result_value(
                        response=workspace_folder,
                        key="id",
                    )
                else:
                    # If the workspace template is not matching
                    # the path we may have an error here. Then
                    # we fall back to workspace root level.
                    self.logger.warning(
                        "Folder path does not exist in workspace -> '%s'. Using workspace root level instead...",
                        workspace_name,
                    )
                    workspace_folder_id = workspace_id
            else:
                workspace_folder_id = workspace_id

            # Get all documents (-3 = non-container) from the defined folder:
            response = self._otcs.get_subnodes(
                parent_node_id=workspace_folder_id,
                filter_node_types=-3,
            )
            documents = self._otcs.get_result_values(response=response, key="id")

            process_id = None
            for workflow_step in workflow_steps:
                result = self.process_workflow_step(
                    workflow_id=workflow_id,
                    workflow_step=workflow_step,
                    workflow_attribute_definition=workflow_attribute_definition,
                    documents=documents,
                    process_id=process_id,
                )
                # If the step fails we are bailing out as it doesn't make
                # sense to continue with further steps:
                if not result:
                    success = False
                    break
                if "process_id" in workflow_step:
                    process_id = workflow_step["process_id"]
        # end for workspace_instance in workspace_instances:
    # end for workflow in self._workflows

    # Set back admin credentials:
    self._otcs.set_credentials(
        username=admin_credentials["username"],
        password=admin_credentials["password"],
    )
    # Authenticate back as the admin user:
    self.logger.info(
        "Authenticate as admin user -> '%s'...",
        admin_credentials["username"],
    )
    # True = force new login with new user
    self._otcs.authenticate(revalidate=True)

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._workflows,
    )

    return success

process_workspace(workspace)

Worker thread for workspace creation.

Parameters:

Name Type Description Default
workspace dict

Dictionary with payload of a single workspace.

required

Returns:

Name Type Description
bool bool

True = Success, False = Failure

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace")
def process_workspace(
    self,
    workspace: dict,
) -> bool:
    """Worker thread for workspace creation.

    Args:
        workspace (dict):
            Dictionary with payload of a single workspace.

    Returns:
        bool:
            True = Success, False = Failure

    """

    # Read name from payload:
    if "name" not in workspace:
        self.logger.error("Workspace needs a name! Skipping to next workspace...")
        return False
    name = workspace["name"]

    # Check if workspace has been explicitly disabled in payload
    # (enabled = false). In this case we skip the element:
    if not workspace.get("enabled", True):
        self.logger.info(
            "Payload for workspace -> '%s' is disabled. Skipping...",
            name,
        )
        return True

    # Read optional description from payload:
    description = workspace.get("description", "")

    # Read Type Name from payload:
    if "type_name" not in workspace:
        self.logger.error(
            "Workspace -> '%s' needs a type name! Skipping to next workspace...",
            name,
        )
        return False
    type_name = workspace["type_name"]

    # We need to do this early to find out if we have a cross-application workspace
    # and need to continue even if the workspace does exist...
    if workspace.get("business_objects"):
        business_objects = workspace["business_objects"]

        business_object_list = self.prepare_workspace_business_objects(
            workspace=workspace,
            business_objects=business_objects,
        )
        # Check if any of the external systems are avaiable:
        if business_object_list:
            self.logger.info(
                "Workspace -> '%s' will be connected to -> %s business object%s.",
                name,
                str(len(business_object_list)),
                "s" if len(business_object_list) > 1 else "",
            )
    else:
        self.logger.debug(
            "Workspace -> '%s' is not connected to any business object.",
            name,
        )
        business_object_list = []

    # Intialize cross-application workspace to "off":
    ibo_workspace_id = None

    # check if the workspace has been created before (effort to make the customizing code idem-potent)
    self.logger.debug(
        "Check if workspace -> '%s' of type -> '%s' does already exist...",
        name,
        type_name,
    )
    # Check if workspace does already exist
    # In case the workspace exists, determine_workspace_id()
    # also stores the node ID into workspace["nodeId"]
    workspace_id = self.determine_workspace_id(workspace=workspace)
    if workspace_id:
        self.logger.info(
            "Workspace -> '%s' of type -> '%s' does already exist and has ID -> %d.",
            name,
            type_name,
            workspace_id,
        )
        # Check if we have an existing workspace that is cross-application.
        # In this case we cannot just skip (return).
        if len(business_object_list) > 1:
            ibo_workspace_id = workspace_id
            self.logger.info(
                "Workspace -> '%s' is a cross-application workspace so we cannot skip the creation...",
                name,
            )
        elif not business_object_list:
            self.logger.info(
                "Workspace -> '%s' does already exist and has no business object references to update - skipping the creation...",
                name,
            )
            return True

    # Parent ID is optional and only required if workspace type does not specify a create location.
    # This is typically the case if it is a nested workspace or workspaces of the same type can be created
    # in different locations in the Enterprise Workspace:
    parent_id = workspace.get("parent_id")

    if parent_id is not None:
        parent_workspace = next(
            (item for item in self._workspaces if item["id"] == parent_id),
            None,
        )
        if parent_workspace is None:
            self.logger.error(
                "Parent workspace with logical ID -> %s not found.",
                parent_id,
            )
            return False

        parent_workspace_node_id = self.determine_workspace_id(
            workspace=parent_workspace,
        )
        if not parent_workspace_node_id:
            self.logger.error(
                "Parent workspace without node ID (parent workspace creation may have failed). Skipping to next workspace...",
            )
            return False

        self.logger.debug(
            "Parent workspace with logical ID -> %s has node ID -> %s",
            parent_id,
            parent_workspace_node_id,
        )
    else:
        # Alternatively a path could be specified in the payload:
        parent_path = workspace.get("parent_path")
        if parent_path:
            self.logger.info(
                "Workspace -> '%s' has parent path -> %s specified in payload.",
                name,
                parent_path,
            )
            response = self._otcs.get_node_by_volume_and_path(
                volume_type=self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE,
                path=parent_path,
                create_path=True,
            )
            parent_workspace_node_id = self._otcs.get_result_value(
                response=response,
                key="id",
            )
        else:
            # if no parent_id is specified the workspace location is determined by the workspace type definition
            # and we pass None as parent ID to the get_workspace_create_form and create_workspace methods below:
            parent_workspace_node_id = None
            self.logger.info(
                "Workspace -> '%s' has no parent path specified in payload. Using the default defined in the workspace type...",
                name,
            )

    # Find the workspace type with the name given in the payload:
    workspace_type = next(
        (item for item in self._workspace_types if item["name"] == type_name),
        None,
    )
    if workspace_type is None:
        self.logger.error(
            "Workspace type -> '%s' not found. Skipping to next workspace...",
            type_name,
        )
        return False
    if workspace_type["templates"] == []:
        self.logger.error(
            "Workspace type -> '%s' does not have templates. Skipping to next workspace...",
            type_name,
        )
        return False

    # check if the template to be used is specified in the payload:
    if "template_name" in workspace:
        template_name = workspace["template_name"]
        workspace_template = next(
            (item for item in workspace_type["templates"] if item["name"] == template_name),
            None,
        )
        if workspace_template:  # does this template exist?
            self.logger.debug(
                "Workspace template -> '%s' has been specified in payload and it does exist.",
                template_name,
            )
        else:
            self.logger.error(
                "Workspace template -> '%s' has been specified in payload but it doesn't exist!",
                template_name,
            )
            self.logger.error(
                "Workspace type -> '%s' has only these templates -> %s",
                type_name,
                workspace_type["templates"],
            )
            return False
    # template to be used is NOT specified in the payload - then we just take the first one:
    else:
        workspace_template = workspace_type["templates"][0]
        self.logger.info(
            "Workspace template has not been specified in payload - taking the first one (%s)...",
            workspace_template,
        )

    template_id = workspace_template["id"]
    template_name = workspace_template["name"]
    workspace_type_id = workspace_type["id"]

    if not workspace_id:
        self.logger.info(
            "Create workspace -> '%s' (type -> '%s') from workspace template -> '%s' (%s)%s...",
            name,
            type_name,
            template_name,
            template_id,
            " with business object references -> {}".format(business_object_list) if business_object_list else "",
        )
    elif business_object_list:
        self.logger.info(
            "Update workspace -> '%s' (type -> '%s') business object references with -> %s...",
            name,
            type_name,
            str(business_object_list),
        )

    # Handle the case where the workspace is not connected
    # to any external system / business object:
    if not business_object_list:
        business_object_list.append(
            {
                "ext_system_id": None,
                "bo_type": None,
                "bo_id": None,
            },
        )

    for business_object in business_object_list:
        external_system_id = business_object["ext_system_id"]
        bo_type = business_object["bo_type"]
        bo_id = business_object["bo_id"]

        if workspace_id and not ibo_workspace_id and bo_id:
            # See if the existing workspace does not yet have a business object references
            # that is given in the payload:
            self.logger.info("Get existing workspace references for workspace -> '%s' (%d)...", name, workspace_id)
            workspace_references = self._otcs.get_workspace_references(node_id=workspace_id)
            workspace_reference = next(
                (
                    item
                    for item in workspace_references or []
                    if item["external_system_id"] == external_system_id
                    and item["business_object_id"] == bo_id
                    and item["business_object_type"].lower() == bo_type.lower()
                ),
                None,
            )
            if not workspace_reference:
                self.logger.info(
                    "Set workspace reference for workspace -> '%s' (%d) to business object -> ('%s', '%s', %s)...",
                    name,
                    workspace_id,
                    external_system_id,
                    bo_type,
                    bo_id,
                )
                response = self._otcs.set_workspace_reference(
                    workspace_id=workspace_id, external_system_id=external_system_id, bo_type=bo_type, bo_id=bo_id
                )
                if not response:
                    self.logger.error(
                        "Failed to set a reference for workspace -> '%s' (%d) to business object -> ('%s', '%s', %s)!",
                        name,
                        workspace_id,
                        external_system_id,
                        bo_type,
                        bo_id,
                    )
                    return False
            else:
                self.logger.info(
                    "Workspace -> '%s' (%d) has already a reference to business object -> ('%s', '%s', %s).",
                    name,
                    workspace_id,
                    external_system_id,
                    bo_type,
                    bo_id,
                )
            continue

        # Read categories from payload:
        if "categories" in workspace:
            categories = workspace["categories"]
            workspace_category_data = self.prepare_workspace_create_form(
                categories=categories,
                template_id=template_id,
                ext_system_id=business_object["ext_system_id"],
                bo_type=business_object["bo_type"],
                bo_id=business_object["bo_id"],
                parent_workspace_node_id=parent_workspace_node_id,
            )
            if not workspace_category_data:
                self.logger.error(
                    "Failed to prepare the category data for workspace -> '%s'!",
                    name,
                )
                return False
        else:
            self.logger.debug(
                "Workspace payload has no category data! Will leave category attributes empty...",
            )
            workspace_category_data = {}

        if ibo_workspace_id:
            self.logger.info(
                "Connect existing workspace -> '%s' to an additional business object of type -> '%s' and ID -> '%s' (IBO)...",
                name,
                business_object["bo_type"],
                business_object["bo_id"],
            )
        # Create the workspace with all provided information:
        response = self._otcs.create_workspace(
            workspace_template_id=template_id,
            workspace_name=name,
            workspace_description=description,
            workspace_type=workspace_type_id,
            category_data=workspace_category_data,
            external_system_id=business_object["ext_system_id"],
            bo_type=business_object["bo_type"],
            bo_id=business_object["bo_id"],
            parent_id=parent_workspace_node_id,
            ibo_workspace_id=ibo_workspace_id,
            show_error=(
                not self._sap
            ),  # if SAP is active it may produce workspaces concurrently (race condition). Then we don't want to issue errors.
        )
        if response is None:
            # Check if workspace has been concurrently created by some other
            # process (e.g. via SAP or Salesforce). This would be a race condition
            # that seems to really occur.

            # we wait a bit to give the concurrent thread the chance to fully finish the creation:
            time.sleep(5)

            workspace_id = self.determine_workspace_id(workspace=workspace)
            if workspace_id:
                self.logger.info(
                    "Workspace -> '%s' of type -> '%s' has been created by an external process and has ID -> %s.",
                    name,
                    type_name,
                    workspace_id,
                )
                workspace["nodeId"] = workspace_id
            else:
                self.logger.error(
                    "Failed to create workspace -> '%s' of type -> '%s'!",
                    name,
                    type_name,
                )
                return False
        # Now we add the node ID of the new workspace to the payload data structure
        # This will be reused when creating the workspace relationships!
        elif not ibo_workspace_id:
            workspace["nodeId"] = self._otcs.get_result_value(
                response=response,
                key="id",
            )
            workspace_id = workspace["nodeId"]
            ibo_workspace_id = workspace["nodeId"]

            # We also get the name the workspace was finally created with.
            # This can be different form the name in the payload as additional
            # naming conventions from the Workspace Type definitions may apply.
            # This is important to make the python container idem-potent.
            response = self._otcs.get_workspace(node_id=workspace["nodeId"])
            workspace["name"] = self._otcs.get_result_value(
                response=response,
                key="name",
            )
            # Also update the 'name' variable accordingly, as it is used below.
            name = workspace["name"]

            self.logger.info(
                "Successfully created workspace with final name -> '%s', type -> '%s', and node ID -> %s.",
                workspace["name"],
                type_name,
                workspace["nodeId"],
            )
    # end for business_object in business_object_list

    # if the workspace creation has failed - e.g. error in lookup of business
    # object in external system then we continue with the next workspace:
    if "nodeId" not in workspace:
        self.logger.error(
            "Couldn't create the workspace -> '%s'. Skipping to next workspace...",
            workspace["name"],
        )
        return False

    # Check if there's an workspace nickname configured:
    if "nickname" in workspace:
        nickname = workspace["nickname"]
        self.logger.info(
            "Assign nickname -> '%s' to workspace -> '%s' (%s)...",
            nickname,
            name,
            workspace["nodeId"],
        )
        response = self._otcs.set_node_nickname(
            node_id=workspace["nodeId"],
            nickname=nickname,
            show_error=True,
        )
        if not response:
            self.logger.error(
                "Failed to assign nickname -> '%s' to workspace -> '%s' (%s)!",
                nickname,
                name,
                workspace["nodeId"],
            )

    # Check if there's an workspace icon/image configured:
    if "image_nickname" in workspace:
        image_nickname = workspace["image_nickname"]

        response = self._otcs.get_node_from_nickname(nickname=image_nickname)
        node_id = self._otcs.get_result_value(response=response, key="id")
        if node_id:
            mime_type = self._otcs.get_result_value(
                response=response,
                key="mime_type",
            )
            if not mime_type:
                self.logger.warning(
                    "Missing mime type information - assuming 'image/png'...",
                )
                mime_type = "image/png"
            file_path = os.path.join(tempfile.gettempdir(), image_nickname)
            result = self._otcs.download_document(node_id=node_id, file_path=file_path)
            if not result:
                self.logger.error(
                    "Failed to download workspace image with nickname -> '%s' to file -> '%s'!",
                    image_nickname,
                    file_path,
                )
            else:
                response = self._otcs.update_workspace_icon(
                    workspace_id=workspace["nodeId"],
                    file_path=file_path,
                    file_mimetype=mime_type,
                )
                if not response:
                    self.logger.error(
                        "Failed to assign icon -> '%s' to workspace -> '%s' from file -> '%s'!",
                        image_nickname,
                        name,
                        file_path,
                    )
        else:
            self.logger.error(
                "Cannot find workspace image with nickname -> '%s' for workspace -> '%s'!",
                image_nickname,
                name,
            )

    # Check if an RM classification is specified for the workspace:
    # RM Classification is specified as list of path elements (top-down)
    if "rm_classification_path" in workspace and workspace["rm_classification_path"] != []:
        rm_class_node = self._otcs.get_node_by_volume_and_path(
            volume_type=self._otcs.VOLUME_TYPE_CLASSIFICATION_VOLUME,
            path=workspace["rm_classification_path"],
        )
        rm_class_node_id = self._otcs.get_result_value(
            response=rm_class_node,
            key="id",
        )
        if rm_class_node_id:
            response = self._otcs.assign_rm_classification(
                node_id=workspace["nodeId"],
                rm_classification=rm_class_node_id,
                apply_to_sub_items=False,
            )
            if response is None:
                self.logger.error(
                    "Failed to assign RM classification -> '%s' (%s) to workspace -> '%s'!",
                    workspace["rm_classification_path"][-1],
                    rm_class_node_id,
                    name,
                )
            else:
                self.logger.info(
                    "Assigned RM Classification -> '%s' to workspace -> '%s'.",
                    workspace["rm_classification_path"][-1],
                    name,
                )
    # Check if one or multiple classifications are specified for the workspace
    # Classifications are specified as list of path elements (top-down)
    if "classification_pathes" in workspace and workspace["classification_pathes"] != []:
        for classification_path in workspace["classification_pathes"]:
            class_node = self._otcs.get_node_by_volume_and_path(
                volume_type=self._otcs.VOLUME_TYPE_CLASSIFICATION_VOLUME,
                path=classification_path,
            )
            class_node_id = self._otcs.get_result_value(
                response=class_node,
                key="id",
            )
            if class_node_id:
                response = self._otcs.assign_classifications(
                    node_id=workspace["nodeId"],
                    classifications=[class_node_id],
                    apply_to_sub_items=False,
                )
                if response is None:
                    self.logger.error(
                        "Failed to assign classification -> '%s' to workspace -> '%s'!",
                        class_node_id,
                        name,
                    )
                else:
                    self.logger.info(
                        "Successfully assigned Classification -> '%s' to workspace -> '%s'.",
                        classification_path[-1],
                        name,
                    )

    return True

process_workspace_aviators(section_name='workspaceAviators')

Process workspaces Content Aviator settings in payload and enable Aviator for selected workspaces.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'workspaceAviators'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_aviators")
def process_workspace_aviators(
    self,
    section_name: str = "workspaceAviators",
) -> bool:
    """Process workspaces Content Aviator settings in payload and enable Aviator for selected workspaces.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._workspaces:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for workspace in self._workspaces:
        # Read name from payload (just for logging):
        workspace_name = workspace.get("name")
        if not workspace_name:
            continue

        # Check if workspace has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not workspace.get("enabled", True):
            self.logger.info(
                "Payload for workspace -> '%s' is disabled. Skipping...",
                workspace_name,
            )
            continue

        # Read Aviator setting from payload:
        if not workspace.get("enable_aviator", False):
            self.logger.info(
                "Aviator is not enabled for workspace -> '%s'. Skipping to next workspace...",
                workspace_name,
            )
            continue

        # We cannot just lookup with workspace.get("nodeId") as the customizer
        # may have been restarted inbetween - so we use our proper determine_workspace_id
        # here...
        workspace_id = self.determine_workspace_id(workspace=workspace)
        if not workspace_id:
            self.logger.error(
                "Cannot find node ID for workspace -> '%s'. Workspace creation may have failed. Skipping to next workspace...",
                workspace_name,
            )
            success = False
            continue

        # Make code idem-potent and check if Aviator is already enabled
        # for this workspace:
        if self._otcs.check_workspace_aviator(workspace_id=workspace_id):
            self.logger.info(
                "Skip workspace -> '%s' (%s) as Aviator is already enabled...",
                workspace_name,
                workspace_id,
            )
            continue

        # Now enable the Content Aviator for the workspace:
        response = self._otcs.update_workspace_aviator(
            workspace_id=workspace_id,
            status=True,
        )
        if not response:
            self.logger.error(
                "Failed to enable Content Aviator for workspace -> '%s' (%s)!",
                workspace_name,
                workspace_id,
            )
            success = False
            continue
        # Also embed the workspace metadata:
        if not self._otcs.aviator_embed_metadata(
            node_id=workspace_id,
            workspace_metadata=True,
            wait_for_completion=False,
        ):
            success = False
            continue
    # end for workspace in self._workspaces

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._workspaces,
    )

    return success

process_workspace_member_permissions(section_name='workspaceMemberPermissions')

Process workspaces members in payload and set their permissions.

We need this separate from process_workspace_members() which also sets permissions (if in payload) as we add documents to workspaces with content transports and these documents don't inherit role permissions (this is a transport limitation)

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'workspaceMemberPermissions'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_member_permissions")
def process_workspace_member_permissions(
    self,
    section_name: str = "workspaceMemberPermissions",
) -> bool:
    """Process workspaces members in payload and set their permissions.

    We need this separate from process_workspace_members() which also
    sets permissions (if in payload) as we add documents to workspaces with
    content transports and these documents don't inherit role permissions
    (this is a transport limitation)

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._workspaces:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for workspace in self._workspaces:
        # Read name from payload (just for logging):
        workspace_name = workspace.get("name")
        if not workspace_name:
            continue

        # Check if workspace has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not workspace.get("enabled", True):
            self.logger.info(
                "Payload for workspace -> '%s' is disabled. Skipping...",
                workspace_name,
            )
            continue

        # Read members from payload:
        members = workspace.get("members")
        if not members:
            self.logger.info(
                "Workspace -> '%s' has no members in payload. No need to update permissions. Skipping to next workspace...",
                workspace_name,
            )
            continue

        workspace_id = workspace["id"]
        workspace_node_id = int(self.determine_workspace_id(workspace=workspace))
        if not workspace_node_id:
            self.logger.warning(
                "Workspace without node ID cannot cannot get permission changes (workspaces creation may have failed). Skipping to next workspace...",
            )
            continue

        workspace_roles = self._otcs.get_workspace_roles(
            workspace_id=workspace_node_id,
        )
        if workspace_roles is None:
            self.logger.info(
                "Workspace with payload ID -> %s and node Id -> %s has no roles to update permissions. Skipping to next workspace...",
                workspace_id,
                workspace_node_id,
            )
            continue

        for member in members:
            # read user list and role name from payload:
            member_users = (
                member["users"] if member.get("users") else []
            )  # be careful to avoid key errors as users are optional
            member_groups = (
                member["groups"] if member.get("groups") else []
            )  # be careful to avoid key errors as groups are optional
            member_role_name = member["role"]

            if member_role_name == "":  # role name is required
                self.logger.error(
                    "Members of workspace -> '%s' is missing the role name.",
                    workspace_name,
                )
                success = False
                continue
            if member_users == [] and member_groups == []:  # we either need users or groups (or both)
                self.logger.debug(
                    "Role -> '%s' of workspace -> '%s' does not have any members (no users nor groups).",
                    member_role_name,
                    workspace_name,
                )
                continue

            role_id = self._otcs.lookup_result_value(
                response=workspace_roles,
                key="name",
                value=member_role_name,
                return_key="id",
            )
            if role_id is None:
                self.logger.error(
                    "Workspace -> '%s' does not have a role -> '%s'",
                    workspace_name,
                    member_role_name,
                )
                success = False
                continue
            self.logger.debug(
                "Role -> '%s' has ID -> %s",
                member_role_name,
                role_id,
            )

            member_permissions = member.get("permissions", [])
            if member_permissions == []:
                self.logger.debug(
                    "No permission change for workspace -> '%s' and role -> '%s'.",
                    workspace_name,
                    member_role_name,
                )
                continue

            self.logger.info(
                "Update permissions of workspace -> '%s' (%s) and role -> '%s' to -> %s...",
                workspace_name,
                str(workspace_node_id),
                member_role_name,
                str(member_permissions),
            )
            response = self._otcs.assign_permission(
                node_id=workspace_node_id,
                assignee_type="custom",
                assignee=role_id,
                permissions=member_permissions,
                apply_to=2,  # inherit to all sub-folders
            )
            if not response:
                self.logger.error(
                    "Failed to update permissions of workspace -> '%s' (%s) and role -> '%s' to -> %s!",
                    workspace_name,
                    str(workspace_node_id),
                    member_role_name,
                    str(member_permissions),
                )
                success = False

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._workspaces,
    )

    return success

process_workspace_members(section_name='workspaceMembers')

Process workspaces members in payload and create them in Content Server.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'workspaceMembers'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_members")
def process_workspace_members(self, section_name: str = "workspaceMembers") -> bool:
    """Process workspaces members in payload and create them in Content Server.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._workspaces:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for workspace in self._workspaces:
        # Read name from payload (just for logging):
        if "name" not in workspace:
            continue
        workspace_name = workspace["name"]

        # Check if workspace has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not workspace.get("enabled", True):
            self.logger.info(
                "Payload for workspace -> '%s' is disabled. Skipping...",
                workspace_name,
            )
            continue

        # Read members from payload:
        members = workspace.get("members")
        if not members:
            self.logger.info(
                "Workspace -> '%s' has no members in payload. Skipping to next workspace...",
                workspace_name,
            )
            continue

        workspace_id = workspace["id"]

        workspace_node_id = int(self.determine_workspace_id(workspace=workspace))
        if not workspace_node_id:
            self.logger.warning(
                "Workspace without node ID cannot have members (workspaces creation may have failed). Skipping to next workspace...",
            )
            continue

        self.logger.info(
            "Workspace -> '%s' (%s) has memberships in payload - establishing...", workspace_name, workspace_node_id
        )

        # now determine the actual node IDs of the workspaces (have been created by process_workspaces()):
        workspace_node = self._otcs.get_node(node_id=workspace_node_id)
        workspace_owner_id = self._otcs.get_result_value(
            response=workspace_node,
            key="owner_user_id",
        )
        workspace_owner_name = self._otcs.get_result_value(
            response=workspace_node,
            key="owner",
        )

        workspace_roles = self._otcs.get_workspace_roles(
            workspace_id=workspace_node_id,
        )
        if workspace_roles is None:
            self.logger.debug(
                "Workspace -> '%s' (%s) has no roles. Skipping to next workspace...",
                workspace_name,
                workspace_node_id,
            )
            continue

        # We don't want the workspace creator to be in the leader role
        # of automatically created workspaces - this can happen because the
        # creator gets added to the leader role automatically if
        # the workspace type advanved configuration setting
        # "Add the creator of a business workspace to the Lead role" is
        # enabled:
        roles_iterator = self._otcs.get_result_values_iterator(response=workspace_roles)
        for role in roles_iterator:
            # We can have two leader roles if in a sub-workspaces a leader
            # roles is inherited from the parent workspace. As we want
            # don't want to consider leader role of the parent workspace
            # we check that 'inherited_from_id' is not set:
            if role["leader"] and role["inherited_from_id"] is None:
                leader_role_id = role["id"]
                break
        else:
            leader_role_id = None

        if leader_role_id:
            leader_role_name = self._otcs.lookup_result_value(
                response=workspace_roles,
                key="leader",
                value=True,
                return_key="name",
            )
            response = self._otcs.remove_workspace_member(
                workspace_id=workspace_node_id,
                role_id=leader_role_id,
                member_id=workspace_owner_id,
                show_warning=False,
            )
            if response:
                self.logger.info(
                    "Removed creator user -> '%s' (%s) from leader role -> '%s' (%s) of workspace -> '%s' (%s).",
                    workspace_owner_name,
                    workspace_owner_id,
                    leader_role_name,
                    leader_role_id,
                    workspace_name,
                    workspace_node_id,
                )
            else:
                self.logger.info(
                    "Creator user -> '%s' (%s) is not in leader role -> '%s' (%s) of workspace -> '%s' (%s). No need to remove it.",
                    workspace_owner_name,
                    workspace_owner_id,
                    leader_role_name,
                    leader_role_id,
                    workspace_name,
                    workspace_node_id,
                )

        self.logger.info(
            "Adding members to workspace -> '%s' (%s) defined in payload...",
            workspace_name,
            workspace_node_id,
        )

        for member in members:
            # read user list and role name from payload:
            member_users = member.get("users", [])
            member_groups = member.get("groups", [])
            member_role_name = member.get("role", "")
            member_clear = member.get("clear", False)

            if member_role_name == "":  # role name is required
                self.logger.error(
                    "Members of workspace -> '%s' is missing the role name in the payload.",
                    workspace_name,
                )
                success = False
                continue

            role_id = self._otcs.lookup_result_value(
                response=workspace_roles,
                key="name",
                value=member_role_name,
                return_key="id",
            )
            if role_id is None:
                #    if member_role is None:
                self.logger.error(
                    "Workspace -> '%s' does not have a role -> '%s'",
                    workspace_name,
                    member_role_name,
                )
                success = False
                continue
            inherited_role_id = self._otcs.lookup_result_value(
                response=workspace_roles,
                key="name",
                value=member_role_name,
                return_key="inherited_from_id",
            )
            if inherited_role_id is not None:
                self.logger.error(
                    "The role -> '%s' (%s) of workspace -> '%s' (%s) is inherited from role with ID -> %d and members cannot be set in this sub-workspace.",
                    member_role_name,
                    role_id,
                    workspace_name,
                    workspace_node_id,
                    inherited_role_id,
                )
                success = False
                continue
            self.logger.debug(
                "Role -> '%s' has ID -> %s",
                member_role_name,
                role_id,
            )

            # check if we want to clear (remove) existing members of this role:
            if member_clear:
                self.logger.info(
                    "Clear existing members of role -> '%s' (%s) for workspace -> '%s' (%s)...",
                    member_role_name,
                    role_id,
                    workspace_name,
                    workspace_id,
                )
                self._otcs.remove_workspace_members(
                    workspace_id=workspace_node_id,
                    role_id=role_id,
                )

            if member_users == [] and member_groups == []:  # we either need users or groups (or both)
                self.logger.debug(
                    "Role -> '%s' of workspace -> '%s' does not have any members (no users nor groups).",
                    member_role_name,
                    workspace_name,
                )
                continue

            # Process users as workspaces members:
            for member_user in member_users:
                # find member user in current payload:
                member_user_id = next(
                    (item for item in self._users if item["name"] == member_user),
                    {"name": member_user},  # we construct a payload on the fly to make determine_user_id() work
                )
                user_id = self.determine_user_id(user=member_user_id)
                if not user_id:
                    self.logger.error(
                        "Cannot find member user with login -> '%s'. Skipping...",
                        member_user,
                    )
                    continue

                # Add member if it does not yet exists - suppress warning
                # message if user is already in role:
                response = self._otcs.add_workspace_member(
                    workspace_id=workspace_node_id,
                    role_id=int(role_id),
                    member_id=user_id,
                    show_warning=False,
                )
                if response is None:
                    self.logger.error(
                        "Failed to add user -> '%s' (%s) as member to role -> '%s' of workspace -> '%s' (%s)!",
                        member_user,
                        user_id,
                        member_role_name,
                        workspace_name,
                        workspace_node_id,
                    )
                    success = False
                else:
                    self.logger.info(
                        "Successfully added user -> '%s' (%s) as member to role -> '%s' of workspace -> '%s' (%s).",
                        member_user,
                        user_id,
                        member_role_name,
                        workspace_name,
                        workspace_node_id,
                    )

            # Process groups as workspaces members:
            for member_group in member_groups:
                member_group_id = next(
                    (item for item in self._groups if item["name"] == member_group),
                    None,
                )

                group_id = self.determine_group_id(group=member_group_id) if member_group_id else None
                if not group_id:
                    self.logger.error(
                        "Cannot find member group -> '%s'. Skipping...",
                        member_group,
                    )
                    success = False
                    continue

                response = self._otcs.add_workspace_member(
                    workspace_id=workspace_node_id,
                    role_id=int(role_id),
                    member_id=group_id,
                    show_warning=False,
                )
                if response is None:
                    self.logger.error(
                        "Failed to add group -> '%s' (%s) to role -> '%s' of workspace -> '%s'!",
                        member_group_id["name"],
                        group_id,
                        member_role_name,
                        workspace_name,
                    )
                    success = False
                else:
                    self.logger.info(
                        "Successfully added group -> '%s' (%s) to role -> '%s' of workspace -> '%s'.",
                        member_group_id["name"],
                        group_id,
                        member_role_name,
                        workspace_name,
                    )

            # Optionally the payload may have a permission list for the role
            # to change the default permission from the workspace template
            # to something more specific:
            member_permissions = member.get("permissions", [])
            if member_permissions == []:
                self.logger.debug(
                    "No permission change for workspace -> '%s' and role -> '%s'",
                    workspace_name,
                    member_role_name,
                )
                continue

            self.logger.info(
                "Update permissions of workspace -> '%s' (%s) and role -> '%s' to -> %s...",
                workspace_name,
                str(workspace_node_id),
                member_role_name,
                str(member_permissions),
            )
            response = self._otcs.assign_permission(
                node_id=workspace_node_id,
                assignee_type="custom",
                assignee=role_id,
                permissions=member_permissions,
                apply_to=2,
            )
            if not response:
                self.logger.error(
                    "Failed to update permissions of workspace -> '%s' (%s) and role -> '%s' to -> %s!",
                    workspace_name,
                    str(workspace_node_id),
                    member_role_name,
                    str(member_permissions),
                )
                success = False
        # end for member in members:
    # end for workspace in self._workspaces:

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._workspaces,
    )

    return success

process_workspace_permissions(section_name='workspacePermissions')

Process items specified in payload and upadate workspace permissions.

Parameters:

Name Type Description Default
workspace_permissions list

List of items to apply permissions to. Each list item in the payload is a dict with this structure: { workspace_type = "..." workspace_folder = "..." regex = True public_permissions = ["see", "see_content", ...] owner_permissions = [] owner_group_permissions = [] groups = [ { name = "..." permissions = [] } ] users = [ { name = "..." permissions = [] } ] roles = [ { name = "..." permissions = [] } ] apply_to = 2 }

required
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections like "permissionsPost"). This name is also used for the "success" status files written to the Admin Personal Workspace.

'workspacePermissions'

Returns:

Name Type Description
bool bool

True if payload has been processed without errors, False otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_permissions")
def process_workspace_permissions(
    self,
    section_name: str = "workspacePermissions",
) -> bool:
    """Process items specified in payload and upadate workspace permissions.

    Args:
        workspace_permissions (list):
            List of items to apply permissions to.
            Each list item in the payload is a dict with this structure:
            {
                workspace_type = "..."
                workspace_folder = "..."
                regex = True
                public_permissions = ["see", "see_content", ...]
                owner_permissions = []
                owner_group_permissions = []
                groups = [
                    {
                        name = "..."
                        permissions = []
                    }
                ]
                users = [
                    {
                        name = "..."
                        permissions = []
                    }
                ]
                roles = [
                    {
                        name = "..."
                        permissions = []
                    }
                ]
                apply_to = 2
            }
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections like "permissionsPost").
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True if payload has been processed without errors, False otherwise

    """

    if not self._workspace_permissions:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for workspace_permission in self._workspace_permissions:
        workspace_type_name = workspace_permission.get("workspace_type")
        if not workspace_type_name:
            self.logger.error(
                "Workspaces type to change permissions is not specified. Skipping...",
            )
            success = False
            continue

        # Check if workspace permission has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not workspace_permission.get("enabled", True):
            self.logger.info(
                "Payload for workspace permission for workspace type -> '%s' is disabled. Skipping...",
                workspace_type_name,
            )
            continue

        workspace_folder_name = workspace_permission.get("workspace_folder", None)
        regex = workspace_permission.get("regex", False)

        # Make item + sub-items (2) the default:
        apply_to = workspace_permission.get("apply_to", 2)

        # Find the workspace type with the name given in the payload:
        workspace_type_id = next(
            (item["id"] for item in self._workspace_types if item["name"] == workspace_type_name),
            None,
        )
        if workspace_type_id is None:
            self.logger.error(
                "Workspace type -> '%s' not found. Skipping...",
                workspace_type_name,
            )
            success = False
            continue

        # The workspace type name is used as a "starts with" search on the
        # workspace type name. This can cause issues, so we prefer to use the type ID
        # if we have it. Otherwise the REST API prefers the name over the type:
        workspace_instances = self._otcs.get_workspace_instances_iterator(
            type_name=workspace_type_name if not workspace_type_id else None,
            type_id=workspace_type_id,
        )
        for workspace_instance in workspace_instances:
            workspace = workspace_instance.get("data").get("properties")
            workspace_id = workspace["id"]
            workspace_name = workspace["name"]
            if workspace_folder_name:
                if not regex:
                    workspace_folder = self._otcs.get_node_by_parent_and_name(
                        parent_id=workspace_id,
                        name=workspace_folder_name,
                    )
                else:
                    workspace_folder = self._otcs.lookup_node_by_regex(
                        parent_node_id=workspace_id,
                        regex_list=[workspace_folder_name],
                    )
                workspace_folder_id = self._otcs.get_result_value(
                    response=workspace_folder,
                    key="id",
                )
                if not workspace_folder_id:
                    self.logger.info(
                        "Workspace folder name -> '%s' was not found in workspace -> '%s' (%s). Skipping this workspace...",
                        workspace_folder_name,
                        workspace_name,
                        workspace_id,
                    )
                    continue
            else:
                # if no folder is specified we apply the permission on the workspace
                workspace_folder_id = workspace_id
                workspace_folder_name = workspace_name

            # Process a single workspace permission payload item:
            if not self.process_permission(
                node_id=workspace_folder_id,
                node_name=workspace_folder_name,
                permission=workspace_permission,
                apply_to=apply_to,
            ):
                success = False
                continue
        # end for workspace_instance in workspace_instances:

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._workspace_permissions,
    )

    return success

process_workspace_relationship(workspace)

Worker thread for workspace relationship creation.

Parameters:

Name Type Description Default
workspace dict

Dictionary with payload of a single workspace.

required

Returns:

Name Type Description
bool bool

True = Success, False = Failure

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_relationship")
def process_workspace_relationship(self, workspace: dict) -> bool:
    """Worker thread for workspace relationship creation.

    Args:
        workspace (dict):
            Dictionary with payload of a single workspace.

    Returns:
        bool:
            True = Success, False = Failure

    """

    # Read name from payload. If it does not exist then bail out:
    name = workspace.get("name")
    if not name:
        return False

    # Check if element has been explicitly disabled in payload
    # (enabled = false). In this case we skip the element:
    if not workspace.get("enabled", True):
        self.logger.info(
            "Payload for workspace -> '%s' is disabled. Skipping...",
            name,
        )
        return True

    # Read relationships from payload:
    if "relationships" not in workspace:
        self.logger.debug(
            "Workspace -> '%s' has no relationships. Skipping to next workspace...",
            name,
        )
        return True

    # Check that workspaces actually have a logical ID -
    # otherwise we cannot establish the relationship:
    workspace_id = workspace.get("id")
    if not workspace_id:
        self.logger.warning(
            "Workspace without logical ID in payload cannot have a relationship. Skipping to next workspace...",
        )
        return False

    self.logger.info(
        "Workspace -> '%s' (type -> '%s') has relationships - creating...",
        name,
        workspace["type_name"],
    )

    # now determine the actual node ID of the workspace (which should have been created before):
    workspace_node_id = int(self.determine_workspace_id(workspace=workspace))
    if not workspace_node_id:
        self.logger.warning(
            "Workspace -> '%s' (type -> '%s') has no node ID and cannot have a relationship (workspace creation may have failed or final name is different from payload). Skipping to next workspace...",
            name,
            workspace["type_name"],
        )
        return False

    self.logger.debug(
        "Workspace with logical ID -> %s has node ID -> %s",
        str(workspace_id),
        str(workspace_node_id),
    )

    success: bool = True

    for related_workspace in workspace["relationships"]:
        # Initialize variable to determine if we found a related workspace:
        related_workspace_node_id = None
        found_by = ""

        if isinstance(related_workspace, (str, int)):
            #
            # 1. Option: Find the related workspace with the logical ID given in the payload:
            #
            related_workspace_payload = next(
                (item for item in self._workspaces if str(item["id"]) == str(related_workspace)),
                None,
            )
            if related_workspace_payload:
                if not related_workspace_payload.get("enabled", True):
                    self.logger.info(
                        "Payload for related workspace -> '%s' is disabled. Skipping...",
                        related_workspace_payload["name"],
                    )
                    continue

                related_workspace_node_id = self.determine_workspace_id(
                    workspace=related_workspace_payload,
                )
                if not related_workspace_node_id:
                    self.logger.warning(
                        "Related workspace -> '%s' (type -> '%s') has no node ID (workspaces creation may have failed or name is different from payload). Skipping to next workspace...",
                        related_workspace_payload["name"],
                        related_workspace_payload["type_name"],
                    )
                    continue
                found_by = "logical ID -> '{}' in payload".format(related_workspace)
            # end if related_workspace_payload:

            #
            # 2. Option: Find the related workspace with nickname:
            #
            else:
                # See if a nickname exists the the provided related_workspace:
                response = self._otcs.get_node_from_nickname(nickname=related_workspace)
                related_workspace_node_id = self._otcs.get_result_value(
                    response=response,
                    key="id",
                )
                if related_workspace_node_id:
                    found_by = "nickname -> '{}'".format(related_workspace)
        # end if isinstance(related_workspace_id, (str, int)):

        #
        # 3. Option: Find the related workspace type and name:
        #
        elif isinstance(related_workspace, dict):
            related_workspace_type = related_workspace.get("type", None)
            related_workspace_name = related_workspace.get("name", None)
            if related_workspace_type and related_workspace_name:
                response = self._otcs.get_workspace_by_type_and_name(
                    type_name=related_workspace_type, name=related_workspace_name
                )
                related_workspace_node_id = self._otcs.get_result_value(
                    response=response,
                    key="id",
                )
                if related_workspace_node_id:
                    found_by = "type -> '{}' and name -> '{}'".format(
                        related_workspace_type, related_workspace_name
                    )
        #
        # 4. Option: Find the related workspace volume and path:
        #
        elif isinstance(related_workspace, list):
            response = self._otcs.get_node_by_volume_and_path(
                volume_type=self._otcs.VOLUME_TYPE_ENTERPRISE_WORKSPACE, path=related_workspace
            )
            related_workspace_node_id = self._otcs.get_result_value(
                response=response,
                key="id",
            )
            if related_workspace_node_id:
                found_by = "path -> {}".format(related_workspace)

        if related_workspace_node_id is None:
            self.logger.error(
                "Related workspace -> %s not found.",
                related_workspace,
            )
            success = False
            continue

        self.logger.debug(
            "Related workspace with %s has node ID -> %s",
            found_by,
            related_workspace_node_id,
        )

        # Check if relationship does already exists:
        response = self._otcs.get_workspace_relationships(
            workspace_id=workspace_node_id,
        )

        existing_workspace_relationship = self._otcs.exist_result_item(
            response=response,
            key="id",
            value=related_workspace_node_id,
        )
        if existing_workspace_relationship:
            self.logger.info(
                "Workspace relationship between workspace ID -> %s and related workspace ID -> %s does already exist. Skipping...",
                str(workspace_node_id),
                related_workspace_node_id,
            )
            continue

        self.logger.info(
            "Create workspace relationship between workspace node ID -> %s and workspace node ID -> %s...",
            str(workspace_node_id),
            related_workspace_node_id,
        )

        response = self._otcs.create_workspace_relationship(
            workspace_id=workspace_node_id,
            related_workspace_id=related_workspace_node_id,
        )
        if not response:
            self.logger.error("Failed to create workspace relationship!")
            success = False
        else:
            self.logger.info("Successfully created workspace relationship.")

    # end for relationships

    return success

process_workspace_relationships(section_name='workspaceRelationships')

Process workspaces relationships in payload and create them in Content Server.

Relationships can only be created if all workspaces have been created before. Once a workspace got created, the node ID of that workspaces has been added to the payload["workspaces"] data structure (see process_workspaces()) Relationships are created between the node IDs of two business workspaces (and not the logical IDs in the inital payload specification)

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'workspaceRelationships'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_relationships")
def process_workspace_relationships(
    self,
    section_name: str = "workspaceRelationships",
) -> bool:
    """Process workspaces relationships in payload and create them in Content Server.

    Relationships can only be created if all workspaces have been created before.
    Once a workspace got created, the node ID of that workspaces has been added
    to the payload["workspaces"] data structure (see process_workspaces())
    Relationships are created between the node IDs of two business workspaces
    (and not the logical IDs in the inital payload specification)

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    if not self._workspaces:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    if ENABLE_MULTI_THREADING:
        # Create a list to hold the threads
        threads = []
        # And another list to collect the results
        results = []

        df = Data(self._workspaces, logger=self.logger)

        partitions: list = df.partitionate(number=THREAD_NUMBER)

        # Create and start a thread for each partition
        for index, partition in enumerate(partitions, start=1):
            # start a thread executing the process_workspace_relationships_worker() method below:
            thread = threading.Thread(
                name=f"{section_name}_{index:02}",
                target=self.thread_wrapper,
                args=(
                    self.process_workspace_relationships_worker,
                    partition,
                    results,
                ),
            )
            self.logger.info("Starting thread -> '%s'...", str(thread.name))
            thread.start()
            threads.append(thread)
            # Avoid that all threads start at the exact same time with
            # potentially expired cookies that cases race conditions:
            time.sleep(1)
        # end for index, partition in enumerate(partitions, start=1)

        # Wait for all threads to complete
        for thread in threads:
            self.logger.info(
                "Waiting for thread -> '%s' to complete...",
                str(thread.name),
            )
            thread.join()
            self.logger.info("Thread -> '%s' has completed.", str(thread.name))

        # Print statistics for each thread. In addition,
        # check if all threads have completed without error / failure.
        # If there's a single failure in on of the thread results we
        # set 'success' variable to False.
        results.sort(key=lambda x: x["thread_name"])
        for result in results:
            if not result["success"]:
                self.logger.info(
                    "Thread -> '%s' had %s failures and completed relationships for %s workspaces successfully.",
                    result["thread_name"],
                    result["failure_counter"],
                    result["success_counter"],
                )
                success = False  # mark the complete processing as "failure" for the status file.
            else:
                self.logger.info(
                    "Thread -> '%s' completed relationships for %s workspace successfully.",
                    result["thread_name"],
                    result["success_counter"],
                )
    else:  # no multi-threading
        for workspace in self._workspaces:
            result = self.process_workspace_relationship(workspace=workspace)
            success = (
                success and result
            )  # if a single result is False then the 'success' variable becomes 'False' as well.

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._workspaces,
    )

    return success

process_workspace_relationships_worker(partition, results)

Multi-threading worker method to process a partition of the workspaces to create workspace relationships.

Parameters:

Name Type Description Default
partition DataFrame

The partition of the workspace relationships to process by this thread.

required
results list

A mutable (shared) list of all workers to collect the results.

required
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_relationships_worker")
def process_workspace_relationships_worker(
    self,
    partition: pd.DataFrame,
    results: list,
) -> None:
    """Multi-threading worker method to process a partition of the workspaces to create workspace relationships.

    Args:
        partition (pd.DataFrame):
            The partition of the workspace relationships to process by this thread.
        results (list):
            A mutable (shared) list of all workers to collect the results.

    """

    thread_id = threading.get_ident()
    thread_name = threading.current_thread().name

    result = {}
    result["thread_id"] = thread_id
    result["thread_name"] = thread_name
    result["success"] = True
    result["failure_counter"] = 0
    result["success_counter"] = 0

    total_rows = len(partition)

    # Process all datasets in the partion that was given to the thread:
    for index, row in partition.iterrows():
        # Calculate percentage of completion
        percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

        self.logger.info(
            "Processing data row -> %s to create relationships for workspace -> '%s' (%.2f %% complete)...",
            str(index),
            row["name"],
            percent_complete,
        )
        success = self.process_workspace_relationship(
            workspace=row.dropna().to_dict(),
        )
        if success:
            result["success_counter"] += 1
        else:
            self.logger.error(
                "Failed to process row -> %s for relationships of workspace -> '%s'!",
                str(index),
                row["name"],
            )
            result["failure_counter"] += 1
            result["success"] = False

    results.append(result)

process_workspace_templates(section_name='workspaceTemplates')

Process workspace template playload.

This allows to define role members on template basis. This avoids having to "pollute" workspace templates with user or group information and instead controls this via payload.

This method also allows to assign (additional) categories to a workspace template controlled by payload.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'workspaceTemplates'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_templates")
def process_workspace_templates(
    self,
    section_name: str = "workspaceTemplates",
) -> bool:
    """Process workspace template playload.

    This allows to define role members on template basis.
    This avoids having to "pollute" workspace templates
    with user or group information and instead controls this via payload.

    This method also allows to assign (additional) categories to
    a workspace template controlled by payload.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    """

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        return True

    success: bool = True

    for workspace_template in self._workspace_templates:
        # Read Workspace Type Name from payload:
        type_name = workspace_template.get("type_name")
        if not type_name:
            self.logger.error(
                "Workspace template needs a type name! Skipping to next workspace template...",
            )
            success = False
            continue

        # Check if workspace template has been explicitly disabled in payload
        # (enabled = false). In this case we skip the element:
        if not workspace_template.get("enabled", True):
            self.logger.info(
                "Payload for workspace template -> '%s' is disabled. Skipping to next workspace template...",
                type_name,
            )
            continue

        # Read Workspace Template Name from payload:
        template_name = workspace_template.get("template_name")
        if not template_name:
            self.logger.error(
                "Workspace template for workspace type -> '%s' needs a template name! Skipping...",
                type_name,
            )
            success = False
            continue

        # Read members from payload:
        members = workspace_template.get("members", [])

        # Read categories from payload:
        categories = workspace_template.get("categories", [])

        # Find the workspace type with the name given in the _workspace_types
        # datastructure that has been generated by process_workspace_types() method before:
        workspace_type = next(
            (item for item in self._workspace_types if item["name"] == type_name),
            None,
        )
        if workspace_type is None:
            self.logger.error(
                "Workspace type -> '%s' not found. Skipping...",
                type_name,
            )
            success = False
            continue
        if workspace_type["templates"] == []:
            self.logger.error(
                "Workspace type -> '%s' does not have templates. Skipping...",
                type_name,
            )
            success = False
            continue

        workspace_template = next(
            (item for item in workspace_type["templates"] if item["name"] == template_name),
            None,
        )
        if workspace_template:  # does this template exist?
            self.logger.info(
                "Workspace template -> '%s' has been specified in payload and it does exist.",
                template_name,
            )
        else:
            self.logger.error(
                "Workspace template -> '%s' has been specified in payload but it doesn't exist!",
                template_name,
            )
            self.logger.error(
                "Workspace type -> '%s' has only these templates -> %s",
                type_name,
                workspace_type["templates"],
            )
            success = False
            continue

        template_id = workspace_template["id"]

        workspace_roles = self._otcs.get_workspace_roles(workspace_id=template_id)
        if workspace_roles is None:
            self.logger.info(
                "Workspace template -> '%s' with node Id -> %s has no roles. Skipping to next workspace...",
                template_name,
                template_id,
            )
            continue

        for member in members:
            # read user list, group list, and role name from payload:
            member_users = member.get("users", [])
            member_groups = member.get("groups", [])
            member_role_name = member.get("role", "")

            if not member_role_name:  # role name is required
                self.logger.error(
                    "Members of workspace template -> '%s' is missing the role name.",
                    template_name,
                )
                success = False
                continue
            if (
                member_users == [] and member_groups == []
            ):  # we either need users or groups (or both) in the payload
                self.logger.debug(
                    "Payload for workspace template -> '%s' and role -> '%s' does not have any members (no users nor groups).",
                    template_name,
                    member_role_name,
                )
                continue

            role_id = self._otcs.lookup_result_value(
                response=workspace_roles,
                key="name",
                value=member_role_name,
                return_key="id",
            )
            if role_id is None:
                #    if member_role is None:
                self.logger.error(
                    "Workspace template -> '%s' does not have a role -> '%s'",
                    template_name,
                    member_role_name,
                )
                success = False
                continue

            # Process users as workspace template members:
            for member_user in member_users:
                # find member user in current payload:
                member_user_id = next(
                    (item for item in self._users if item["name"] == member_user),
                    {"name": member_user},  # we construct a payload on the fly to make determine_user_id() work
                )
                user_id = self.determine_user_id(user=member_user_id) if member_user_id else None
                if not user_id:
                    self.logger.error(
                        "Cannot find member user with login -> '%s'. Skipping...",
                        member_user,
                    )
                    success = False
                    continue

                # Add member if it does not yet exists - suppress warning
                # message if user is already in role:
                response = self._otcs.add_workspace_member(
                    workspace_id=template_id,
                    role_id=int(role_id),
                    member_id=user_id,
                    show_warning=False,
                )
                if response is None:
                    self.logger.error(
                        "Failed to add user -> '%s' (%s) as member to role -> '%s' (%s) of workspace template -> '%s' (%s)!",
                        member_user,
                        user_id,
                        member_role_name,
                        role_id,
                        template_name,
                        template_id,
                    )
                    success = False
                else:
                    self.logger.info(
                        "Successfully added user -> '%s' (%s) as member to role -> '%s' (%s) of workspace template -> '%s' (%s).",
                        member_user,
                        user_id,
                        member_role_name,
                        role_id,
                        template_name,
                        template_id,
                    )
            # end for member_user in member_users:

            # Process groups as workspace template members:
            for member_group in member_groups:
                member_group_id = next(
                    (item for item in self._groups if item["name"] == member_group),
                    None,
                )

                group_id = self.determine_group_id(group=member_group_id) if member_group_id else None
                if not group_id:
                    self.logger.error(
                        "Cannot find member group -> '%s'. Skipping...",
                        member_group,
                    )
                    success = False
                    continue

                response = self._otcs.add_workspace_member(
                    workspace_id=template_id,
                    role_id=int(role_id),
                    member_id=group_id,
                    show_warning=False,
                )
                if response is None:
                    self.logger.error(
                        "Failed to add group -> '%s' (%s) as member to role -> '%s' (%s) of workspace template -> '%s' (%s)!",
                        member_group_id["name"],
                        group_id,
                        member_role_name,
                        role_id,
                        template_name,
                        template_id,
                    )
                    success = False
                else:
                    self.logger.info(
                        "Successfully added group -> '%s' (%s) as member to role -> '%s' (%s) of workspace template -> '%s' (%s).",
                        member_group_id["name"],
                        group_id,
                        member_role_name,
                        role_id,
                        template_name,
                        template_id,
                    )
            # end for member_group in member_groups:
        # end for member in members:

        existing_template_categories = None
        for category in categories:
            category_path = category.get("path")
            category_nickname = category.get("nickname")
            inheritance = category.get("inheritance")
            apply_to_sub_items = category.get("apply_to_sub_items")
            category_id = None

            if not category_nickname and not category_path:
                self.logger.error(
                    "Category assignment to workspace template needs eith the category nickname or the category path in the category volume!",
                )
                success = False
                continue
            if category_path:
                response = self._otcs.get_node_by_volume_and_path(
                    volume_type=self._otcs.VOLUME_TYPE_CATEGORIES_VOLUME,
                    path=category_path,
                )
                category_id = self._otcs.get_result_value(response=response, key="id")
            elif category_nickname:
                response = self._otcs.get_node_from_nickname(nickname=category_nickname, show_error=False)
                category_id = self._otcs.get_result_value(response=response, key="id")
            if not category_id:
                self.logger.error(
                    "Cannot find category for workspace template -> '%s' (%s). Tried category %s.",
                    template_name,
                    template_id,
                    "path {}".format(category_path) if category_path else "nickname {}".format(category_nickname),
                )
                success = False
                continue
            if existing_template_categories is None:
                existing_template_categories = self._otcs.get_node_category_ids(node_id=template_id)
            if category_id not in existing_template_categories:
                result = self._otcs.assign_category(
                    node_id=template_id,
                    category_id=category_id,
                    inheritance=inheritance,
                    apply_to_sub_items=apply_to_sub_items,
                )
                if not result:
                    self.logger.error(
                        "Cannot assign category with ID -> %s to workspace template -> '%s' (%s)!",
                        category_id,
                        template_name,
                        template_id,
                    )
                    success = False
                    continue
                self.logger.info(
                    "Successfully assigned category with ID -> %s to workspace template -> '%s' (%s).",
                    category_id,
                    template_name,
                    template_id,
                )
                # Write ID back into the payload dictionary:
                category["id"] = category_id
            else:
                self.logger.info(
                    "Category with ID -> %s is already assigned to workspace template -> '%s' (%s).",
                    category_id,
                    template_name,
                    template_id,
                )

        # end for category in categories:
    # end for workspace_template in self._workspace_templates:

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._workspace_templates,
    )

    return success

process_workspace_types(section_name='workspaceTypes')

Create a data structure for all workspace types in the OTCS.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'workspaceTypes'

Returns:

Name Type Description
list list

A list of workspace types. Each list element is a dict with these values: - id (str) - name (str) - templates (list) + name (str) + id

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspace_types")
def process_workspace_types(self, section_name: str = "workspaceTypes") -> list:
    """Create a data structure for all workspace types in the OTCS.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        list:
            A list of workspace types. Each list element is a dict with these values:
            - id (str)
            - name (str)
            - templates (list)
                + name (str)
                + id

    """

    # If this payload section has been processed successfully before we
    # still need to read the data structure from the status file and
    # initialize self._workspace_types:
    if self.check_status_file(payload_section_name=section_name):
        # read the list from the json file in admin Home
        # this is important for restart of customizer pod
        # as this data structure is used later on for workspace processing
        self.logger.info(
            "Re-Initialize workspace types list from status file -> '%s' for later use...",
            self.get_status_file_name(payload_section_name=section_name),
        )
        self._workspace_types = self.get_status_file(
            payload_section_name=section_name,
        )
        if self._workspace_types:
            self.logger.info(
                "Found -> %s workspace types.",
                str(len(self._workspace_types)),
            )
            self.logger.debug("Workspace types -> %s", str(self._workspace_types))
            return self._workspace_types
        else:
            self.logger.error(
                "Couldn't read workspace types from status file -> '%s'. Regenerate list...",
                self.get_status_file_name(payload_section_name=section_name),
            )

    # Read payload_section "workspaceTypes" if available
    payload_section = {}
    for section in self._payload_sections:
        if section["name"] == "workspaceTypes":
            payload_section = section

    # get all workspace types (these have been created by the transports and are not in the payload!)
    # we need to do this each time as it needs to work across multiple payload files...
    response = self._otcs.get_workspace_types()
    if response is None:
        self.logger.error("No workspace types found!")
        self._workspace_types = []
    else:
        self._workspace_types = response["results"]
        self.logger.info(
            "Found -> %s workspace types.",
            str(len(self._workspace_types)),
        )
        self.logger.debug("Workspace types -> %s", str(self._workspace_types))

    # now we enrich the workspace_type list elments (which are dicts)
    # with additional dict elements for further processing:
    for workspace_type in self._workspace_types:
        workspace_type_id = workspace_type["data"]["properties"]["wksp_type_id"]
        self.logger.debug("Workspace Type ID -> %s", workspace_type_id)
        workspace_type["id"] = workspace_type_id
        workspace_type_name = workspace_type["data"]["properties"]["wksp_type_name"]
        workspace_type["name"] = workspace_type_name
        workspace_templates = workspace_type["data"]["properties"]["templates"]
        if not workspace_templates:
            self.logger.warning(
                "Workspace type -> '%s' has no workspace templates to process! Skipping...",
                workspace_type_name,
            )
            continue
        self.logger.info(
            "Workspace type -> '%s' has %d template%s to process...",
            workspace_type_name,
            len(workspace_templates),
            "s" if len(workspace_templates) > 1 else "",
        )

        # Create empty lists of dicts with template names and node IDs:
        workspace_type["templates"] = []
        # Determine available templates per workspace type (there can be multiple!)
        # and record them in a simplified data structure:
        for workspace_template in workspace_templates:
            workspace_template_id = workspace_template["id"]
            workspace_template_name = workspace_template["name"]
            self.logger.info(
                "Found workspace template -> '%s' (%s).",
                workspace_template_name,
                workspace_template_id,
            )
            template = {
                "name": workspace_template_name,
                "id": workspace_template_id,
            }
            workspace_type["templates"].append(template)

            if payload_section.get("inherit_permissions", False):
                # TODO: Workaround for problem with workspace role inheritance
                # which may be related to Transport or REST API: to work-around this we
                # push down the workspace roles to the workspace folders explicitly:
                response = self._otcs.get_workspace_roles(
                    workspace_id=workspace_template_id,
                )
                for roles in response["results"]:
                    role_name = roles["data"]["properties"]["name"]
                    role_id = roles["data"]["properties"]["id"]
                    permissions = roles["data"]["properties"]["perms"]
                    # as get_workspace_roles() delivers permissions as a value (bit encoded)
                    # we need to convert it to a permissions string list:
                    permission_string_list = self._otcs.convert_permission_value_to_permission_string(
                        permission_value=permissions,
                    )

                    self.logger.info(
                        "Inherit permissions of workspace template -> '%s' (%s) and role -> '%s' (%s) to workspace folders...",
                        workspace_template_name,
                        workspace_template_id,
                        role_name,
                        role_id,
                    )

                    # Inherit permissions to folders of workspace template:
                    response = self._otcs.assign_workspace_permissions(
                        workspace_id=workspace_template_id,
                        role_id=role_id,
                        permissions=permission_string_list,
                        apply_to=1,  # 1 = only sub items - workspace node itself is OK
                    )
                # end for roles in response["results"]:
            # end if payload_section.get("inherit_permissions", False):
        # end for workspace_template in workspace_templates:
    # end for workspace_type in self._workspace_types:

    self.write_status_file(success=True, payload_section_name=section_name, payload_section=self._workspace_types)

    return self._workspace_types

process_workspaces(section_name='workspaces')

Process workspaces in payload and create them in Content Server.

Parameters:

Name Type Description Default
section_name str

The name of the payload section. It can be overridden for cases where multiple sections of same type are used (e.g. the "Post" sections). This name is also used for the "success" status files written to the Admin Personal Workspace.

'workspaces'

Returns:

Name Type Description
bool bool

True, if payload has been processed without errors, False otherwise.

Side Effects

Set workspace["nodeId"] to the node ID of the created workspace and update the workspace["name"] to the final name of the workspaces (which may be different from the ones in the payload depending on workspace type configutrations)

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspaces")
def process_workspaces(self, section_name: str = "workspaces") -> bool:
    """Process workspaces in payload and create them in Content Server.

    Args:
        section_name (str, optional):
            The name of the payload section. It can be overridden
            for cases where multiple sections of same type
            are used (e.g. the "Post" sections).
            This name is also used for the "success" status
            files written to the Admin Personal Workspace.

    Returns:
        bool:
            True, if payload has been processed without errors, False otherwise.

    Side Effects:
        Set workspace["nodeId"] to the node ID of the created workspace and update
        the workspace["name"] to the final name of the workspaces (which may be different
        from the ones in the payload depending on workspace type configutrations)

    """

    if not self._workspaces:
        self.logger.info(
            "Payload section -> '%s' is empty. Skipping...",
            section_name,
        )
        return True

    # If this payload section has been processed successfully before we
    # can return True and skip processing it once more:
    if self.check_status_file(payload_section_name=section_name):
        # Read the list of created workspaces from the json file in admin Home
        # This is important in case of restart / rerun of customizer pod
        # as this data structure is used later on for workspace relationship
        # processing (and other) and the workspaces dictionaries have been
        # updated with "nodeId" and "name" (the final names of the workspaces
        # that can be different from the names in the payload)
        self.logger.info(
            "Re-Initialize workspace list from status file -> '%s' to have final names and node IDs...",
            self.get_status_file_name(payload_section_name=section_name),
        )
        self._workspaces = self.get_status_file(payload_section_name=section_name)

        return True

    success: bool = True

    if ENABLE_MULTI_THREADING:
        # Create a list to hold the threads
        threads = []
        # And another list to collect the results
        results = []

        df = Data(self._workspaces, logger=self.logger)

        # Add empty column for "nodeId" so that the worker threads can properly fill it:
        df.get_data_frame()["nodeId"] = None

        self.logger.info(
            "Created a data frame with -> %s rows from the workspaces list with -> %s elements.",
            str(len(df)),
            str(len(self._workspaces)),
        )
        df.print_info()

        partitions = df.partitionate(THREAD_NUMBER)

        # Create and start a thread for each partition
        for index, partition in enumerate(partitions, start=1):
            # start a thread executing the process_workspaces_worker() method below:
            thread = threading.Thread(
                name=f"{section_name}_{index:02}",
                target=self.thread_wrapper,
                args=(self.process_workspaces_worker, partition, results),
            )
            self.logger.info("Starting thread -> '%s'...", str(thread.name))
            thread.start()
            threads.append(thread)
            # Avoid that all threads start at the exact same time with
            # potentially expired cookies that cases race conditions:
            time.sleep(1)
        # end for index, partition in enumerate(partitions, start=1)

        # Wait for all threads to complete
        for thread in threads:
            self.logger.info(
                "Waiting for thread -> '%s' to complete...",
                str(thread.name),
            )
            thread.join()
            self.logger.info("Thread -> '%s' has completed.", str(thread.name))

        # As we have basically created a copy of self._workspaces into the Pandas
        # data frame (see df = Data(...) above) and the workspace processing
        # updates the workspaces data with "nodeID" and the final
        # workspace names, we need to write the Pandas Data frame
        # back into the self._workspaces data structure for further processing
        # e.g. in the process_workspace_relationships. Otherwise the
        # changes to "nodeId" or "name" would be lost. We need to do it
        # in 2 steps as we want to avoid to have NaN values in the resulting dicts:
        # 1. Convert the data frame back to a list of dictionaries:
        updated_workspaces = df.get_data_frame().to_dict(orient="records")
        # 2. Remove any dictionary item that has a "NaN" scalar value
        # (pd.notna() only works on scalar values, not on lists!):
        self._workspaces = [
            #                {k: v for k, v in w.items() if pd.notna(v)} for w in updated_workspaces
            {
                key: value
                for key, value in updated_workspace.items()
                if not pd.api.types.is_scalar(value) or pd.notna(value)
            }
            for updated_workspace in updated_workspaces
        ]

        # Print statistics for each thread. In addition,
        # check if all threads have completed without error / failure.
        # If there's a single failure in on of the thread results we
        # set 'success' variable to False.
        results.sort(key=lambda x: x["thread_name"])
        for result in results:
            if not result["success"]:
                self.logger.info(
                    "Thread -> '%s' had %s failures and completed %s workspaces successfully.",
                    result["thread_name"],
                    result["failure_counter"],
                    result["success_counter"],
                )
                success = False  # mark the complete processing as "failure" for the status file.
            else:
                self.logger.info(
                    "Thread -> '%s' completed %s workspaces successfully.",
                    result["thread_name"],
                    result["success_counter"],
                )
    else:  # no multi-threading
        for workspace in self._workspaces:
            try:
                result = self.process_workspace(workspace=workspace)
            except Exception:
                self.logger.exception("Failed process workspace -> %s", workspace)
                result = False
            success = success and result  # if a single result is False then mark this in 'success' variable.

    self.write_status_file(
        success=success,
        payload_section_name=section_name,
        payload_section=self._workspaces,
    )

    return success

process_workspaces_worker(partition, results)

Multi-threading worker method to process a partition of the workspaces.

Parameters:

Name Type Description Default
partition DataFrame

The partition of the workspaces to process by this thread.

required
results list

A mutable (shared) list of all workers to collect the results.

required
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="process_workspaces_worker")
def process_workspaces_worker(
    self,
    partition: pd.DataFrame,
    results: list,
) -> None:
    """Multi-threading worker method to process a partition of the workspaces.

    Args:
        partition (pd.DataFrame):
            The partition of the workspaces to process by this thread.
        results (list):
            A mutable (shared) list of all workers to collect the results.

    """

    thread_id = threading.get_ident()
    thread_name = threading.current_thread().name

    t = trace.get_current_span()
    t.set_attributes(
        {
            "thread.id": thread_id,
            "thread.name": thread_name,
        }
    )

    result = {}
    result["thread_id"] = thread_id
    result["thread_name"] = thread_name
    result["success"] = True
    result["failure_counter"] = 0
    result["success_counter"] = 0

    total_rows = len(partition)

    # Process all datasets in the partion that was given to the thread:
    for index, row in partition.iterrows():
        # Calculate percentage of completion
        percent_complete = ((partition.index.get_loc(index) + 1) / total_rows) * 100

        self.logger.info(
            "Processing data row -> %s to create workspace -> '%s' (%.2f %% complete)...",
            str(index),
            row["name"],
            percent_complete,
        )
        # Convert the row to a dictionary - omitting any empty column:
        workspace = row.dropna().to_dict()
        # workspace is a mutable dictionary that may be updated
        # by process_workspace():
        try:
            success = self.process_workspace(workspace=workspace)
        except Exception:
            self.logger.exception("Failed process workspace -> %s", workspace)
            success = False
        # We need to make sure the row (and the whole data frame)
        # gets these updates back (and adds new columns such as "nodeId"):
        for key, value in workspace.items():
            row[key] = value  # This will update existing keys and add new ones
        self.logger.debug("Final values of row %s -> %s", str(index), str(row))

        # As iterrows() creates a copy of the data we need to
        # write the changes back into the partition
        partition.loc[index] = row

        if success:
            result["success_counter"] += 1
        else:
            self.logger.error(
                "Failed to process row -> %s for workspace -> '%s'!",
                str(index),
                row["name"],
            )
            result["failure_counter"] += 1
            result["success"] = False

    results.append(result)

read_data_source_file(data_source, compression=True)

Read a data source file from the Admin Personal Workspace in Content Server.

Parameters:

Name Type Description Default
data_source dict

Data source dictionary with embedded "data" Data object.

required
compression bool

Use True, if the data source file is compressed.

True

Returns:

Name Type Description
bool bool

True if the data source file (JSON) as been loaded from Content Server successfully, False otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="read_data_source_file")
def read_data_source_file(
    self,
    data_source: dict,
    compression: bool = True,
) -> bool:
    """Read a data source file from the Admin Personal Workspace in Content Server.

    Args:
        data_source (dict):
            Data source dictionary with embedded "data" Data object.
        compression (bool, optional):
            Use True, if the data source file is compressed.

    Returns:
        bool:
            True if the data source file (JSON) as been loaded from
            Content Server successfully, False otherwise

    """

    data_source_name = data_source.get("name", "")
    if not data_source_name:
        self.logger.error("Missing data source name!")
        return False

    response = self._otcs.get_node_by_volume_and_path(
        volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
    )  # write to Personal Workspace of Admin (with Volume Type ID = 142)
    target_folder_id = self._otcs.get_result_value(response=response, key="id")
    if not target_folder_id:
        target_folder_id = 2004  # use Personal Workspace of Admin as fallback

    if self._payload_source:
        payload_file_name = os.path.basename(
            self._payload_source,
        )  # remove directories
        # Split once at the first occurance of a dot
        # as the _payload_source may have multiple suffixes
        # such as .yml.gz.b64:
        payload_file_name = payload_file_name.split(".", 1)[0] + "_"
    else:
        payload_file_name = ""

    file_name = "data_source_" + payload_file_name + data_source_name + ".json"
    if compression:
        file_name += ".zip"
    full_path = os.path.join(tempfile.gettempdir(), file_name)

    # Check if the data source file has been uploaded before.
    # This can happen if we re-run the python container.
    # In this case we add a version to the existing document:
    response = self._otcs.get_node_by_parent_and_name(
        parent_id=int(target_folder_id),
        name=file_name,
        show_error=False,
    )
    data_source_node_id = self._otcs.get_result_value(response=response, key="id")
    if not data_source_node_id:
        self.logger.warning(
            "Data source -> '%s' file -> '%s' not found!",
            data_source_name,
            file_name,
        )
        return False

    self.logger.info(
        "Download data source to temporary file -> '%s'...",
        full_path,
    )
    if not self._otcs.download_document(
        node_id=int(data_source_node_id),
        file_path=full_path,
    ):
        self.logger.error(
            "Error downloading data source -> '%s' to file -> '%s'!",
            data_source_name,
            full_path,
        )
        return False

    self.logger.info("Load data source file -> '%s' into data frame...", full_path)
    data = Data()
    if not data.load_json_data(
        json_path=full_path,
        index_column="row",
        compression="zip" if compression else None,
    ):
        self.logger.error(
            "Data source -> '%s' could not be loaded!",
            data_source_name,
        )
        return False

    self.logger.info(
        "Data source file -> '%s' has been loaded from Personal Workspace of admin user into the data frame.",
        file_name,
    )

    data_source["data"] = data

    # Don't leave stuff behind:
    os.remove(full_path)

    return True

replace_bulk_placeholders(input_string, row, index=0, replacements=None, additional_regex_list=None)

Replace placeholders like "{variable.subvalue}" in payload of bulk processing.

Parameters:

Name Type Description Default
input_string str

The string to replace placeholders in.

required
row Series

Curent row in the data frame.

required
index int | None

The index for use if we encounter a list value. If index is "None" then we return the complete list as value. Otherwise we return the list item with the given index (0 = first element is the default). IMPORTANT: None and 0 have different effects!

0
replacements dict

The replacements to apply to given fields (dictionary key = field name)

None
additional_regex_list list

These are not coming from the payload but dynamically added for special needs like determining the nicknames.

None

Returns:

Name Type Description
str str

Updated string with replacements.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="replace_bulk_placeholders")
def replace_bulk_placeholders(
    self,
    input_string: str,
    row: pd.Series,
    index: int | None = 0,  # don't use None as the default here!
    replacements: dict | None = None,
    additional_regex_list: list | None = None,
) -> str:
    """Replace placeholders like "{variable.subvalue}" in payload of bulk processing.

    Args:
        input_string (str):
            The string to replace placeholders in.
        row (pd.Series):
            Curent row in the data frame.
        index (int | None, optional):
            The index for use if we encounter a list value.
            If index is "None" then we return the complete list as value.
            Otherwise we return the list item with the given index (0 = first element is the default).
            IMPORTANT: None and 0 have different effects!
        replacements (dict, optional):
            The replacements to apply to given fields (dictionary key = field name)
        additional_regex_list (list, optional):
            These are not coming from the payload but dynamically
            added for special needs like determining the nicknames.

    Returns:
        str:
            Updated string with replacements.

    """

    r"""
    XML data sources may include "@" in Pandas column names as well!
    This happens if the XML elements have attributes.
           pattern = r"\{([\w@]+(\.[\w@]+)*)\}"
           pattern = r"\{(\w+(\.\w+)*)\}"
    Adjust Pattern to allow any sequence of characters withint the {...}
       pattern = r"\{([^}]*)\}"
    """

    # non-greedy match of placeholders that are surrounded by curly braces:
    pattern = r"\{(.*?)\}"

    had_lookup_error = False

    # Define a function to replace placeholders. This
    # function is called by re.sub() for each pattern match below.
    def replace(match: re.Match) -> str:
        # we want to change the variable of the main method
        nonlocal had_lookup_error

        # Extract the first captured group from the regex match, which corresponds
        # to the placeholder name. In the regex pattern r"\{(.*?)\}", the parentheses
        # define a capturing group that matches any content inside curly braces `{}`.
        # For example, in the string "Hello {user.name}", the match would capture "user.name".
        # `match.group(1)` retrieves this captured text (the placeholder name),
        # which will later be used to look up corresponding values in the provided data frame
        # row or replacement dictionary.
        field_name = match.group(1)
        # split up the field name into keys at ".", e.g. cm_vehicles.make
        keys = field_name.split(".")  # Splitting the variable and sub-value
        # we initialize value with the data frame row (pd.Series):
        value = row
        # Walk through the list of keys:
        for key in keys:
            # first we access the field in the row and handle the
            # exception that key may not be a valid column (KeyError):
            try:
                # read the value of the data frame column defined by key
                value = value[key]
            except KeyError as e:
                self.logger.warning(
                    "KeyError: Cannot replace field -> '%s'%s as the data frame row does not have a column called -> '%s': %s",
                    field_name,
                    " (sub-key -> '{}')".format(key) if key != field_name else "",
                    field_name,
                    str(e),
                )
                had_lookup_error = True
                return ""
            except TypeError:
                # If we get an error with (value type -> <class 'float'>)
                # this is actually indicating we have a NaN value!
                self.logger.error(
                    "TypeError: Cannot replace field -> '%s'%s (value type -> %s). Expecting a dictionary-like value.",
                    field_name,
                    " (sub-key -> '{}')".format(key) if key != field_name else "",
                    str(type(value)),
                )
                had_lookup_error = True
                return ""

            # if the returned value is a list we use the index parameter
            # to select the item in the list according to the given index
            # We handle the exception that index may be out of range for
            # the list (IndexError).
            # If the given index is None we return the whole list. This
            # is required for multi-value attributes.
            if isinstance(value, list) and index is not None and len(value) > 0:
                try:
                    value = value[index]
                except IndexError:
                    self.logger.error(
                        "Index error in replacement of list field -> '%s' using index -> %s.",
                        field_name,
                        str(index),
                    )
                    had_lookup_error = True
                    return ""

            # Check if the column or sub-field value is NaN or None. Then we should not
            # continue to process the keys and return "" (it is important to do this
            # after the test for a list above otherwise we may get an error
            # "The truth value of an array with more than one element is ambiguous."):
            if not isinstance(value, list) and pd.isna(value):
                self.logger.debug(
                    "Cannot replace field -> '%s' as %s has no value in the current row!",
                    field_name,
                    (
                        "sub-key -> '{}'".format(key) if key != keys[0] else "column -> '{}'".format(key)
                    ),  # the first key is the column
                )
                had_lookup_error = True
                return ""

        # end for key in keys

        if isinstance(value, list):
            if value == []:
                had_lookup_error = True
                return ""
        else:
            if pd.isna(value):
                had_lookup_error = True
                return ""
            value = str(value)

        if replacements and field_name in replacements:
            # replacements is a dictionary that is defined
            # in the payload. Each item is a dictionary
            # that can be looked up by the field name
            field_replacements = replacements[field_name]
            upper = field_replacements.get("upper_case", False)
            lower = field_replacements.get("lower_case", False)
            regex_list = field_replacements.get("regex_list", [])
        else:
            regex_list = []
            upper = False
            lower = False

        if additional_regex_list:
            regex_list = (
                regex_list + additional_regex_list
            )  # don't do an append here as it would modify the original list

        if regex_list or upper or lower:
            if not isinstance(value, list):
                value = self.cleanup_value(
                    cleanup_value=value,
                    regex_list=regex_list,
                    upper=upper,
                    lower=lower,
                )
            else:  # we have a list, so we need to iterate
                for v in value:
                    v = self.cleanup_value(
                        cleanup_value=v,
                        regex_list=regex_list,
                        upper=upper,
                        lower=lower,
                    )

        value = str(value)

        return value

    # end sub-method replace()

    if not input_string:
        return ""

    # Use re.sub() to replace placeholders using the defined function
    # replace() - see above.
    result_string = re.sub(pattern, replace, input_string)

    if had_lookup_error:
        return ""

    return result_string

replace_bulk_placeholders_list(input_list, row, index=0, replacements=None, additional_regex_list=None)

Process list of payload strings and replace placeholders (see next method).

Parameters:

Name Type Description Default
input_list list

A list of strings that contain placeholders.

required
row Series

The curent data frame row.

required
index int

Index for use if we encounter a list value. Pass None here if you want alle elements of the list to be parsed and placeholders replaced.

0
replacements dict

The replacements to apply to given fields (dictionary key = field name)

None
additional_regex_list list

These are not coming from the payload but dynamically added for special needs like determining the nicknames.

None

Returns:

Name Type Description
bool bool

True = all replacements worked, False = some replacements had lookup errors.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="replace_bulk_placeholders_list")
def replace_bulk_placeholders_list(
    self,
    input_list: list,
    row: pd.Series,
    index: int = 0,
    replacements: dict | None = None,
    additional_regex_list: list | None = None,
) -> bool:
    """Process list of payload strings and replace placeholders (see next method).

    Args:
        input_list (list):
            A list of strings that contain placeholders.
        row (pd.Series):
            The curent data frame row.
        index (int, optional):
            Index for use if we encounter a list value.
            Pass None here if you want alle elements of
            the list to be parsed and placeholders replaced.
        replacements (dict, optional):
            The replacements to apply to given fields (dictionary key = field name)
        additional_regex_list (list, optional):
            These are not coming from the payload but dynamically
            added for special needs like determining the nicknames.

    Returns:
        bool:
            True = all replacements worked, False = some replacements had lookup errors.

    """

    success = True

    if not input_list:
        return False

    for i, value in enumerate(input_list):
        input_list[i] = self.replace_bulk_placeholders(
            input_string=value,
            row=row,
            index=index,
            replacements=replacements,
            additional_regex_list=additional_regex_list,
        )
        if not input_list[i]:
            success = False

    return success

replace_placeholders(content)

Replace placeholders in file content.

The content of the file is provided via a parameter. The replacements are defined in a object variable _placeholder_values (type = dictionary) The placeholder values are supposed to be surrounded by double % signs like %%OTAWP_RESOURCE_ID%%

Parameters:

Name Type Description Default
content str

The file content to replace placeholders in.

required

Returns:

Name Type Description
str str

Updated content with all defined replacements.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def replace_placeholders(self, content: str) -> str:
    """Replace placeholders in file content.

    The content of the file is provided via a parameter.
    The replacements are defined in a object variable
    _placeholder_values (type = dictionary)
    The placeholder values are supposed to be surrounded by
    double % signs like %%OTAWP_RESOURCE_ID%%

    Args:
        content (str):
            The file content to replace placeholders in.

    Returns:
        str:
            Updated content with all defined replacements.

    """
    # https://stackoverflow.com/questions/63502218/replacing-placeholders-in-a-text-file-with-python

    # if no placeholders are defined we can return the
    # initial value:
    if not self._placeholder_values:
        return content

    try:
        # We do a dynamic replacement here. The replacement is calculated
        # by the lambda function that is basically a lookup of the replacement
        # key found in the settings file with the value defined in the list
        # of replacement values in self._placeholder_values
        return re.sub(
            r"%%(\w+?)%%",
            lambda match: self._placeholder_values[match.group(1)],
            content,
        )
    except KeyError:
        self.logger.error(
            "Found placeholder in settings file without a defined value!",
        )
        return content
    except re.error:
        self.logger.error("Regex substitution error!")
        return content

resolve_attribute_values(attribute_name, attribute_type, attribute_values)

Resolve user logins or group names to user / group IDs.

This method handles the "special" cases where the actual attribute values need to be IDs but the payload has names.

This method is called for all attributes. If no special handling is required it just return an unmodified attribute value.

Parameters:

Name Type Description Default
attribute_name str

The name of the attribute.

required
attribute_type str

The type of the attribute.

required
attribute_values str | list[str]

The value of the attribute. A single value or a list of values.

required

Returns:

Type Description
str | list[str]

str | list[str]: A resolved attribute value or a list of resolved attribute values.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="resolve_attribute_values")
def resolve_attribute_values(
    self,
    attribute_name: str,
    attribute_type: str,
    attribute_values: str | list[str],
) -> str | list[str]:
    """Resolve user logins or group names to user / group IDs.

    This method handles the "special" cases where the actual
    attribute values need to be IDs but the payload has names.

    This method is called for all attributes. If no special handling
    is required it just return an unmodified attribute value.

    Args:
        attribute_name (str):
            The name of the attribute.
        attribute_type (str):
            The type of the attribute.
        attribute_values (str | list[str]):
            The value of the attribute. A single value or a list of values.

    Returns:
        str | list[str]:
            A resolved attribute value or a list of resolved attribute values.

    """

    # Special Case 1: handle special case where attribute type is a user picker.
    # we expect that the payload includes the login name for this
    # (as user IDs are not stable across systems) but then we need
    # to lookup the real user ID here:
    if attribute_type in ("otcs_user_picker", "otcs_member_picker"):
        self.logger.debug(
            "Attribute -> '%s' is is of type -> 'User Picker' (%s). Looking up user ID for user login name -> %s",
            attribute_name,
            attribute_type,
            attribute_values,
        )

        # Do we have a single value (not a list)?
        if not isinstance(attribute_values, list):
            user = self._otcs_frontend.get_user(name=attribute_values)
            user_id = self._otcs_frontend.lookup_result_value(
                response=user,
                key="name",
                value=attribute_values,
                return_key="id",
            )
            if user_id:
                # User has been found - determine ID:
                self.logger.debug(
                    "User -> '%s' has ID -> %s",
                    attribute_values,
                    user_id,
                )
                # Put the user ID into data structure
                return str(user_id)
            else:
                self.logger.error(
                    "User with login name -> '%s' does not exist!",
                    attribute_values,
                )
                # Clear the value to avoid workspace create failure
                return ""
        # Multi-value user attribute:
        else:
            user_ids = []
            for value in attribute_values:
                user = self._otcs_frontend.get_user(name=value)
                user_id = self._otcs_frontend.lookup_result_value(
                    response=user,
                    key="name",
                    value=value,
                    return_key="id",
                )
                if user_id:
                    # User has been found - determine ID:
                    self.logger.debug(
                        "User -> '%s' has ID -> %s",
                        value,
                        user_id,
                    )
                    # Put the user ID into the result list:
                    user_ids.append(str(user_id))
                else:
                    self.logger.error(
                        "User with login name -> '%s' does not exist!",
                        value,
                    )
            return user_ids

    # Special Case 2: handle Extended ECM for Government attribute type "Organizational Unit" (OU).
    # This is referring to a group ID which is not stable across deployments. So we need to lookup
    # the Group ID and add it to the data structure. This expects that the payload has the
    # group name and not the group ID:
    elif attribute_type == str(11480):
        self.logger.debug(
            "Attribute -> '%s' is is of type -> 'Organizational Unit' (%s). Looking up group ID for group name -> %s",
            attribute_name,
            attribute_type,
            attribute_values,
        )
        # Do we have a single value (not a list)?
        if not isinstance(attribute_values, list):
            group = self._otcs_frontend.get_group(
                name=attribute_values,
            )
            group_id = self._otcs_frontend.lookup_result_value(
                response=group,
                key="name",
                value=attribute_values,
                return_key="id",
            )

            if group_id:
                self.logger.debug(
                    "Group for Organizational Unit -> '%s' has ID -> %s",
                    attribute_values,
                    group_id,
                )
                # Put the group ID as a string:
                return str(group_id)
            else:
                self.logger.error(
                    "Group for Organizational Unit -> '%s' does not exist!",
                    attribute_values,
                )
                # Return an empty value string:
                return ""
        # Multi-value org group attribute:
        else:
            group_ids = []
            for value in attribute_values:
                group = self._otcs_frontend.get_group(
                    name=value,
                )
                group_id = self._otcs_frontend.lookup_result_value(
                    response=group,
                    key="name",
                    value=value,
                    return_key="id",
                )

                if group_id:
                    self.logger.debug(
                        "Group for Organizational Unit -> '%s' has ID -> %s",
                        value,
                        group_id,
                    )
                    # Put the group ID into the result list:
                    group_ids.append(str(group_id))
                else:
                    self.logger.error(
                        "Group for Organizational Unit -> '%s' does not exist!",
                        value,
                    )
            return group_ids

    # This is the default case - we return the unchanged attribute values:
    return attribute_values

start_impersonation(username, otcs_object=None)

Impersonate to a defined user.

Parameters:

Name Type Description Default
username str

The user to impersonate.

required
otcs_object OTCS | None

The OTCS object to use. If not provided then the _otcs object in the Payload object is used.

None
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="start_impersonation")
def start_impersonation(self, username: str, otcs_object: OTCS | None = None) -> bool:
    """Impersonate to a defined user.

    Args:
        username (str):
            The user to impersonate.
        otcs_object (OTCS | None, optional):
            The OTCS object to use. If not provided then the _otcs object
            in the Payload object is used.

    """

    if not otcs_object:
        otcs_object = self._otcs

    # Depending on the authentication type used with OTDS (token or ticket based)
    # the response structure is different:
    response = self._otds.impersonate_user(user_id=username)
    if not response:
        return False

    if "ticket" in response:
        otds_ticket = response.get("ticket", None)
        otcs_object.set_otds_ticket(ticket=otds_ticket)
    elif "access_token" in response:
        access_token = response.get("access_token", None)
        otcs_object.set_otds_token(token=access_token)
    else:
        self.logger.error("Impersonation response does not contain ticket or access_token!")
        return False

    otcs_object.invalidate_authentication_ticket()
    response = otcs_object.authenticate(revalidate=False)

    return bool(response)

stop_impersonation(otcs_object=None)

Impersonate back to admin user.

Parameters:

Name Type Description Default
username str

The user to impersonate.

required
otcs_object OTCS | None

The OTCS object to use. If not provided then the _otcs object in the Payload object is used.

None
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="stop_impersonation")
def stop_impersonation(self, otcs_object: OTCS | None = None) -> bool:
    """Impersonate back to admin user.

    Args:
        username (str):
            The user to impersonate.
        otcs_object (OTCS | None, optional):
            The OTCS object to use. If not provided then the _otcs object
            in the Payload object is used.

    """

    if not otcs_object:
        otcs_object = self._otcs

    # Clear OTDS ticket and token:
    otcs_object.set_otds_ticket(None)
    otcs_object.set_otds_token(None)

    # Clear OTCS ticket:
    otcs_object.invalidate_authentication_ticket()

    # Reauthenticate as admin (using username / password of OTCS object):
    response = otcs_object.authenticate(revalidate=True)

    return bool(response)

thread_wrapper(target, *args, **kwargs)

Wrap around threads to catch exceptions during exection.

Parameters:

Name Type Description Default
target callable

The method (callable) the Thread should run.

required
args tuple

The arguments for the method.

()
kwargs dict

Keyword arguments for the method.

{}
Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def thread_wrapper(self, target: Callable, *args: tuple, **kwargs: dict) -> None:
    """Wrap around threads to catch exceptions during exection.

    Args:
        target (callable):
            The method (callable) the Thread should run.
        args (tuple):
            The arguments for the method.
        kwargs (dict):
            Keyword arguments for the method.

    """

    try:
        target(*args, **kwargs)
    except Exception:
        thread_name = threading.current_thread().name
        self.logger.error(
            "Thread '%s' failed!",
            thread_name,
        )
        self.logger.error(traceback.format_exc())

write_data_source_file(data_source, compression=True)

Write a data source file as JSON into the Admin Personal Workspace in Content Server.

This allows for further analysis or to reload it in succeeding customizer runs.

Parameters:

Name Type Description Default
data_source dict

Data source dictionary with embedded "data" Data object.

required
compression bool

If True, a compressed JSON file gets saved. If False then an uncompressed JSON is saved.

True

Returns:

Name Type Description
bool bool

True if the data source file (JSON) as been upladed to Content Server successfully, False otherwise.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="write_data_source_file")
def write_data_source_file(
    self,
    data_source: dict,
    compression: bool = True,
) -> bool:
    """Write a data source file as JSON into the Admin Personal Workspace in Content Server.

    This allows for further analysis or to reload it in succeeding customizer runs.

    Args:
        data_source (dict):
            Data source dictionary with embedded "data" Data object.
        compression (bool):
            If True, a compressed JSON file gets saved. If False then
            an uncompressed JSON is saved.

    Returns:
        bool:
            True if the data source file (JSON) as been upladed to
            Content Server successfully, False otherwise.

    """

    data_source_name = data_source.get("name", "")
    if not data_source_name:
        self.logger.error("Missing data source name!")
        return False

    data: Data = data_source.get("data")
    if not data:
        self.logger.error(
            "Data source -> '%s' does not have data! Cannot save it.",
            data_source_name,
        )
        return False

    response = self._otcs.get_node_by_volume_and_path(
        volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
    )  # write to Personal Workspace of Admin (with Volume Type ID = 142)
    target_folder_id = self._otcs.get_result_value(response=response, key="id")
    if not target_folder_id:
        target_folder_id = 2004  # use Personal Workspace of Admin as fallback

    if self._payload_source:
        payload_file_name = os.path.basename(
            self._payload_source,
        )  # remove directories
        # Split once at the first occurance of a dot
        # as the _payload_source may have multiple suffixes
        # such as .yml.gz.b64:
        payload_file_name = payload_file_name.split(".", 1)[0] + "_"
    else:
        payload_file_name = ""

    file_name = "data_source_" + payload_file_name + data_source_name + ".json"
    if compression:
        file_name += ".zip"
    full_path = os.path.join(tempfile.gettempdir(), file_name)

    # We also want to keep the row numbers (index):
    if not data.save_json_data(
        json_path=full_path,
        preserve_index=True,
        index_column="row",
        compression="zip" if compression else None,
    ):
        self.logger.error(
            "Data source -> '%s' could not be saved!",
            data_source_name,
        )
        return False

    # Check if the status file has been uploaded before.
    # This can happen if we re-run the python container.
    # In this case we add a version to the existing document:
    response = self._otcs.get_node_by_parent_and_name(
        parent_id=int(target_folder_id),
        name=file_name,
        show_error=False,
    )
    data_source_node_id = self._otcs.get_result_value(response=response, key="id")
    if data_source_node_id:
        response = self._otcs.add_document_version(
            node_id=int(data_source_node_id),
            file_url=full_path,
            file_name=file_name,
            mime_type="application/zip" if compression else "application/json",
            description="Updated data source file after re-run of customization",
        )
    else:
        response = self._otcs.upload_file_to_parent(
            file_url=full_path,
            file_name=file_name,
            mime_type="application/zip" if compression else "application/json",
            parent_id=int(target_folder_id),
        )

    if response:
        self.logger.info(
            "Data source file -> '%s' has been written to Personal Workspace of admin user.",
            file_name,
        )
        # Don't leave stuff behind:
        os.remove(full_path)

        return True

    self.logger.error(
        "Failed to write data source file -> '%s' to Personal Workspace of admin user!",
        file_name,
    )

    return False

write_status_file(success, payload_section_name, payload_section, payload_specific=True)

Write a status file into the Admin Personal Workspace in Content Server.

This is to indicate that the payload section has been deployed successfully. This speeds up the customizing process in case the customizer pod is restarted.

Parameters:

Name Type Description Default
success bool

True, if the section was processed successful, False otherwise.

required
payload_section_name str

The name of the payload section.

required
payload_section list

The payload section content - this is written as JSon into the file.

required
payload_specific bool

Whether or not the success should be specific for each payload file or if success is "global" - like for the deletion of the existing M365 teams (which we don't want to execute per payload file)

True

Returns:

Name Type Description
bool bool

True if the status file as been upladed to Content Server successfully, False otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
@tracer.start_as_current_span(attributes=OTEL_TRACING_ATTRIBUTES, name="write_status_file")
def write_status_file(
    self,
    success: bool,
    payload_section_name: str,
    payload_section: list,
    payload_specific: bool = True,
) -> bool:
    """Write a status file into the Admin Personal Workspace in Content Server.

    This is to indicate that the payload section has been deployed successfully.
    This speeds up the customizing process in case the customizer pod
    is restarted.

    Args:
        success (bool):
            True, if the section was processed successful, False otherwise.
        payload_section_name (str):
            The name of the payload section.
        payload_section (list):
            The payload section content - this is written as JSon into the file.
        payload_specific (bool, optional):
            Whether or not the success should be specific for
            each payload file or if success is "global" - like for the deletion
            of the existing M365 teams (which we don't want to execute per
            payload file)

    Returns:
        bool:
            True if the status file as been upladed to Content Server successfully, False otherwise

    """

    # Do we want status files to be uploaded?
    if not self.upload_status_files:
        return True

    if success:
        self.logger.info(
            "Payload section -> '%s' has been completed successfully.",
            payload_section_name,
        )
        prefix = "success_"
    else:
        self.logger.error(
            "Payload section -> '%s' had failures!",
            payload_section_name,
        )
        prefix = "failure_"

    while not self._otcs.is_ready():
        self.logger.info(
            "OTCS is not ready. Cannot write status file for -> '%s' to OTCS. Waiting 30 seconds and retry...",
            payload_section_name,
        )
        time.sleep(30)

    response = self._otcs.get_node_by_volume_and_path(
        volume_type=self._otcs.VOLUME_TYPE_PERSONAL_WORKSPACE,
    )  # write to Personal Workspace of Admin (with Volume Type ID = 142)
    target_folder_id = self._otcs.get_result_value(response=response, key="id")
    if not target_folder_id:
        target_folder_id = 2004  # use Personal Workspace of Admin as fallback

    file_name = self.get_status_file_name(
        payload_section_name=payload_section_name,
        payload_specific=payload_specific,
        prefix=prefix,
    )

    full_path = os.path.join(tempfile.gettempdir(), file_name)

    with open(full_path, mode="w", encoding="utf-8") as localfile:
        localfile.write(json.dumps(payload_section, indent=2))

    # Check if the status file has been uploaded before.
    # This can happen if we re-run the python container.
    # In this case we add a version to the existing document:
    response = self._otcs.get_node_by_parent_and_name(
        parent_id=int(target_folder_id),
        name=file_name,
        show_error=False,
    )
    target_document_id = self._otcs.get_result_value(response=response, key="id")
    if target_document_id:
        response = self._otcs.add_document_version(
            node_id=int(target_document_id),
            file_url=full_path,
            file_name=file_name,
            mime_type="text/plain",
            description="Updated status file after re-run of customization",
        )
    else:
        response = self._otcs.upload_file_to_parent(
            file_url=full_path,
            file_name=file_name,
            mime_type="text/plain",
            parent_id=int(target_folder_id),
        )

    if response:
        self.logger.info(
            "Status file -> '%s' has been written to Personal Workspace of admin user.",
            file_name,
        )
        return True

    self.logger.error(
        "Failed to write status file -> '%s' to Personal Workspace of admin user!",
        file_name,
    )

    return False

load_payload(payload_source, logger=default_logger)

Load payload file.

We don't want to have this inside the class to allow it to be used also indepent (see api.py)

Parameters:

Name Type Description Default
payload_source str

The path to the payload file.

required
logger Logger

The logger object. Defaults to default_logger.

default_logger

Returns:

Type Description
dict | None

dict | None: The payload dictionary or None in case of an error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload.py
def load_payload(
    payload_source: str,
    logger: logging.Logger = default_logger,
) -> dict | None:
    """Load payload file.

    We don't want to have this inside the class
    to allow it to be used also indepent (see api.py)

    Args:
        payload_source (str):
            The path to the payload file.
        logger (logging.Logger, optional):
            The logger object. Defaults to default_logger.

    Returns:
        dict | None:
            The payload dictionary or None in case of an error.

    """

    if not os.path.exists(payload_source):
        logger.error("Cannot access payload file -> '%s'!", payload_source)
        return None

    # Is it a YAML file?
    if payload_source.endswith(".yaml"):
        logger.info("Open payload from YAML file -> '%s'...", payload_source)
        try:
            with open(payload_source, encoding="utf-8") as stream:
                payload_data = stream.read()
            return yaml.safe_load(payload_data)
        except yaml.YAMLError:
            logger.error(
                "Error while reading YAML payload file -> '%s'!",
                payload_source,
            )
            payload = {}
    # Or is it a Terraform HCL file?
    elif payload_source.endswith((".tf", ".tfvars")):
        logger.info("Open payload from Terraform file -> '%s'...", payload_source)
        try:
            with open(payload_source, encoding="utf-8") as stream:
                payload = hcl2.api.load(stream)
            # If payload is wrapped into "external_payload" we unwrap it:
            if payload.get("external_payload"):
                payload = payload["external_payload"]

        except (
            lark_exceptions.UnexpectedToken,
            lark_exceptions.UnexpectedCharacters,
        ) as exc:
            exception = f"Syntax error while reading Terraform payload file -> '{payload_source}'! --> {traceback.format_exception_only(exc)}"
            raise PayloadImportError(exception) from exc

        except (
            FileNotFoundError,
            ImportError,
            ValueError,
            SyntaxError,
        ) as exc:
            exception = f"Error while reading Terraform payload file -> '{payload_source}'! --> {traceback.format_exception_only(exc)}"
            raise PayloadImportError(exception) from exc

    elif payload_source.endswith(".yml.gz.b64"):
        logger.info("Open payload from base64-gz-YAML file -> '%s'...", payload_source)
        try:
            with open(payload_source, encoding="utf-8") as stream:
                content = base64.b64decode(stream.read())
                decoded_data = gzip.decompress(content)

                payload = yaml.safe_load(decoded_data)

        except yaml.YAMLError:
            logger.error(
                "Error while reading YAML payload file -> '%s'!",
                payload_source,
            )
            payload = {}

    # If not, it is an unsupported type:
    else:
        logger.error(
            "Payload file -> '%s' has unsupported file type!",
            payload_source,
        )
        payload = {}

    if not payload:
        logger.error(
            "Payload -> '%s' is undefined or empty! Skipping...",
            payload_source,
        )
        return None

    return payload

Payload List Module to implement methods to maintain and process a list of payload files.

This code typically runs in a container as part of the cloud automation.

PayloadList

Manage a sorted list of payload items using a pandas data frame.

Each payload item with metadata such as name, filename, dependency (referencing another item by index), logfile, and status. Provides list-like functionality with additional methods for adding, removing, and reordering items.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
class PayloadList:
    """Manage a sorted list of payload items using a pandas data frame.

    Each payload item with metadata such as name, filename, dependency (referencing another item by index),
    logfile, and status. Provides list-like functionality with additional methods
    for adding, removing, and reordering items.
    """

    logger: logging.Logger = default_logger

    _stopped: bool = True
    payload_items: pd.DataFrame

    def __init__(self, logger: logging.Logger = default_logger) -> None:
        """Initialize the Payload List object.

        Args:
            logger (logging.Logger, optional):
                The logging object to use for all log messages. Defaults to default_logger.

        """
        if logger != default_logger:
            self.logger = logging.getLogger(f"{logger.name}.payload_list")

        self.payload_items = pd.DataFrame(
            columns=[
                "name",
                "filename",
                "dependencies",
                "logfile",
                "status",
                "enabled",
                "git_url",
                "loglevel",
                "start_time",
                "stop_time",
                "duration",
                "log_debug",
                "log_info",
                "log_warning",
                "log_error",
                "log_critical",
                "customizer_settings",
            ],
        )

    # end method definition

    def calculate_payload_item_duration(self) -> None:
        """Update the dataframe column "duration" for all running items."""

        def calculate_duration(row: pd.Series) -> str:
            if row["status"] == "running":
                now = datetime.now(UTC)
                start_time = pd.to_datetime(row["start_time"])

                duration = now - start_time
                hours, remainder = divmod(duration.total_seconds(), 3600)
                minutes, seconds = divmod(remainder, 60)
                formatted_duration = f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}"

                return formatted_duration
            else:
                return str(row["duration"])  # or whatever the original value should be

        # updates the "duration" column of the DataFrame self.payload_items
        # by applying the method calculate_duration() to each row:
        self.payload_items["duration"] = self.payload_items.apply(
            calculate_duration,
            axis=1,
        )

    # end method definition

    def get_payload_items(self) -> pd.DataFrame:
        """Get the payload items in their current order in the PayloadList.

        Returns:
            pd.DataFrame:
                A data frame containing all items in their current order.

        """

        self.calculate_payload_item_duration()

        return self.payload_items

    # end method definition

    def get_payload_item(self, index: int) -> pd.Series:
        """Get the payload item by index if it exists, otherwise return None.

        Args:
            index (int): index of the row

        Returns:
            pd.Series: row with the matching index or None if there is no row with that index

        """

        self.calculate_payload_item_duration()

        if index not in self.payload_items.index:
            self.logger.error("Index -> %s is out of range", str(index))
            return None

        return self.payload_items.loc[index]

    # end method definition

    def get_payload_item_by_name(self, name: str) -> pd.Series:
        """Get the payload item by name if it exists, otherwise return None.

        Args:
            name (str):
                The name of the payload.

        Returns:
            pd.Series:
                Row with the matching name or None if there is no row with that index

        """

        self.calculate_payload_item_duration()

        df = self.get_payload_items()
        data = [{"index": idx, **row} for idx, row in df.iterrows()]

        return next((item for item in data if item.get("name") == name), None)

    # end method definition

    def get_payload_items_by_value(
        self,
        column: str,
        value: str,
    ) -> pd.DataFrame | None:
        """Filter the PayloadList by a given value in a specific column.

        Args:
            column (str):
                The column to filter by.
            value (str):
                The value to match in the specified column.

        Returns:
            pd.DataFrame: A DataFrame containing rows where the given column matches the value.

        Example:
            >>> payload_list = PayloadList()
            >>> payload_list.add_item("Task1", "task1.txt", status="running")
            >>> payload_list.add_item("Task2", "task2.txt", status="completed")
            >>> payload_list.add_item("Task3", "task3.txt", status="running")
            >>> payload_list.get_payload_items_by_value(column="status", value="running")
                name         file    dependencies  logfile   status    enabled
            0   Task1    task1.txt        NaN        None   running     True
            2   Task3    task3.txt        NaN        None   running     True

        """

        if column not in self.payload_items.columns:
            self.logger.error(
                "Column -> '%s' does not exist in the payload list!",
                str(column),
            )
            return None

        filtered_items = self.payload_items[self.payload_items[column] == value]

        return filtered_items

    # end method definition

    def add_payload_item(
        self,
        name: str,
        filename: str,
        logfile: str,
        dependencies: list | None = None,
        status: str = "pending",
        enabled: bool = True,
        git_url: str | None = None,
        loglevel: str = "INFO",
        customizer_settings: dict | None = None,
    ) -> dict:
        """Add a new item to the PayloadList.

        Args:
            name (str):
                The name of the item.
            filename (str):
                The file associated with the item.
            logfile (str):
                Log file information for the item. Defaults to None.
            dependencies (list):
                The index of another item this item depends on. Defaults to None.
            status (str):
                The status of the item. Must be one of 'planned', 'running',
                'completed', or 'failed'. Defaults to 'planned'.
            enabled (bool):
                True if the payload is enabled. False otherwise.
            git_url (str):
                Link to the payload in the GIT repository.
            loglevel (str):
                The log level for processing the payload. Either "INFO" or "DEBUG".
            customizer_settings (dict):
                Customizer settings for the payload. Defaults to None.

        """

        new_item = {
            "name": name if name else filename,
            "filename": filename,
            "dependencies": dependencies if dependencies else [],
            "logfile": logfile,
            "status": status,
            "enabled": enabled,
            "git_url": git_url,
            "loglevel": loglevel,
            "log_debug": 0,
            "log_info": 0,
            "log_warning": 0,
            "log_error": 0,
            "log_critical": 0,
            "customizer_settings": customizer_settings if customizer_settings else {},
        }
        self.payload_items = pd.concat(
            [self.payload_items, pd.DataFrame([new_item])],
            ignore_index=True,
        )

        new_item = self.payload_items.tail(1).to_dict(orient="records")[0]
        new_item["index"] = self.payload_items.index[-1]

        return new_item

    # end method definition

    def update_payload_item(
        self,
        index: int,
        update_data: dict,
    ) -> bool:
        """Update an existing item in the PayloadList.

        Args:
            index (int):
                The position of the payload.
            update_data (str):
                The data of the item.

        Returns:
            bool:
                True = success, False = error.

        """

        if index not in self.payload_items.index:
            self.logger.error("Illegal index -> %s for payload update!", index)
            return False

        for column in self.payload_items.columns:
            if column in update_data:
                tmp = self.payload_items.loc[index].astype(object)
                tmp[column] = update_data[column]
                self.payload_items.loc[index] = tmp

        return True

    # end method definition

    def remove_payload_item(self, index: int) -> bool:
        """Remove an item by its index from the PayloadList.

        Args:
            index (int):
                The index of the item to remove.

        Returns:
            bool:
                True = success. False = failure.

        Raises:
            IndexError: If the index is out of range.

        """

        if index not in self.payload_items.index:
            self.logger.error("Index -> %s is out of range!", index)
            return False

        self.payload_items.drop(index, inplace=True)

        return True

    # end method definition

    def move_payload_item_up(self, index: int) -> int | None:
        """Move an item up by one position in the PayloadList.

        Args:
            index (int): The index of the item to move up.

        Results:
            bool: False, if the index is out of range or the item is already at the top.
                  True otherwise

        """

        if index <= 0 or index >= len(self.payload_items):
            self.logger.error(
                "Index -> %s is out of range or already at the top!",
                str(index),
            )
            return None

        self.payload_items.iloc[[index - 1, index]] = self.payload_items.iloc[[index, index - 1]].to_numpy()

        new_postion = self.payload_items.index.get_loc(index)

        return new_postion

    # end method definition

    def move_payload_item_down(self, index: int) -> int | None:
        """Move an item down by one position in the PayloadList.

        Args:
            index (int):
                The index of the item to move down.

        Returns:
            int:
                The new position of the payload item.

        """

        if index < 0 or index >= len(self.payload_items) - 1:
            self.logger.error(
                "Index -> %s is out of range or already at the bottom!",
                str(index),
            )
            return None

        self.payload_items.iloc[[index, index + 1]] = self.payload_items.iloc[[index + 1, index]].to_numpy()

        new_postion = self.payload_items.index.get_loc(index)

        return new_postion

    # end method definition

    def __len__(self) -> int:
        """Return the number of items in the PayloadList.

        Returns:
            int:
                The count of items in the list.

        """

        return len(self.payload_items)

    # end method definition

    def __getitem__(self, index: int) -> pd.Series:
        """Get an item by its index using the "[index]" syntax.

        Args:
            index (int):
                The index of the item to retrieve.

        Returns:
            pd.Series:
                The item at the specified index as a Series.

        Raises:
            IndexError: If the index is out of range.

        Example:
            >>> payload_list = PayloadList()
            >>> payload_list.add_item("Task1", "task1.txt")
            >>> payload_list[0]
            name        Task1
            file    task1.txt
            dependencies    NaN
            logfile      None
            status    planned
            Name: 0, dtype: object

        """

        if index not in self.payload_items.index:
            exception = "Index -> {} is out of range".format(index)
            raise IndexError(exception)

        return self.payload_items.loc[index]

    # end method definition

    def __setitem__(self, index: int, value: dict) -> None:
        """Set an item at the specified index using the "[index]" syntax.

        Args:
            index (int): The index to set the item at.
            value (dict): The item dictionary to set, which must include 'name' and 'file' keys.

        Raises:
            IndexError: If the index is out of range.
            ValueError: If the provided value is not a valid item dictionary.

        Example:
            >>> payload_list = PayloadList()
            >>> payload_list.add_item("Task1", "task1.txt")
            >>> payload_list[0]
            name        Task1
            filename    task1.txt
            dependencies    NaN
            logfile      None
            status    planned
            Name: 0, dtype: object
            >>> payload_list[0] = {"name": "Updated Task1", "file": "updated_task1.txt", "status": "completed"}
            >>> payload_list[0]
            name        Updated Task1
            filename    updated_task1.txt
            dependencies    NaN
            logfile      None
            status    completed
            Name: 0, dtype: object

        """

        if not {"name", "filename"}.issubset(value):
            exception = ("Value must be a dictionary with at least 'name' and 'filename' keys",)
            raise ValueError(
                exception,
            )

        if index not in self.payload_items.index:
            exception = "Index -> {} is out of range".format(index)
            raise IndexError(exception)

        self.payload_items.loc[index] = value

    # end method definition

    def __delitem__(self, index: int) -> None:
        """Delete an item by its index.

        Args:
            index (int): The index of the item to delete.

        Raises:
            IndexError: If the index is out of range.

        """

        self.remove_item(index=index)

    # end method definition

    def __getattr__(self, attribute: str) -> pd.Series:
        """Provide dynamic access to columns using the "." syntax.

        For example, `payload_list.name` will return the 'name' column values.

        Args:
            attribute (str): The column name to retrieve.

        Returns:
            pd.Series: The specified column as a pandas Series.

        Example:
            >>> payload_list = PayloadList()
            >>> payload_list.add_item("Task1", "task1.txt")
            >>> payload_list.name
            0    Task1
            Name: name, dtype: object

        """

        if attribute in self.payload_items.columns:
            return self.payload_items[attribute]

        self.logger.error("Payload list has no attribute -> '%s'", attribute)
        return None

    # end method definition

    def __repr__(self) -> str:
        """Return a string representation of the PayloadList for logging and debugging.

        Returns:
            str:
                A string representing the items in the DataFrame.

        """

        return self.payload_items.to_string(index=True)

    # end method definition

    def __iter__(self) -> iter:
        """Iterate over the rows of the PayloadList.

        Returns:
            iterator: An iterator over the rows of the payload_items DataFrame.

        Example:
            >>> payload_list = PayloadList()
            >>> payload_list.add_item("Task1", "task1.txt")
            >>> payload_list.add_item("Task2", "task2.txt")
            >>> for payload in payload_list:
            >>>     print(payload)
            name        Task1
            filename    task1.txt
            dependencies    NaN
            logfile      None
            status    planned
            Name: 0, dtype: object
            name        Task2
            file    task2.txt
            dependencies    NaN
            logfile      None
            status    planned
            Name: 1, dtype: object

        """

        # Return an iterator for the rows of the DataFrame
        for _, row in self.payload_items.iterrows():
            yield row

    # end method definition

    def pick_runnables(self) -> pd.DataFrame:
        """Pick all PayloadItems with status "planned" and no dependencies on items that are not in status "completed".

        Returns:
            pd.DataFrame:
                A list of runnable payload items.

        """

        def is_runnable(row: pd.Series) -> bool:
            # Check if item is enabled:
            if not row["enabled"]:
                return False

            # Check if all dependencies have been completed
            dependencies: list[int] = row["dependencies"]

            return all(self.payload_items.loc[dep, "status"] == "completed" for dep in dependencies or [])

        # end sub-method definition

        if self.payload_items.empty:
            return None

        # Filter payload items to find runnable items
        runnable_df: pd.DataFrame = self.payload_items[
            (self.payload_items["status"] == "planned") & self.payload_items.apply(is_runnable, axis=1)
        ].copy()

        # Add index as a column to the resulting DataFrame
        runnable_df["index"] = runnable_df.index

        # Log each runnable item
        for _, row in runnable_df.iterrows():
            self.logger.debug(
                "Added payload file -> '%s' with index -> %s to runnable queue.",
                row["name"] if row["name"] else row["filename"],
                row["index"],
            )

        return runnable_df

    # end method definition

    def pick_running(self) -> int:
        """Pick all PayloadItems with status "running".

        Returns:
            pd.DataFrame:
                A list of running payload items.

        """

        if self.payload_items.empty:
            return 0

        all_status = self.payload_items["status"].value_counts().to_dict()

        return all_status.get("running", 0)

    # end method definition

    def process_payload_list(self, concurrent: int | None = None) -> None:
        """Process runnable payloads.

        Args:
            concurrent (int | None, optional):
                The maximum number of concurrent payloads to run at any given time.

        Continuously checks for runnable payload items and starts their
        "process_payload" method in separate threads.
        Runs as a daemon until the customizer ends.

        """

        @tracer.start_as_current_span("run_and_complete_payload")
        def run_and_complete_payload(payload_item: pd.Series) -> None:
            """Run the payload's process_payload method and marks the status as completed afterward."""

            t = trace.get_current_span()
            t.set_attributes({"payload_id": payload_item["index"], "payload_name": payload_item["name"]})

            start_time = datetime.now(UTC)
            self.update_payload_item(payload_item["index"], {"start_time": start_time})

            # Create a logger with thread_id:
            thread_logger = logging.getLogger(
                name="Payload_{}".format(payload_item["index"]),
            )

            thread_logger.setLevel(level=payload_item["loglevel"])

            # Check if the logger already has handlers. If it does, they are removed before creating new ones.
            if thread_logger.hasHandlers():
                thread_logger.handlers.clear()

            # Create a handler for the logger:
            handler = logging.FileHandler(filename=payload_item.logfile)

            # Create a formatter:
            formatter = logging.Formatter(
                fmt="%(asctime)s %(levelname)s [%(name)s] [%(threadName)s] %(message)s",
                datefmt="%d-%b-%Y %H:%M:%S",
            )
            # Add the formatter to the handler
            handler.setFormatter(fmt=formatter)
            thread_logger.addHandler(hdlr=handler)

            if len(thread_logger.filters) == 0:
                thread_logger.debug("Adding log count filter to logger")
                thread_logger.addFilter(
                    LogCountFilter(
                        payload_items=self.payload_items,
                        index=payload_item["index"],
                    ),
                )

            thread_logger.info(
                "Start processing of payload -> '%s' (%s) from filename -> '%s'",
                payload_item["name"],
                payload_item["index"],
                payload_item["filename"],
            )

            local = threading.local()

            # Read customizer Settings from customizerSettings in the payload:
            payload = load_payload(payload_item["filename"])

            if not payload:
                success = False

            if payload:
                customizer_settings = payload_item["customizer_settings"]

                # Overwrite the customizer settings with the payload specific ones:
                customizer_settings.update(
                    {
                        "cust_payload": payload_item["filename"],
                        "cust_payload_gz": "",
                        "cust_payload_external": "",
                        "cust_log_file": payload_item.logfile,
                    },
                )

                try:
                    local.customizer_thread_object = Customizer(
                        settings=customizer_settings,
                        logger=thread_logger,
                    )
                    thread_logger.info("Customizer initialized successfully.")

                    thread_logger.debug(
                        "Customizer Settings -> \n %s",
                        pprint.pformat(
                            local.customizer_thread_object.settings.model_dump(),
                        ),
                    )

                    if customizer_settings.get("profiling", False):
                        import cProfile
                        import pstats

                        cprofiler = cProfile.Profile()
                        cprofiler.enable()

                    success = local.customizer_thread_object.customization_run()

                    if customizer_settings.get("profiling", False):
                        cprofiler.disable()

                    now = datetime.now(UTC)
                    log_path = os.path.dirname(payload_item.logfile)
                    profile_log_prefix = (
                        f"{log_path}/{payload_item['index']}_{payload_item['name']}_{now.strftime('%Y-%m-%d_%H-%M-%S')}"
                    )

                    if customizer_settings.get("profiling", False):
                        import io

                        s = io.StringIO()
                        stats = pstats.Stats(cprofiler, stream=s).sort_stats("cumtime")
                        stats.print_stats()
                        with open(f"{profile_log_prefix}.log", "w+") as f:
                            f.write(s.getvalue())
                        stats.dump_stats(filename=f"{profile_log_prefix}.cprof")

                except ValidationError:
                    thread_logger.error("Validation error!")
                    success = False

                except StopOnError:
                    success = False
                    thread_logger.error(
                        "StopOnErrorException occurred. Stopping payload processing...",
                    )

                except Exception:
                    success = False
                    thread_logger.error(
                        "An exception occurred: \n%s",
                        traceback.format_exc(),
                    )

            if not success:
                thread_logger.error(
                    "Failed to initialize payload -> '%s'!",
                    payload_item["filename"],
                )
                # Update the status to "failed" in the DataFrame after processing finishes
                self.update_payload_item(payload_item["index"], {"status": "failed"})

            else:
                # Update the status to "completed" in the DataFrame after processing finishes
                self.update_payload_item(payload_item["index"], {"status": "completed"})

            stop_time = datetime.now(UTC)
            duration = stop_time - start_time

            # Format duration in hh:mm:ss
            hours, remainder = divmod(duration.total_seconds(), 3600)
            minutes, seconds = divmod(remainder, 60)
            formatted_duration = f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}"

            self.update_payload_item(
                payload_item["index"],
                {"stop_time": stop_time, "duration": formatted_duration},
            )

        # end  def run_and_complete_payload()

        # add delay here to allow for logging to work reliably for the the first payload
        time.sleep(10)

        while not self._stopped:
            # Get runnable items as subset of the initial data frame:
            runnable_items: pd.DataFrame = self.pick_runnables()

            # Start a thread for each runnable item (item is a pd.Series)
            if runnable_items is not None:
                for _, item in runnable_items.iterrows():
                    if concurrent and self.pick_running() >= concurrent:
                        self.logger.debug(
                            "Reached concurrency limit of %s payloads. Waiting for one to finish.",
                        )
                        break

                    self.logger.info(
                        "Added payload file -> '%s' with index -> %s to runnable queue.",
                        item["name"] if item["name"] else item["filename"],
                        item["index"],
                    )

                    # Update the status to "running" in the data frame to prevent re-processing
                    self.payload_items.loc[
                        self.payload_items["name"] == item["name"],
                        "status",
                    ] = "running"

                    # Start the process_payload method in a new thread
                    thread = threading.Thread(
                        target=run_and_complete_payload,
                        args=(item,),
                        name=item["name"],
                    )
                    thread.start()
                    break

            # Sleep briefly to avoid a busy wait loop
            time.sleep(1)

    # end method definition

    def run_payload_processing(self, concurrent: int | None = None) -> None:
        """Start the `process_payload_list` method in a daemon thread."""

        scheduler_thread = threading.Thread(
            target=self.process_payload_list,
            daemon=True,
            name="Scheduler",
            kwargs={"concurrent": concurrent},
        )

        self.logger.info(
            "Starting '%s' thread for payload list processing...",
            str(scheduler_thread.name),
        )
        self._stopped = False
        scheduler_thread.start()

    # end method definition

    def stop_payload_processing(self) -> None:
        """Set a stop flag which triggers the stopping of further payload processing."""

        self._stopped = True

__delitem__(index)

Delete an item by its index.

Parameters:

Name Type Description Default
index int

The index of the item to delete.

required

Raises:

Type Description
IndexError

If the index is out of range.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def __delitem__(self, index: int) -> None:
    """Delete an item by its index.

    Args:
        index (int): The index of the item to delete.

    Raises:
        IndexError: If the index is out of range.

    """

    self.remove_item(index=index)

__getattr__(attribute)

Provide dynamic access to columns using the "." syntax.

For example, payload_list.name will return the 'name' column values.

Parameters:

Name Type Description Default
attribute str

The column name to retrieve.

required

Returns:

Type Description
Series

pd.Series: The specified column as a pandas Series.

Example

payload_list = PayloadList() payload_list.add_item("Task1", "task1.txt") payload_list.name 0 Task1 Name: name, dtype: object

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def __getattr__(self, attribute: str) -> pd.Series:
    """Provide dynamic access to columns using the "." syntax.

    For example, `payload_list.name` will return the 'name' column values.

    Args:
        attribute (str): The column name to retrieve.

    Returns:
        pd.Series: The specified column as a pandas Series.

    Example:
        >>> payload_list = PayloadList()
        >>> payload_list.add_item("Task1", "task1.txt")
        >>> payload_list.name
        0    Task1
        Name: name, dtype: object

    """

    if attribute in self.payload_items.columns:
        return self.payload_items[attribute]

    self.logger.error("Payload list has no attribute -> '%s'", attribute)
    return None

__getitem__(index)

Get an item by its index using the "[index]" syntax.

Parameters:

Name Type Description Default
index int

The index of the item to retrieve.

required

Returns:

Type Description
Series

pd.Series: The item at the specified index as a Series.

Raises:

Type Description
IndexError

If the index is out of range.

Example

payload_list = PayloadList() payload_list.add_item("Task1", "task1.txt") payload_list[0] name Task1 file task1.txt dependencies NaN logfile None status planned Name: 0, dtype: object

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def __getitem__(self, index: int) -> pd.Series:
    """Get an item by its index using the "[index]" syntax.

    Args:
        index (int):
            The index of the item to retrieve.

    Returns:
        pd.Series:
            The item at the specified index as a Series.

    Raises:
        IndexError: If the index is out of range.

    Example:
        >>> payload_list = PayloadList()
        >>> payload_list.add_item("Task1", "task1.txt")
        >>> payload_list[0]
        name        Task1
        file    task1.txt
        dependencies    NaN
        logfile      None
        status    planned
        Name: 0, dtype: object

    """

    if index not in self.payload_items.index:
        exception = "Index -> {} is out of range".format(index)
        raise IndexError(exception)

    return self.payload_items.loc[index]

__init__(logger=default_logger)

Initialize the Payload List object.

Parameters:

Name Type Description Default
logger Logger

The logging object to use for all log messages. Defaults to default_logger.

default_logger
Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def __init__(self, logger: logging.Logger = default_logger) -> None:
    """Initialize the Payload List object.

    Args:
        logger (logging.Logger, optional):
            The logging object to use for all log messages. Defaults to default_logger.

    """
    if logger != default_logger:
        self.logger = logging.getLogger(f"{logger.name}.payload_list")

    self.payload_items = pd.DataFrame(
        columns=[
            "name",
            "filename",
            "dependencies",
            "logfile",
            "status",
            "enabled",
            "git_url",
            "loglevel",
            "start_time",
            "stop_time",
            "duration",
            "log_debug",
            "log_info",
            "log_warning",
            "log_error",
            "log_critical",
            "customizer_settings",
        ],
    )

__iter__()

Iterate over the rows of the PayloadList.

Returns:

Name Type Description
iterator iter

An iterator over the rows of the payload_items DataFrame.

Example

payload_list = PayloadList() payload_list.add_item("Task1", "task1.txt") payload_list.add_item("Task2", "task2.txt") for payload in payload_list: print(payload) name Task1 filename task1.txt dependencies NaN logfile None status planned Name: 0, dtype: object name Task2 file task2.txt dependencies NaN logfile None status planned Name: 1, dtype: object

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def __iter__(self) -> iter:
    """Iterate over the rows of the PayloadList.

    Returns:
        iterator: An iterator over the rows of the payload_items DataFrame.

    Example:
        >>> payload_list = PayloadList()
        >>> payload_list.add_item("Task1", "task1.txt")
        >>> payload_list.add_item("Task2", "task2.txt")
        >>> for payload in payload_list:
        >>>     print(payload)
        name        Task1
        filename    task1.txt
        dependencies    NaN
        logfile      None
        status    planned
        Name: 0, dtype: object
        name        Task2
        file    task2.txt
        dependencies    NaN
        logfile      None
        status    planned
        Name: 1, dtype: object

    """

    # Return an iterator for the rows of the DataFrame
    for _, row in self.payload_items.iterrows():
        yield row

__len__()

Return the number of items in the PayloadList.

Returns:

Name Type Description
int int

The count of items in the list.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def __len__(self) -> int:
    """Return the number of items in the PayloadList.

    Returns:
        int:
            The count of items in the list.

    """

    return len(self.payload_items)

__repr__()

Return a string representation of the PayloadList for logging and debugging.

Returns:

Name Type Description
str str

A string representing the items in the DataFrame.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def __repr__(self) -> str:
    """Return a string representation of the PayloadList for logging and debugging.

    Returns:
        str:
            A string representing the items in the DataFrame.

    """

    return self.payload_items.to_string(index=True)

__setitem__(index, value)

Set an item at the specified index using the "[index]" syntax.

Parameters:

Name Type Description Default
index int

The index to set the item at.

required
value dict

The item dictionary to set, which must include 'name' and 'file' keys.

required

Raises:

Type Description
IndexError

If the index is out of range.

ValueError

If the provided value is not a valid item dictionary.

Example

payload_list = PayloadList() payload_list.add_item("Task1", "task1.txt") payload_list[0] name Task1 filename task1.txt dependencies NaN logfile None status planned Name: 0, dtype: object payload_list[0] = {"name": "Updated Task1", "file": "updated_task1.txt", "status": "completed"} payload_list[0] name Updated Task1 filename updated_task1.txt dependencies NaN logfile None status completed Name: 0, dtype: object

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def __setitem__(self, index: int, value: dict) -> None:
    """Set an item at the specified index using the "[index]" syntax.

    Args:
        index (int): The index to set the item at.
        value (dict): The item dictionary to set, which must include 'name' and 'file' keys.

    Raises:
        IndexError: If the index is out of range.
        ValueError: If the provided value is not a valid item dictionary.

    Example:
        >>> payload_list = PayloadList()
        >>> payload_list.add_item("Task1", "task1.txt")
        >>> payload_list[0]
        name        Task1
        filename    task1.txt
        dependencies    NaN
        logfile      None
        status    planned
        Name: 0, dtype: object
        >>> payload_list[0] = {"name": "Updated Task1", "file": "updated_task1.txt", "status": "completed"}
        >>> payload_list[0]
        name        Updated Task1
        filename    updated_task1.txt
        dependencies    NaN
        logfile      None
        status    completed
        Name: 0, dtype: object

    """

    if not {"name", "filename"}.issubset(value):
        exception = ("Value must be a dictionary with at least 'name' and 'filename' keys",)
        raise ValueError(
            exception,
        )

    if index not in self.payload_items.index:
        exception = "Index -> {} is out of range".format(index)
        raise IndexError(exception)

    self.payload_items.loc[index] = value

add_payload_item(name, filename, logfile, dependencies=None, status='pending', enabled=True, git_url=None, loglevel='INFO', customizer_settings=None)

Add a new item to the PayloadList.

Parameters:

Name Type Description Default
name str

The name of the item.

required
filename str

The file associated with the item.

required
logfile str

Log file information for the item. Defaults to None.

required
dependencies list

The index of another item this item depends on. Defaults to None.

None
status str

The status of the item. Must be one of 'planned', 'running', 'completed', or 'failed'. Defaults to 'planned'.

'pending'
enabled bool

True if the payload is enabled. False otherwise.

True
git_url str

Link to the payload in the GIT repository.

None
loglevel str

The log level for processing the payload. Either "INFO" or "DEBUG".

'INFO'
customizer_settings dict

Customizer settings for the payload. Defaults to None.

None
Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def add_payload_item(
    self,
    name: str,
    filename: str,
    logfile: str,
    dependencies: list | None = None,
    status: str = "pending",
    enabled: bool = True,
    git_url: str | None = None,
    loglevel: str = "INFO",
    customizer_settings: dict | None = None,
) -> dict:
    """Add a new item to the PayloadList.

    Args:
        name (str):
            The name of the item.
        filename (str):
            The file associated with the item.
        logfile (str):
            Log file information for the item. Defaults to None.
        dependencies (list):
            The index of another item this item depends on. Defaults to None.
        status (str):
            The status of the item. Must be one of 'planned', 'running',
            'completed', or 'failed'. Defaults to 'planned'.
        enabled (bool):
            True if the payload is enabled. False otherwise.
        git_url (str):
            Link to the payload in the GIT repository.
        loglevel (str):
            The log level for processing the payload. Either "INFO" or "DEBUG".
        customizer_settings (dict):
            Customizer settings for the payload. Defaults to None.

    """

    new_item = {
        "name": name if name else filename,
        "filename": filename,
        "dependencies": dependencies if dependencies else [],
        "logfile": logfile,
        "status": status,
        "enabled": enabled,
        "git_url": git_url,
        "loglevel": loglevel,
        "log_debug": 0,
        "log_info": 0,
        "log_warning": 0,
        "log_error": 0,
        "log_critical": 0,
        "customizer_settings": customizer_settings if customizer_settings else {},
    }
    self.payload_items = pd.concat(
        [self.payload_items, pd.DataFrame([new_item])],
        ignore_index=True,
    )

    new_item = self.payload_items.tail(1).to_dict(orient="records")[0]
    new_item["index"] = self.payload_items.index[-1]

    return new_item

calculate_payload_item_duration()

Update the dataframe column "duration" for all running items.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def calculate_payload_item_duration(self) -> None:
    """Update the dataframe column "duration" for all running items."""

    def calculate_duration(row: pd.Series) -> str:
        if row["status"] == "running":
            now = datetime.now(UTC)
            start_time = pd.to_datetime(row["start_time"])

            duration = now - start_time
            hours, remainder = divmod(duration.total_seconds(), 3600)
            minutes, seconds = divmod(remainder, 60)
            formatted_duration = f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}"

            return formatted_duration
        else:
            return str(row["duration"])  # or whatever the original value should be

    # updates the "duration" column of the DataFrame self.payload_items
    # by applying the method calculate_duration() to each row:
    self.payload_items["duration"] = self.payload_items.apply(
        calculate_duration,
        axis=1,
    )

get_payload_item(index)

Get the payload item by index if it exists, otherwise return None.

Parameters:

Name Type Description Default
index int

index of the row

required

Returns:

Type Description
Series

pd.Series: row with the matching index or None if there is no row with that index

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def get_payload_item(self, index: int) -> pd.Series:
    """Get the payload item by index if it exists, otherwise return None.

    Args:
        index (int): index of the row

    Returns:
        pd.Series: row with the matching index or None if there is no row with that index

    """

    self.calculate_payload_item_duration()

    if index not in self.payload_items.index:
        self.logger.error("Index -> %s is out of range", str(index))
        return None

    return self.payload_items.loc[index]

get_payload_item_by_name(name)

Get the payload item by name if it exists, otherwise return None.

Parameters:

Name Type Description Default
name str

The name of the payload.

required

Returns:

Type Description
Series

pd.Series: Row with the matching name or None if there is no row with that index

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def get_payload_item_by_name(self, name: str) -> pd.Series:
    """Get the payload item by name if it exists, otherwise return None.

    Args:
        name (str):
            The name of the payload.

    Returns:
        pd.Series:
            Row with the matching name or None if there is no row with that index

    """

    self.calculate_payload_item_duration()

    df = self.get_payload_items()
    data = [{"index": idx, **row} for idx, row in df.iterrows()]

    return next((item for item in data if item.get("name") == name), None)

get_payload_items()

Get the payload items in their current order in the PayloadList.

Returns:

Type Description
DataFrame

pd.DataFrame: A data frame containing all items in their current order.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def get_payload_items(self) -> pd.DataFrame:
    """Get the payload items in their current order in the PayloadList.

    Returns:
        pd.DataFrame:
            A data frame containing all items in their current order.

    """

    self.calculate_payload_item_duration()

    return self.payload_items

get_payload_items_by_value(column, value)

Filter the PayloadList by a given value in a specific column.

Parameters:

Name Type Description Default
column str

The column to filter by.

required
value str

The value to match in the specified column.

required

Returns:

Type Description
DataFrame | None

pd.DataFrame: A DataFrame containing rows where the given column matches the value.

Example

payload_list = PayloadList() payload_list.add_item("Task1", "task1.txt", status="running") payload_list.add_item("Task2", "task2.txt", status="completed") payload_list.add_item("Task3", "task3.txt", status="running") payload_list.get_payload_items_by_value(column="status", value="running") name file dependencies logfile status enabled 0 Task1 task1.txt NaN None running True 2 Task3 task3.txt NaN None running True

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def get_payload_items_by_value(
    self,
    column: str,
    value: str,
) -> pd.DataFrame | None:
    """Filter the PayloadList by a given value in a specific column.

    Args:
        column (str):
            The column to filter by.
        value (str):
            The value to match in the specified column.

    Returns:
        pd.DataFrame: A DataFrame containing rows where the given column matches the value.

    Example:
        >>> payload_list = PayloadList()
        >>> payload_list.add_item("Task1", "task1.txt", status="running")
        >>> payload_list.add_item("Task2", "task2.txt", status="completed")
        >>> payload_list.add_item("Task3", "task3.txt", status="running")
        >>> payload_list.get_payload_items_by_value(column="status", value="running")
            name         file    dependencies  logfile   status    enabled
        0   Task1    task1.txt        NaN        None   running     True
        2   Task3    task3.txt        NaN        None   running     True

    """

    if column not in self.payload_items.columns:
        self.logger.error(
            "Column -> '%s' does not exist in the payload list!",
            str(column),
        )
        return None

    filtered_items = self.payload_items[self.payload_items[column] == value]

    return filtered_items

move_payload_item_down(index)

Move an item down by one position in the PayloadList.

Parameters:

Name Type Description Default
index int

The index of the item to move down.

required

Returns:

Name Type Description
int int | None

The new position of the payload item.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def move_payload_item_down(self, index: int) -> int | None:
    """Move an item down by one position in the PayloadList.

    Args:
        index (int):
            The index of the item to move down.

    Returns:
        int:
            The new position of the payload item.

    """

    if index < 0 or index >= len(self.payload_items) - 1:
        self.logger.error(
            "Index -> %s is out of range or already at the bottom!",
            str(index),
        )
        return None

    self.payload_items.iloc[[index, index + 1]] = self.payload_items.iloc[[index + 1, index]].to_numpy()

    new_postion = self.payload_items.index.get_loc(index)

    return new_postion

move_payload_item_up(index)

Move an item up by one position in the PayloadList.

Parameters:

Name Type Description Default
index int

The index of the item to move up.

required
Results

bool: False, if the index is out of range or the item is already at the top. True otherwise

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def move_payload_item_up(self, index: int) -> int | None:
    """Move an item up by one position in the PayloadList.

    Args:
        index (int): The index of the item to move up.

    Results:
        bool: False, if the index is out of range or the item is already at the top.
              True otherwise

    """

    if index <= 0 or index >= len(self.payload_items):
        self.logger.error(
            "Index -> %s is out of range or already at the top!",
            str(index),
        )
        return None

    self.payload_items.iloc[[index - 1, index]] = self.payload_items.iloc[[index, index - 1]].to_numpy()

    new_postion = self.payload_items.index.get_loc(index)

    return new_postion

pick_runnables()

Pick all PayloadItems with status "planned" and no dependencies on items that are not in status "completed".

Returns:

Type Description
DataFrame

pd.DataFrame: A list of runnable payload items.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def pick_runnables(self) -> pd.DataFrame:
    """Pick all PayloadItems with status "planned" and no dependencies on items that are not in status "completed".

    Returns:
        pd.DataFrame:
            A list of runnable payload items.

    """

    def is_runnable(row: pd.Series) -> bool:
        # Check if item is enabled:
        if not row["enabled"]:
            return False

        # Check if all dependencies have been completed
        dependencies: list[int] = row["dependencies"]

        return all(self.payload_items.loc[dep, "status"] == "completed" for dep in dependencies or [])

    # end sub-method definition

    if self.payload_items.empty:
        return None

    # Filter payload items to find runnable items
    runnable_df: pd.DataFrame = self.payload_items[
        (self.payload_items["status"] == "planned") & self.payload_items.apply(is_runnable, axis=1)
    ].copy()

    # Add index as a column to the resulting DataFrame
    runnable_df["index"] = runnable_df.index

    # Log each runnable item
    for _, row in runnable_df.iterrows():
        self.logger.debug(
            "Added payload file -> '%s' with index -> %s to runnable queue.",
            row["name"] if row["name"] else row["filename"],
            row["index"],
        )

    return runnable_df

pick_running()

Pick all PayloadItems with status "running".

Returns:

Type Description
int

pd.DataFrame: A list of running payload items.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def pick_running(self) -> int:
    """Pick all PayloadItems with status "running".

    Returns:
        pd.DataFrame:
            A list of running payload items.

    """

    if self.payload_items.empty:
        return 0

    all_status = self.payload_items["status"].value_counts().to_dict()

    return all_status.get("running", 0)

process_payload_list(concurrent=None)

Process runnable payloads.

Parameters:

Name Type Description Default
concurrent int | None

The maximum number of concurrent payloads to run at any given time.

None

Continuously checks for runnable payload items and starts their "process_payload" method in separate threads. Runs as a daemon until the customizer ends.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def process_payload_list(self, concurrent: int | None = None) -> None:
    """Process runnable payloads.

    Args:
        concurrent (int | None, optional):
            The maximum number of concurrent payloads to run at any given time.

    Continuously checks for runnable payload items and starts their
    "process_payload" method in separate threads.
    Runs as a daemon until the customizer ends.

    """

    @tracer.start_as_current_span("run_and_complete_payload")
    def run_and_complete_payload(payload_item: pd.Series) -> None:
        """Run the payload's process_payload method and marks the status as completed afterward."""

        t = trace.get_current_span()
        t.set_attributes({"payload_id": payload_item["index"], "payload_name": payload_item["name"]})

        start_time = datetime.now(UTC)
        self.update_payload_item(payload_item["index"], {"start_time": start_time})

        # Create a logger with thread_id:
        thread_logger = logging.getLogger(
            name="Payload_{}".format(payload_item["index"]),
        )

        thread_logger.setLevel(level=payload_item["loglevel"])

        # Check if the logger already has handlers. If it does, they are removed before creating new ones.
        if thread_logger.hasHandlers():
            thread_logger.handlers.clear()

        # Create a handler for the logger:
        handler = logging.FileHandler(filename=payload_item.logfile)

        # Create a formatter:
        formatter = logging.Formatter(
            fmt="%(asctime)s %(levelname)s [%(name)s] [%(threadName)s] %(message)s",
            datefmt="%d-%b-%Y %H:%M:%S",
        )
        # Add the formatter to the handler
        handler.setFormatter(fmt=formatter)
        thread_logger.addHandler(hdlr=handler)

        if len(thread_logger.filters) == 0:
            thread_logger.debug("Adding log count filter to logger")
            thread_logger.addFilter(
                LogCountFilter(
                    payload_items=self.payload_items,
                    index=payload_item["index"],
                ),
            )

        thread_logger.info(
            "Start processing of payload -> '%s' (%s) from filename -> '%s'",
            payload_item["name"],
            payload_item["index"],
            payload_item["filename"],
        )

        local = threading.local()

        # Read customizer Settings from customizerSettings in the payload:
        payload = load_payload(payload_item["filename"])

        if not payload:
            success = False

        if payload:
            customizer_settings = payload_item["customizer_settings"]

            # Overwrite the customizer settings with the payload specific ones:
            customizer_settings.update(
                {
                    "cust_payload": payload_item["filename"],
                    "cust_payload_gz": "",
                    "cust_payload_external": "",
                    "cust_log_file": payload_item.logfile,
                },
            )

            try:
                local.customizer_thread_object = Customizer(
                    settings=customizer_settings,
                    logger=thread_logger,
                )
                thread_logger.info("Customizer initialized successfully.")

                thread_logger.debug(
                    "Customizer Settings -> \n %s",
                    pprint.pformat(
                        local.customizer_thread_object.settings.model_dump(),
                    ),
                )

                if customizer_settings.get("profiling", False):
                    import cProfile
                    import pstats

                    cprofiler = cProfile.Profile()
                    cprofiler.enable()

                success = local.customizer_thread_object.customization_run()

                if customizer_settings.get("profiling", False):
                    cprofiler.disable()

                now = datetime.now(UTC)
                log_path = os.path.dirname(payload_item.logfile)
                profile_log_prefix = (
                    f"{log_path}/{payload_item['index']}_{payload_item['name']}_{now.strftime('%Y-%m-%d_%H-%M-%S')}"
                )

                if customizer_settings.get("profiling", False):
                    import io

                    s = io.StringIO()
                    stats = pstats.Stats(cprofiler, stream=s).sort_stats("cumtime")
                    stats.print_stats()
                    with open(f"{profile_log_prefix}.log", "w+") as f:
                        f.write(s.getvalue())
                    stats.dump_stats(filename=f"{profile_log_prefix}.cprof")

            except ValidationError:
                thread_logger.error("Validation error!")
                success = False

            except StopOnError:
                success = False
                thread_logger.error(
                    "StopOnErrorException occurred. Stopping payload processing...",
                )

            except Exception:
                success = False
                thread_logger.error(
                    "An exception occurred: \n%s",
                    traceback.format_exc(),
                )

        if not success:
            thread_logger.error(
                "Failed to initialize payload -> '%s'!",
                payload_item["filename"],
            )
            # Update the status to "failed" in the DataFrame after processing finishes
            self.update_payload_item(payload_item["index"], {"status": "failed"})

        else:
            # Update the status to "completed" in the DataFrame after processing finishes
            self.update_payload_item(payload_item["index"], {"status": "completed"})

        stop_time = datetime.now(UTC)
        duration = stop_time - start_time

        # Format duration in hh:mm:ss
        hours, remainder = divmod(duration.total_seconds(), 3600)
        minutes, seconds = divmod(remainder, 60)
        formatted_duration = f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}"

        self.update_payload_item(
            payload_item["index"],
            {"stop_time": stop_time, "duration": formatted_duration},
        )

    # end  def run_and_complete_payload()

    # add delay here to allow for logging to work reliably for the the first payload
    time.sleep(10)

    while not self._stopped:
        # Get runnable items as subset of the initial data frame:
        runnable_items: pd.DataFrame = self.pick_runnables()

        # Start a thread for each runnable item (item is a pd.Series)
        if runnable_items is not None:
            for _, item in runnable_items.iterrows():
                if concurrent and self.pick_running() >= concurrent:
                    self.logger.debug(
                        "Reached concurrency limit of %s payloads. Waiting for one to finish.",
                    )
                    break

                self.logger.info(
                    "Added payload file -> '%s' with index -> %s to runnable queue.",
                    item["name"] if item["name"] else item["filename"],
                    item["index"],
                )

                # Update the status to "running" in the data frame to prevent re-processing
                self.payload_items.loc[
                    self.payload_items["name"] == item["name"],
                    "status",
                ] = "running"

                # Start the process_payload method in a new thread
                thread = threading.Thread(
                    target=run_and_complete_payload,
                    args=(item,),
                    name=item["name"],
                )
                thread.start()
                break

        # Sleep briefly to avoid a busy wait loop
        time.sleep(1)

remove_payload_item(index)

Remove an item by its index from the PayloadList.

Parameters:

Name Type Description Default
index int

The index of the item to remove.

required

Returns:

Name Type Description
bool bool

True = success. False = failure.

Raises:

Type Description
IndexError

If the index is out of range.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def remove_payload_item(self, index: int) -> bool:
    """Remove an item by its index from the PayloadList.

    Args:
        index (int):
            The index of the item to remove.

    Returns:
        bool:
            True = success. False = failure.

    Raises:
        IndexError: If the index is out of range.

    """

    if index not in self.payload_items.index:
        self.logger.error("Index -> %s is out of range!", index)
        return False

    self.payload_items.drop(index, inplace=True)

    return True

run_payload_processing(concurrent=None)

Start the process_payload_list method in a daemon thread.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def run_payload_processing(self, concurrent: int | None = None) -> None:
    """Start the `process_payload_list` method in a daemon thread."""

    scheduler_thread = threading.Thread(
        target=self.process_payload_list,
        daemon=True,
        name="Scheduler",
        kwargs={"concurrent": concurrent},
    )

    self.logger.info(
        "Starting '%s' thread for payload list processing...",
        str(scheduler_thread.name),
    )
    self._stopped = False
    scheduler_thread.start()

stop_payload_processing()

Set a stop flag which triggers the stopping of further payload processing.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def stop_payload_processing(self) -> None:
    """Set a stop flag which triggers the stopping of further payload processing."""

    self._stopped = True

update_payload_item(index, update_data)

Update an existing item in the PayloadList.

Parameters:

Name Type Description Default
index int

The position of the payload.

required
update_data str

The data of the item.

required

Returns:

Name Type Description
bool bool

True = success, False = error.

Source code in packages/pyxecm/src/pyxecm_customizer/payload_list.py
def update_payload_item(
    self,
    index: int,
    update_data: dict,
) -> bool:
    """Update an existing item in the PayloadList.

    Args:
        index (int):
            The position of the payload.
        update_data (str):
            The data of the item.

    Returns:
        bool:
            True = success, False = error.

    """

    if index not in self.payload_items.index:
        self.logger.error("Illegal index -> %s for payload update!", index)
        return False

    for column in self.payload_items.columns:
        if column in update_data:
            tmp = self.payload_items.loc[index].astype(object)
            tmp[column] = update_data[column]
            self.payload_items.loc[index] = tmp

    return True