This file is indexed.

/usr/share/otrs/scripts/DBUpdate-to-4.pl is in otrs2 5.0.7-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 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
 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
#!/usr/bin/perl
# --
# DBUpdate-to-4.pl - update script to migrate OTRS 3.3.x to 4.0.x
# Copyright (C) 2001-2015 OTRS AG, http://otrs.com/
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --

use strict;
use warnings;

# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . '/Kernel/cpan-lib';

use Getopt::Std qw();
use Kernel::Config;
use Kernel::Output::Template::Provider;
use Kernel::System::ObjectManager;
use Kernel::System::SysConfig;
use Kernel::System::Cache;
use Kernel::System::DynamicField;
use Kernel::System::Package;
use Kernel::System::ProcessManagement::DB::Activity;
use Kernel::System::ProcessManagement::DB::ActivityDialog;
use Kernel::System::ProcessManagement::DB::Entity;
use Kernel::System::ProcessManagement::DB::Process;
use Kernel::System::ProcessManagement::DB::Transition;
use Kernel::System::ProcessManagement::DB::TransitionAction;
use Kernel::System::VariableCheck qw(:all);

local $Kernel::OM = Kernel::System::ObjectManager->new(
    'Kernel::System::Log' => {
        LogPrefix => 'OTRS-DBUpdate-to-4.pl',
    },
);

# define a global variable to store process management lookup old entity IDs and new GUIDs, so it
# can be used by more than one function.
my %EntityLookup;

{

    # get options
    my %Opts;
    Getopt::Std::getopt( 'h', \%Opts );

    if ( exists $Opts{h} ) {
        print <<"EOF";

DBUpdate-to-4.pl - Upgrade script for OTRS 3.3 to 4 migration.
Copyright (C) 2001-2014 OTRS AG, http://otrs.com/

Usage: $0 [-h]
    Options are as follows:
        -h      display this help

EOF
        exit 1;
    }

    # UID check if not on Windows
#    if ( $^O ne 'MSWin32' && $> == 0 ) {    # $EFFECTIVE_USER_ID
#        die "
#Cannot run this program as root.
#Please run it as the 'otrs' user or with the help of su:
#    su -c \"$0\" -s /bin/bash otrs
#";
#    }

    # enable autoflushing of STDOUT
    $| = 1;                                 ## no critic

    print "\nMigration started...\n\n";

    # define the number of steps
    my $Steps = 15;
    my $Step  = 1;

    print "Step $Step of $Steps: Refresh configuration cache... ";
    RebuildConfig() || die;
    print "done.\n\n";
    $Step++;

    # create common objects with new default config
    $Kernel::OM->ObjectsDiscard();

    # check framework version
    print "Step $Step of $Steps: Check framework version... ";
    _CheckFrameworkVersion() || die;
    print "done.\n\n";
    $Step++;

    # migrate FontAwesome
    print "Step $Step of $Steps: Migrate FontAwesome icons... ";
    if ( _MigrateFontAwesome() ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    print "Step $Step of $Steps: Migrate ProcessManagement EntityIDs... ";
    if ( _MigrateProcessManagementEntityIDs() ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    # migrate ProcessManagement dynamic fields.
    print "Step $Step of $Steps: Migrate ProcessManagement Dynamic Field Types... ";
    if ( _MigrateProcessManagementDynamicFieldTypes() ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    # migrate DB ACLs
    print "Step $Step of $Steps: Migrate ACLs stored in the DB... ";
    if ( _MigrateDBACLs() ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    # set service notifications
    print "Step $Step of $Steps: Set service notifications... ";
    if ( _SetServiceNotifications() ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    # migrate settings
    print "Step $Step of $Steps: Migrate Settings... ";
    if ( _MigrateSettings() ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    # uninstall Merged Feature Add-Ons
    print "Step $Step of $Steps: Uninstall Merged Feature Add-Ons... ";
    if ( _UninstallMergedFeatureAddOns() ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    print "Step $Step of $Steps: Migrate SysConfig settings from DT to Template::Toolkit... ";
    if ( _MigrateDTLInSysConfig() ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    print "Step $Step of $Steps: Migrate Dynamic Field links from DT to Template::Toolkit... ";
    if ( _MigrateDTLInDynamicFieldLinks() ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    print "Step $Step of $Steps: Migrate Generic Agent Process Management Dynamic Fields ... ";
    if ( _MigrateGenericAgentProcessManagementDynamicFields() ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    print
        "Step $Step of $Steps: Migrate Notification (Event) Process Management Dynamic Fields ... ";
    if ( _MigrateNotificationEventProcessManagementDynamicFields() ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    # Clean up the cache completely at the end.
    print "Step $Step of $Steps: Clean up the cache... ";
    $Kernel::OM->Get('Kernel::System::Cache')->CleanUp();
    print "done.\n\n";
    $Step++;

    print "Step $Step of $Steps: Refresh configuration cache another time... ";
    RebuildConfig() || die;
    print "done.\n\n";

    print "Migration completed!\n";

    exit 0;
}

=item RebuildConfig($CommonObject)

refreshes the configuration to make sure that a ZZZAAuto.pm is present
after the upgrade.

    RebuildConfig($CommonObject);

=cut

sub RebuildConfig {
    my $SysConfigObject = Kernel::System::SysConfig->new();

    # Rebuild ZZZAAuto.pm with current values
    if ( !$SysConfigObject->WriteDefault() ) {
        die "Error: Can't write default config files!";
    }

    # Force a reload of ZZZAuto.pm and ZZZAAuto.pm to get the new values
    for my $Module ( sort keys %INC ) {
        if ( $Module =~ m/ZZZAA?uto\.pm$/ ) {
            delete $INC{$Module};
        }
    }

    # reload config object
    print
        "\nIf you see warnings about 'Subroutine Load redefined', that's fine, no need to worry!\n";
    $Kernel::OM->ObjectsDiscard();

    return 1;
}

=item _CheckFrameworkVersion()

Check if framework it's the correct one for Dynamic Fields migration.

    _CheckFrameworkVersion();

=cut

sub _CheckFrameworkVersion {
    my $Home = $Kernel::OM->Get('Kernel::Config')->Get('Home');

    # load RELEASE file
    if ( -e !"$Home/RELEASE" ) {
        die "Error: $Home/RELEASE does not exist!";
    }
    my $ProductName;
    my $Version;
    if ( open( my $Product, '<', "$Home/RELEASE" ) ) {    ## no critic
        while (<$Product>) {

            # filtering of comment lines
            if ( $_ !~ /^#/ ) {
                if ( $_ =~ /^PRODUCT\s{0,2}=\s{0,2}(.*)\s{0,2}$/i ) {
                    $ProductName = $1;
                }
                elsif ( $_ =~ /^VERSION\s{0,2}=\s{0,2}(.*)\s{0,2}$/i ) {
                    $Version = $1;
                }
            }
        }
        close($Product);
    }
    else {
        die "Error: Can't read $Home/RELEASE: $!";
    }

    if ( $ProductName ne 'OTRS' ) {
        die "Error: No OTRS system found"
    }
#    if ( $Version !~ /^4\.0(.*)$/ ) {
#
#        die "Error: You are trying to run this script on the wrong framework version $Version!"
#    }

    return 1;
}

=item _MigrateFontAwesome()

Migrate settings that has changed it name.

    _MigrateFontAwesome($CommonObject);

=cut

sub _MigrateFontAwesome {
    my $SysConfigObject = $Kernel::OM->Get('Kernel::System::SysConfig');

    # update toolbar items settings
    # otherwise the new fontawesome icons won't be displayed

    # collect icon data for toolbar items
    my %ModuleAttributes = (
        '1-Ticket::AgentTicketQueue' => {
            'Icon' => 'fa fa-folder',
        },
        '2-Ticket::AgentTicketStatus' => {
            'Icon' => 'fa fa-list-ol',
        },
        '3-Ticket::AgentTicketEscalation' => {
            'Icon' => 'fa fa-exclamation',
        },
        '4-Ticket::AgentTicketPhone' => {
            'Icon' => 'fa fa-phone',
        },
        '5-Ticket::AgentTicketEmail' => {
            'Icon' => 'fa fa-envelope',
        },
        '6-Ticket::AgentTicketProcess' => {
            'Icon' => 'fa fa-th-large',
        },
        '6-Ticket::TicketResponsible' => {
            'Icon'        => 'fa fa-user',
            'IconNew'     => 'fa fa-user',
            'IconReached' => 'fa fa-user',
        },
        '7-Ticket::TicketWatcher' => {
            'Icon'        => 'fa fa-eye',
            'IconNew'     => 'fa fa-eye',
            'IconReached' => 'fa fa-eye',
        },
        '8-Ticket::TicketLocked' => {
            'Icon'        => 'fa fa-lock',
            'IconNew'     => 'fa fa-lock',
            'IconReached' => 'fa fa-lock',
        },
    );

    my $Setting = $Kernel::OM->Get('Kernel::Config')->Get('Frontend::ToolBarModule');

    TOOLBARMODULE:
    for my $ToolbarModule ( sort keys %ModuleAttributes ) {

        next TOOLBARMODULE if !IsHashRefWithData( $Setting->{$ToolbarModule} );

        # set icon and class infos
        for my $Attribute ( sort keys %{ $ModuleAttributes{$ToolbarModule} } ) {
            $Setting->{$ToolbarModule}->{$Attribute} = $ModuleAttributes{$ToolbarModule}->{$Attribute};
        }

        # set new setting,
        my $Success = $SysConfigObject->ConfigItemUpdate(
            Valid => 1,
            Key   => 'Frontend::ToolBarModule###' . $ToolbarModule,
            Value => $Setting->{$ToolbarModule},
        );
    }

    # collect icon data for customer user items
    my %CustomerUserAttributes = (
        '1-GoogleMaps' => {
            'IconName' => 'fa-globe',
        },
        '2-Google' => {
            'IconName' => 'fa-google',
        },
        '2-LinkedIn' => {
            'IconName' => 'fa-linkedin',
        },
        '3-XING' => {
            'IconName' => 'fa-xing',
        },
        '15-OpenTickets' => {
            'IconNameOpenTicket'   => 'fa-exclamation-circle',
            'IconNameNoOpenTicket' => 'fa-check-circle',
        },
        '16-OpenTicketsForCustomerUserLogin' => {
            'IconNameOpenTicket'   => 'fa-exclamation-circle',
            'IconNameNoOpenTicket' => 'fa-check-circle',
        },
        '17-ClosedTickets' => {
            'IconNameOpenTicket'   => 'fa-power-off',
            'IconNameNoOpenTicket' => 'fa-power-off',
        },
        '18-ClosedTicketsForCustomerUserLogin' => {
            'IconNameOpenTicket'   => 'fa-power-off',
            'IconNameNoOpenTicket' => 'fa-power-off',
        },
    );

    $Setting = $Kernel::OM->Get('Kernel::Config')->Get('Frontend::CustomerUser::Item');

    CUSTOMERUSERMODULE:
    for my $CustomerUserModule ( sort keys %CustomerUserAttributes ) {

        next CUSTOMERUSERMODULE if !IsHashRefWithData( $Setting->{$CustomerUserModule} );

        # set icon and class infos
        for my $Attribute ( sort keys %{ $CustomerUserAttributes{$CustomerUserModule} } ) {
            $Setting->{$CustomerUserModule}->{$Attribute} = $CustomerUserAttributes{$CustomerUserModule}->{$Attribute};
        }

        # set new setting,
        my $Success = $SysConfigObject->ConfigItemUpdate(
            Valid => 1,
            Key   => 'Frontend::CustomerUser::Item###' . $CustomerUserModule,
            Value => $Setting->{$CustomerUserModule},
        );
    }

    return 1;
}

=item _MigrateProcessManagementEntityIDs()

Migrate process management EntityIDs from consecutive to GUID style.

    _MigrateProcesses($CommonObject);

=cut

sub _MigrateProcessManagementEntityIDs {
    my $ProcessObject          = Kernel::System::ProcessManagement::DB::Process->new();
    my $EntityObject           = Kernel::System::ProcessManagement::DB::Entity->new();
    my $ActivityObject         = Kernel::System::ProcessManagement::DB::Activity->new();
    my $ActivityDialogObject   = Kernel::System::ProcessManagement::DB::ActivityDialog->new();
    my $TransitionObject       = Kernel::System::ProcessManagement::DB::Transition->new();
    my $TransitionActionObject = Kernel::System::ProcessManagement::DB::TransitionAction->new();

    # get current process management data from the DB
    my %ProcessManagementList;
    $ProcessManagementList{Process} = $ProcessObject->ProcessListGet(
        UserID => 1,
    );
    $ProcessManagementList{Activity} = $ActivityObject->ActivityListGet(
        UserID => 1,
    );
    $ProcessManagementList{ActivityDialog} = $ActivityDialogObject->ActivityDialogListGet(
        UserID => 1,
    );
    $ProcessManagementList{Transition} = $TransitionObject->TransitionListGet(
        UserID => 1,
    );
    $ProcessManagementList{TransitionAction} = $TransitionActionObject->TransitionActionListGet(
        UserID => 1,
    );

    # generate new EntityIDs and create a lookup table
    for my $Part (qw(Process Activity ActivityDialog Transition TransitionAction)) {
        my %PartList = map { $_->{EntityID} => $_ } @{ $ProcessManagementList{$Part} };

        ENTITYID:
        for my $EntityID ( sort keys %PartList ) {

            next ENTITYID if !$EntityID;
            next ENTITYID if $EntityID =~ m{\A $Part - [0-9a-f]{32} \z}msx;

            my $NewEntityID = $EntityObject->EntityIDGenerate(
                EntityType => $Part,
                UserID     => 1,
            );
            $EntityLookup{$Part}->{$EntityID} = $NewEntityID;
        }
    }

    # print EntityIDs migration table
    my $Output = "\n Process EntityIDs Migration Reference\n";
    for my $Part (qw(Process Activity ActivityDialog Transition TransitionAction)) {
        my $DisplayPart = uc $Part;
        if ( $Part eq 'ActivityDialog' ) {
            $DisplayPart = 'ACTIVITY DIALOG';
        }
        elsif ( $Part eq 'TransitionAction' ) {
            $DisplayPart = 'TRANSITION ACTION';
        }
        $Output .= "  $DisplayPart:\n";
        for my $OldEntitiy ( sort keys %{ $EntityLookup{$Part} } ) {
            $Output .= '   ' . sprintf '%-8s', $OldEntitiy;
            $Output .= "$EntityLookup{$Part}->{$OldEntitiy}\n";
        }
        $Output .= "\n";
    }
    $Output .= "\n";
    print $Output;

    my $DBObject     = $Kernel::OM->Get('Kernel::System::DB');
    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

    # migrate processes
    my $ProcessDFName = $ConfigObject->Get('Process::DynamicFieldProcessManagementProcessID');

    my $ProcessDF = $Kernel::OM->Get('Kernel::System::DynamicField')->DynamicFieldGet(
        Name => $ProcessDFName,
    );

    PROCESS:
    for my $Process ( @{ $ProcessManagementList{Process} } ) {

        next PROCESS if $Process->{EntityID} =~ m{\A Process - [0-9a-f]{32} \z}msx;

        if ( !$EntityLookup{Process}->{ $Process->{EntityID} } ) {
            die "Error: No new EntityID was created for Process: $Process->{EntityID}";
        }

        my $OldentityID = $Process->{EntityID};

        # set new process EntityID:
        $Process->{EntityID} = $EntityLookup{Process}->{ $Process->{EntityID} };

        # remove not needed information
        # those are defined inside the configuration
        for my $ProcessPart (qw(Activities Transitions TransitionActions)) {
            delete $Process->{$ProcessPart};
        }

        # update layout:
        my %NewLayout;
        for my $ActivityEntityID ( sort keys %{ $Process->{Layout} } ) {

            my $NewActivityEntityID = $EntityLookup{Activity}->{$ActivityEntityID};

            if ($NewActivityEntityID) {
                $NewLayout{$NewActivityEntityID} = $Process->{Layout}->{$ActivityEntityID};
            }
            else {
                print "\n Warning: Process '$Process->{EntityID} - $Process->{Name}' layout contains a reference"
                    . " to a non existing Activity '$ActivityEntityID', this activity will be skipped."
                    . " Please check the process after the migration process ends\n";
            }
        }
        $Process->{Layout} = \%NewLayout;

        # update config
        ATTRIBUTE:
        for my $Attribute (qw(Activity ActivityDialog)) {
            next ATTRIBUTE if !$Process->{Config}->{"Start$Attribute"};

            my $AttributeEntityID    = $Process->{Config}->{"Start$Attribute"};
            my $NewAttributeEntityID = $EntityLookup{$Attribute}->{$AttributeEntityID};
            if ( !$NewAttributeEntityID ) {
                die "Error: No new EntityID was created for $Attribute: $AttributeEntityID";
            }
            $Process->{Config}->{"Start$Attribute"} = $NewAttributeEntityID;
        }

        # set process path
        my %NewPath;
        for my $ActivityEntityID ( sort keys %{ $Process->{Config}->{Path} } ) {

            # set new activity EntityID in process path
            my $NewActivityEntityID = $EntityLookup{Activity}->{$ActivityEntityID};
            if ( !$NewActivityEntityID ) {
                die "Error: No new EntityID was created for Activity: $ActivityEntityID";
            }

            $NewPath{$NewActivityEntityID} = {};

            # check if original action has configuration (e.g. last activity might be empty)
            my $Activity = $Process->{Config}->{Path}->{$ActivityEntityID};

            if ( IsHashRefWithData($Activity) ) {
                for my $TransitionEntityID ( sort keys %{$Activity} ) {
                    my $Transition = $Activity->{$TransitionEntityID};
                    my $NewTransition;

                    TRANSITIONACTION:
                    for my $TransitionActionEntityID ( @{ $Transition->{TransitionAction} } ) {

                        # set new transition action EntityID from process path activity transition
                        my $NewTransitionActionEntityID = $EntityLookup{TransitionAction}->{$TransitionActionEntityID};

                        # due to a bug in ProcessManagement GUI it could be that there are
                        # references to Transition Actions that no longer exists, see bug 11021.
                        # migration script must be tolerant on this issue and do not stop if there
                        # is a non existing Transition Action, but skip it instead.
                        if ( !$NewTransitionActionEntityID ) {
                            print "\n Warning: Process '$Process->{EntityID} - $Process->{Name}'"
                                . " path contains a reference to a non existing Transition Action"
                                . " '$TransitionActionEntityID', this Transition Action will be"
                                . " skipped. Please check the process after the migration process"
                                . " ends\n";
                            next TRANSITIONACTION;
                        }
                        push @{ $NewTransition->{TransitionAction} }, $NewTransitionActionEntityID;
                    }

                    # set new activity EntityID stored in the transition
                    my $NewDestinationActivityEntityID = $EntityLookup{Activity}->{ $Transition->{ActivityEntityID} };
                    if ( !$NewDestinationActivityEntityID ) {
                        die
                            "Error: No new EntityID was created for Activity: $Transition->{ActivityEntityID}";
                    }
                    $NewTransition->{ActivityEntityID} = $NewDestinationActivityEntityID;

                    # set new transition EntityID
                    my $NewTransitionEntityID = $EntityLookup{Transition}->{$TransitionEntityID};
                    if ( !$NewTransitionEntityID ) {
                        die
                            "Error: No new EntityID was created for Transition: $TransitionEntityID";
                    }

                    # set new transition to its entity hash key
                    $NewPath{$NewActivityEntityID}->{$NewTransitionEntityID} = $NewTransition;
                }
            }
        }
        $Process->{Config}->{Path} = \%NewPath;

        # update dynamic fields
        return if !$DBObject->Do(
            SQL => '
                UPDATE dynamic_field_value
                SET value_text = ?
                WHERE field_id = ?
                    AND value_text =?',
            Bind => [ \$Process->{EntityID}, \$ProcessDF->{ID}, \$OldentityID, ],
        );
    }

    # migrate activities
    my $ActivityDFName = $ConfigObject->Get('Process::DynamicFieldProcessManagementActivityID');

    my $ActivityDF = $Kernel::OM->Get('Kernel::System::DynamicField')->DynamicFieldGet(
        Name => $ActivityDFName,
    );

    ACTIVITY:
    for my $Activity ( @{ $ProcessManagementList{Activity} } ) {

        next ACTIVITY if $Activity->{EntityID} =~ m{\A Activity - [0-9a-f]{32} \z}msx;

        if ( !$EntityLookup{Activity}->{ $Activity->{EntityID} } ) {
            die "Error: No new EntityID was created for Activity: $Activity->{EntityID}";
        }

        my $OldentityID = $Activity->{EntityID};

        # set new Activity EntityID:
        $Activity->{EntityID} = $EntityLookup{Activity}->{ $Activity->{EntityID} };

        # remove not needed information
        # activity dialogs are defined inside the configuration
        delete $Activity->{ActivityDialogs};

        # set new entities for the configured activity dialogs
        my $CurrentActivityDialogs = $Activity->{Config}->{ActivityDialog};
        for my $OrderKey ( sort keys %{$CurrentActivityDialogs} ) {

            # get old activity dialog EntityID
            my $ActivityDialogEntityID = $CurrentActivityDialogs->{$OrderKey};

            # set new activity dialog EntityID
            my $NewActivityDialogEntityID = $EntityLookup{ActivityDialog}->{$ActivityDialogEntityID};
            if ( !$NewActivityDialogEntityID ) {
                die
                    "Error: No new EntityID was created for ActivityDialog: $ActivityDialogEntityID";
            }
            $Activity->{Config}->{ActivityDialog}->{$OrderKey} = $NewActivityDialogEntityID;
        }

        # update dynamic fields
        return if !$DBObject->Do(
            SQL => '
                UPDATE dynamic_field_value
                SET value_text = ?
                WHERE field_id = ?
                    AND value_text =?',
            Bind => [ \$Activity->{EntityID}, \$ActivityDF->{ID}, \$OldentityID, ],
        );
    }

    # migrate all other process parts
    for my $PartName (qw(ActivityDialog Transition TransitionAction)) {

        CURRENTPART:
        for my $CurrentPart ( @{ $ProcessManagementList{$PartName} } ) {

            next CURRENTPART if $CurrentPart->{EntityID} =~ m{\A $PartName - [0-9a-f]{32} \z}msx;

            if ( !$EntityLookup{$PartName}->{ $CurrentPart->{EntityID} } ) {
                die "Error: No new EntityID was created for $PartName: $CurrentPart->{EntityID}";
            }

            # set new Activity EntityID:
            $CurrentPart->{EntityID} = $EntityLookup{$PartName}->{ $CurrentPart->{EntityID} };
        }
    }

    # migrate ACLs
    my $ACLObject = $Kernel::OM->Get('Kernel::System::ACL::DB::ACL');

    # get a list of all ACLs stored in the DB
    my $ACLList = $ACLObject->ACLListGet(
        UserID => 1,
    );

    my @AffectedDBACLs;

    # check if update is needed
    ACL:
    for my $ACL ( @{$ACLList} ) {

        # skip ACLs that does not include ActivityDialogs and Processes in the ConfigChange or
        #    processes in ConfigMatch
        if ( ref $ACL->{ConfigMatch} ne 'HASH' ) {
            $ACL->{ConfigMatch} = {};
        }

        if ( ref $ACL->{ConfigChange} ne 'HASH' ) {
            $ACL->{ConfigChange} = {};
        }

        if (
            !IsHashRefWithData( $ACL->{ConfigMatch} )
            && !IsHashRefWithData( $ACL->{ConfigChange} )
            )
        {
            next ACL;
        }

        for my $Part (qw(Properties PropertiesDatabase)) {
            if ( ref $ACL->{ConfigMatch}->{$Part} ne 'HASH' ) {
                $ACL->{ConfigMatch}->{$Part} = {};
            }
        }

        for my $Part (qw(Possible PossibleNot)) {
            if ( ref $ACL->{ConfigChange}->{$Part} ne 'HASH' ) {
                $ACL->{ConfigChange}->{$Part} = {};
            }
        }

        if (
            !$ACL->{ConfigMatch}->{Properties}->{Process}
            && !$ACL->{ConfigMatch}->{PropertiesDatabase}->{Process}
            && !$ACL->{ConfigChange}->{Possible}->{ActivityDialog}
            && !$ACL->{ConfigChange}->{PossibleNot}->{ActivityDialog}
            && !$ACL->{ConfigChange}->{Possible}->{Process}
            && !$ACL->{ConfigChange}->{PossibleNot}->{Process}
            )
        {
            next ACL;
        }
        push @AffectedDBACLs, $ACL->{Name};

        for my $ACLPart (qw(Properties PropertiesDatabase)) {
            if (
                IsArrayRefWithData( $ACL->{ConfigMatch}->{$ACLPart}->{Process}->{ProcessEntityID} )
                )
            {
                my @NewProcesses;

                ENTITY:
                for my $EntityID (
                    @{ $ACL->{ConfigMatch}->{$ACLPart}->{Process}->{ProcessEntityID} }
                    )
                {
                    next ENTITY if !$EntityID;

                    if ( $EntityID =~ m{\A Process - [0-9a-f]{32} \z}msx ) {
                        push @NewProcesses, $EntityID;
                        next ENTITY;
                    }

                    my $NewEntityID = $EntityLookup{Process}->{$EntityID};
                    if ( !$NewEntityID ) {
                        print
                            "\nError: ACL: '$ACL->{Name}' No new EntityID was created for Process: $EntityID, skipping...\n";
                        next ACL;
                    }
                    push @NewProcesses, $NewEntityID;
                }
                $ACL->{ConfigMatch}->{$ACLPart}->{Process}->{ProcessEntityID} = \@NewProcesses;
            }

            if (
                IsArrayRefWithData(
                    $ACL->{ConfigMatch}->{$ACLPart}->{Process}->{ActivityEntityID}
                )
                )
            {

                my @NewActivities;

                ENTITY:
                for my $EntityID (
                    @{ $ACL->{ConfigMatch}->{$ACLPart}->{Process}->{ActivityEntityID} }
                    )
                {

                    next ENTITY if !$EntityID;

                    if ( $EntityID =~ m{\A Activity - [0-9a-f]{32} \z}msx ) {
                        push @NewActivities, $EntityID;
                        next ENTITY;
                    }

                    my $NewEntityID = $EntityLookup{Activity}->{$EntityID};
                    if ( !$NewEntityID ) {
                        print
                            "\nError: ACL: '$ACL->{Name}' No new EntityID was created for Activity: $EntityID, skipping...\n";
                        next ACL;
                    }
                    push @NewActivities, $NewEntityID;
                }
                $ACL->{ConfigMatch}->{$ACLPart}->{Process}->{ActivityEntityID} = \@NewActivities;
            }

            if (
                IsArrayRefWithData(
                    $ACL->{ConfigMatch}->{$ACLPart}->{Process}->{ActivityDialogEntityID}
                )
                )
            {

                my @NewActivityDialogs;

                ENTITY:
                for my $EntityID (
                    @{ $ACL->{ConfigMatch}->{$ACLPart}->{Process}->{ActivityDialogEntityID} }
                    )
                {

                    next ENTITY if !$EntityID;

                    if ( $EntityID =~ m{\A ActivityDialog - [0-9a-f]{32} \z}msx ) {
                        push @NewActivityDialogs, $EntityID;
                        next ENTITY;
                    }

                    my $NewEntityID = $EntityLookup{ActivityDialog}->{$EntityID};
                    if ( !$NewEntityID ) {
                        print
                            "\nError: ACL: '$ACL->{Name}' Error: No new EntityID was created for ActivityDialog: $EntityID, skipping\n";
                    }
                    push @NewActivityDialogs, $NewEntityID;
                }
                $ACL->{ConfigMatch}->{$ACLPart}->{Process}->{ActivityDialogEntityID} = \@NewActivityDialogs;
            }
        }

        for my $ACLPart (qw(Possible PossibleNot)) {
            if ( IsArrayRefWithData( $ACL->{ConfigChange}->{$ACLPart}->{ActivityDialog} ) ) {
                my @NewActivityDialogs;

                ENTITY:
                for my $EntityID ( @{ $ACL->{ConfigChange}->{$ACLPart}->{ActivityDialog} } ) {

                    next ENTITY if !$EntityID;

                    if ( $EntityID =~ m{\A ActivityDialog - [0-9a-f]{32} \z}msx ) {
                        push @NewActivityDialogs, $EntityID;
                        next ENTITY;
                    }

                    my $NewEntityID = $EntityLookup{ActivityDialog}->{$EntityID};
                    if ( !$NewEntityID ) {
                        print
                            "\nError: ACL: '$ACL->{Name}' No new EntityID was created for ActivityDialog: $EntityID, skipping...\n";
                        next ACL;
                    }
                    push @NewActivityDialogs, $NewEntityID;
                }
                $ACL->{ConfigChange}->{$ACLPart}->{ActivityDialog} = \@NewActivityDialogs;
            }

            if ( IsArrayRefWithData( $ACL->{ConfigChange}->{$ACLPart}->{Process} ) ) {
                my @NewProcesses;

                ENTITY:
                for my $EntityID ( @{ $ACL->{ConfigChange}->{$ACLPart}->{Process} } ) {

                    next ENTITY if !$EntityID;

                    if ( $EntityID =~ m{\A Process - [0-9a-f]{32} \z}msx ) {
                        push @NewProcesses, $EntityID;
                        next ENTITY;
                    }

                    my $NewEntityID = $EntityLookup{Process}->{$EntityID};
                    if ( !$NewEntityID ) {
                        print
                            "\nError: ACL: '$ACL->{Name}' No new EntityID was created for Process: $EntityID, skipping...\n";
                        next ACL;
                    }
                    push @NewProcesses, $NewEntityID;
                }
                $ACL->{ConfigChange}->{$ACLPart}->{Process} = \@NewProcesses;
            }
        }
    }

    my $Error;

    my %ProcessManagementObjects = (
        Process          => $ProcessObject,
        Activity         => $ActivityObject,
        ActivityDialog   => $ActivityDialogObject,
        Transition       => $TransitionObject,
        TransitionAction => $TransitionActionObject
    );

    # get the list of updated or deleted entities
    my $EntitySyncStateList = $EntityObject->EntitySyncStateList(
        UserID => 1,
    );

    my $DeployProcesses = 1;
    if ( IsArrayRefWithData($EntitySyncStateList) ) {
        print "\nThere are process parts that are not yet deployed, automatic deployment is not"
            . " possible, please review all processes and deploy then manually\n";
        $DeployProcesses = 0;
    }

    # update process db records
    for my $PartName (qw(Process Activity ActivityDialog Transition TransitionAction)) {
        for my $Part ( @{ $ProcessManagementList{$PartName} } ) {

            my $Object         = $ProcessManagementObjects{$PartName};
            my $UpdateFunction = $PartName . 'Update';
            my $Success        = $Object->$UpdateFunction(
                %{$Part},
                UserID => 1,
            );
            if ( !$Success ) {
                $Kernel::OM->Get('Kernel::System::Log')->(
                    Priority => 'error',
                    Message  => "\n$PartName $Part->{Name} was not updated!!",
                );
            }
            $Error = 1;
        }
    }

    my %AffectedDBACLsLookup = map { $_ => 1 } @AffectedDBACLs;

    my $DeployACLs = 1;
    if ( $ACLObject->ACLsNeedSync() ) {
        print "\nThere are ACLs that are not yet deployed, automatic deployment is not"
            . " possible, please review all ACLs and deploy them manually!\n";
        $DeployACLs = 0;
    }

    # update ACL db records
    ACL:
    for my $ACL ( @{$ACLList} ) {

        next ACL if !$AffectedDBACLsLookup{ $ACL->{Name} };

        # update DB records with new structure
        my $Success = $ACLObject->ACLUpdate(
            %{$ACL},
            UserID => 1,
        );
        if ( !$Success ) {
            $Kernel::OM->Get('Kernel::System::Log')->(
                Priority => 'error',
                Message  => "Was not possible to migrate ACL $ACL->{Name}!",
            );
            $Error = 1;
        }
    }

    # deploy processes

    if ($DeployProcesses) {

        my $Location = $Kernel::OM->Get('Kernel::Config')->Get('Home')
            . '/Kernel/Config/Files/ZZZProcessManagement.pm';

        my $ProcessDump = ${ProcessObject}->ProcessDump(
            ResultType => 'FILE',
            Location   => $Location,
            UserID     => 1,
        );

        if ($ProcessDump) {
            my $Success = $EntityObject->EntitySyncStatePurge(
                UserID => 1,
            );
            if ( !$Success ) {

                # show error if can't set state
                $Kernel::OM->Get('Kernel::System::Log')->(
                    Priority => 'error',
                    Message  => "There was an error setting the entity sync status.",
                );
                $Error = 1;
            }
        }
        else {

            # show error if can't sync
            $Kernel::OM->Get('Kernel::System::Log')->(
                Priority => 'error',
                Message  => "There was an error synchronizing the processes.",
            );
            $Error = 1;
        }
    }

    # deploy ACLs
    if ($DeployACLs) {
        my $Location = $Kernel::OM->Get('Kernel::Config')->Get('Home') . '/Kernel/Config/Files/ZZZACL.pm';

        my $ACLDump = $ACLObject->ACLDump(
            ResultType => 'FILE',
            Location   => $Location,
            UserID     => 1,
        );

        if ($ACLDump) {

            my $Success = $ACLObject->ACLsNeedSyncReset();

            if ( !$Success ) {
                $Kernel::OM->Get('Kernel::System::Log')->(
                    Priority => 'error',
                    Message  => "There was an error setting the ACL sync status.",
                );
                $Error = 1;
            }
        }
        else {

            # show error if can't sync
            $Kernel::OM->Get('Kernel::System::Log')->(
                Priority => 'error',
                Message  => "There was an error synchronizing the ACLs.",
            );
            $Error = 1;
        }
    }

    # check for in memory ACLs
    my $FileACLs = $Kernel::OM->Get('Kernel::Config')->Get('TicketAcl');

    # remove all DB affected ACLs from in memory ACLs
    for my $ACLName (@AffectedDBACLs) {
        delete $FileACLs->{$ACLName}
    }

    # if there are no ACLs return successfully
    return 1 if !IsHashRefWithData($FileACLs);

    my %AffectedACLs;

    ACL:
    for my $ACLName ( sort keys %{$FileACLs} ) {
        my $ACL = $FileACLs->{$ACLName};

        # skip ACLs that does not include ActivityDialogs
        if ( ref $ACL->{ConfigMatch} ne 'HASH' ) {
            $ACL->{ConfigMatch} = {};
        }

        if ( ref $ACL->{ConfigChange} ne 'HASH' ) {
            $ACL->{ConfigChange} = {};
        }

        if (
            !IsHashRefWithData( $ACL->{ConfigMatch} )
            && !IsHashRefWithData( $ACL->{ConfigChange} )
            )
        {
            next ACL;
        }

        for my $Part (qw(Properties PropertiesDatabase)) {
            if ( ref $ACL->{ConfigMatch}->{$Part} ne 'HASH' ) {
                $ACL->{ConfigMatch}->{$Part} = {};
            }
        }

        for my $Part (qw(Possible PossibleNot)) {
            if ( ref $ACL->{ConfigChange}->{$Part} ne 'HASH' ) {
                $ACL->{ConfigChange}->{$Part} = {};
            }
        }

        if (
            !$ACL->{ConfigMatch}->{Properties}->{Process}
            && !$ACL->{ConfigMatch}->{PropertiesDatabase}->{Process}
            && !$ACL->{ConfigChange}->{Possible}->{ActivityDialog}
            && !$ACL->{ConfigChange}->{PossibleNot}->{ActivityDialog}
            && !$ACL->{ConfigChange}->{Possible}->{Process}
            && !$ACL->{ConfigChange}->{PossibleNot}->{Process}
            )
        {
            next ACL;
        }

        $AffectedACLs{$ACLName} = $ACL;
    }

    # if there are no affected ACLs return successfully
    return 1 if !%AffectedACLs;

    print "\nThe following ACLs are not in the DataBase and have to be updated manually:\n";
    for my $ACLName ( sort keys %AffectedACLs ) {
        print "\t$ACLName\n";
        $Error = 1;
    }

    return if $Error;
    return 1;
}

=item _MigrateProcessManagementDynamicfieldTypes()

Migrate process management Dynamic Fields to use their own driver.

    _MigrateProcessManagementDynamicfieldTypes();

=cut

sub _MigrateProcessManagementDynamicFieldTypes {

    my $ProcessManagementProcessID
        = $Kernel::OM->Get('Kernel::Config')->Get('Process::DynamicFieldProcessManagementProcessID')
        || '';

    if ( !$ProcessManagementProcessID ) {
        print "\tProcess Management dynamic field for Process ID configuration is invalid!\n";

        return;
    }

    my $ProcessManagementActivityID
        = $Kernel::OM->Get('Kernel::Config')->Get('Process::DynamicFieldProcessManagementActivityID') || '';

    if ( !$ProcessManagementActivityID ) {
        print "\tProcess Management dynamic field for Activity ID configuration is invalid!\n";

        return;
    }

    my %UpdateFields = (
        Process => {
            Name      => $ProcessManagementProcessID,
            Label     => 'Process',
            FieldType => 'ProcessID',
        },
        Activity => {
            Name      => $ProcessManagementActivityID,
            Label     => 'Activity',
            FieldType => 'ActivityID',
        },
    );

    my $DBObject = $Kernel::OM->Get('Kernel::System::DB');

    for my $Part (qw(Process Activity)) {
        return if !$DBObject->Do(
            SQL => '
                UPDATE dynamic_field
                SET label = ?, field_type = ?
                WHERE name = ?',
            Bind => [
                \$UpdateFields{$Part}->{Label}, \$UpdateFields{$Part}->{FieldType},
                \$UpdateFields{$Part}->{Name},
            ],
        );
    }

    return 1
}

=item _SetServiceNotifications()

Set new notifications for service update.

    _SetServiceNotifications($CommonObject);

=cut

sub _SetServiceNotifications {

    # define agent notifications
    my %AgentNotificationsLookup = (
        en => [
            'Agent::ServiceUpdate',
            'en',
            'Updated service to <OTRS_TICKET_Service>! (<OTRS_CUSTOMER_SUBJECT[24]>)',
            "Hi <OTRS_UserFirstname>,\n"
                . "\n"
                . "<OTRS_CURRENT_UserFirstname> <OTRS_CURRENT_UserLastname> updated a ticket [<OTRS_TICKET_TicketNumber>] and changed the service to <OTRS_TICKET_Service>.\n"
                . "\n"
                . "<snip>\n"
                . "<OTRS_CUSTOMER_EMAIL[30]>\n"
                . "<snip>\n"
                . "\n"
                . "<OTRS_CONFIG_HttpType>://<OTRS_CONFIG_FQDN>/<OTRS_CONFIG_ScriptAlias>index.pl?Action=AgentTicketZoom;TicketID=<OTRS_TICKET_TicketID>\n"
                . "\n"
                . "Your OTRS Notification Master\n",
        ],
        de => [
            'Agent::ServiceUpdate',
            'de',
            'Service aktualisiert zu <OTRS_TICKET_Service>! (<OTRS_CUSTOMER_SUBJECT[24]>)',
            "Hallo <OTRS_UserFirstname> <OTRS_UserLastname>,\n"
                . "\n"
                . "<OTRS_CURRENT_UserFirstname> <OTRS_CURRENT_UserLastname> aktualisierte Ticket [<OTRS_TICKET_TicketNumber>] and änderte den Service zu <OTRS_TICKET_Service>.\n"
                . "\n"
                . "<snip>\n"
                . "<OTRS_CUSTOMER_EMAIL[30]>\n"
                . "<snip>\n"
                . "\n"
                . "<OTRS_CONFIG_HttpType>://<OTRS_CONFIG_FQDN>/<OTRS_CONFIG_ScriptAlias>index.pl?Action=AgentTicketZoom;TicketID=<OTRS_TICKET_TicketID>\n"
                . "\n"
                . "Ihr OTRS Benachrichtigungs-Master\n",
        ],
    );

    my @AgentNotifications;

    LANGUAGE:
    for my $Language (qw(en de)) {

        return if !$Kernel::OM->Get('Kernel::System::DB')->Prepare(
            SQL => "
                SELECT id
                FROM notifications
                WHERE notification_type = 'Agent::ServiceUpdate'
                    AND notification_language = ?",
            Bind => [ \$Language ],
        );

        my @Data = $Kernel::OM->Get('Kernel::System::DB')->FetchrowArray();

        next LANGUAGE if $Data[0];

        push @AgentNotifications, $AgentNotificationsLookup{$Language};
    }

    # insert the entries
    for my $Notification (@AgentNotifications) {

        my @Binds;
        for my $Value ( @{$Notification} ) {

            # Bind requires scalar references
            push @Binds, \$Value;
        }

        # do the insertion
        return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
            SQL => "
                INSERT INTO notifications (notification_type, notification_language,
                    subject, text, notification_charset, content_type, create_time, create_by,
                    change_time, change_by)
                VALUES ( ?, ?, ?, ?, 'utf-8', 'text/plain', current_timestamp, 1,
                    current_timestamp, 1 )",
            Bind => [@Binds],
        );
    }

    return 1;
}

=item _MigrateSettings()

Migrate different settings

    _MigrateSettings($CommonObject);

=cut

sub _MigrateSettings {
    my $NewValue          = 'MyQueues';
    my $OldValue          = 1;
    my $NewTicketKey      = 'UserSendNewTicketNotification';
    my $FollowUpTicketKey = 'UserSendFollowUpNotification';

    my $Operator = '=';

    # get database object;
    my $DBObject = $Kernel::OM->Get('Kernel::System::DB');

    # use 'LIKE' operator instead of '=' as:
    #    in oracle as the field is defined as CLOB
    #    in mssql as the field is defined as ntext
    #    under this scenarios a direct comparison is not possible using '=' operator
    if (
        $DBObject->{'DB::Type'} eq 'oracle'
        || $DBObject->{'DB::Type'} eq 'mssql'
        )
    {
        $Operator = 'LIKE';
    }

    # do the update
    return if !$DBObject->Do(
        SQL => '
            UPDATE user_preferences
            SET preferences_value = ?
            WHERE
                ( preferences_key = ? OR preferences_key = ? )
                AND preferences_value ' . $Operator . ' ?',
        Bind => [ \$NewValue, \$NewTicketKey, \$FollowUpTicketKey, \$OldValue ],
    );
    return 1;
}

=item _MigrateDBACLs()

Migrate ACLs stored in the DB.

    _MigrateDBACLs($CommonObject);

=cut

sub _MigrateDBACLs {
    my $ACLObject = $Kernel::OM->Get('Kernel::System::ACL::DB::ACL');

    # get a list of all ACLs stored in the DB
    my $ACLList = $ACLObject->ACLListGet(
        UserID => 1,
    );

    my $DeployACLs;
    my @AffectedDBACLs;

    # check if update is needed
    ACL:
    for my $ACL ( @{$ACLList} ) {

        # skip ACLs that does not include Action hash
        next ACL if !ref $ACL->{ConfigChange};
        next ACL if !$ACL->{ConfigChange}->{Possible}->{Action};
        next ACL if ref $ACL->{ConfigChange}->{Possible}->{Action} ne 'HASH';

        # activate deploy ACLs flag
        $DeployACLs = 1;
        push @AffectedDBACLs, $ACL->{Name};

        # convert old hash into an array using only the keys set to 0, and skip those that are set
        # to 1, set them as PossibleNot and delete the Possible->Action section form the ACL.
        my @NewAction = grep { $ACL->{ConfigChange}->{Possible}->{Action}->{$_} == 0 }
            sort keys %{ $ACL->{ConfigChange}->{Possible}->{Action} };

        delete $ACL->{ConfigChange}->{Possible}->{Action};
        $ACL->{ConfigChange}->{PossibleNot}->{Action} = \@NewAction;

        # update DB records with new structure
        my $Success = $ACLObject->ACLUpdate(
            %{$ACL},
            UserID => 1,
        );
        if ( !$Success ) {
            $Kernel::OM->Get('Kernel::System::Log')->(
                Priority => 'error',
                Message  => "Was not possible to migrate ACL $ACL->{Name}!",
            );
            return;
        }
    }

    if ($DeployACLs) {

        if ( $ACLObject->ACLsNeedSync() ) {
            print "\nThere are ACLs that are not yet deployed, Automatic deployment is not"
                . " possible, please check all ACLs and deploy them manually!\n";
        }
        else {
            my $Location = $Kernel::OM->Get('Kernel::Config')->Get('Home')
                . '/Kernel/Config/Files/ZZZACL.pm';

            my $ACLDump = $ACLObject->ACLDump(
                ResultType => 'FILE',
                Location   => $Location,
                UserID     => 1,
            );

            if ($ACLDump) {

                my $Success = $ACLObject->ACLsNeedSyncReset();

                if ( !$Success ) {
                    $Kernel::OM->Get('Kernel::System::Log')->(
                        Priority => 'error',
                        Message  => "There was an error setting the entity sync status.",
                    );
                    return;
                }
            }
            else {

                # show error if can't sync
                $Kernel::OM->Get('Kernel::System::Log')->(
                    Priority => 'error',
                    Message  => "There was an error synchronizing the ACLs.",
                );
                return;
            }
        }
    }

    # check for in memory ACLs
    my $FileACLs = $Kernel::OM->Get('Kernel::Config')->Get('TicketAcl');

    # remove all DB affected ACLs from in memory ACLs
    for my $ACLName (@AffectedDBACLs) {
        delete $FileACLs->{$ACLName}
    }

    # if there are no ACLs return successfully
    return 1 if !IsHashRefWithData($FileACLs);

    my %AffectedACLs;

    ACL:
    for my $ACLName ( sort keys %{$FileACLs} ) {
        my $ACL = $FileACLs->{$ACLName};

        # skip ACLs that does not include Action hash
        next ACL if !$ACL->{Possible}->{Action};
        next ACL if ref $ACL->{Possible}->{Action} ne 'HASH';

        $AffectedACLs{$ACLName} = $ACL;
    }

    # if there are no affected ACLs return successfully
    return 1 if !%AffectedACLs;

    print "\nThe following ACLs are not in the DataBase and have to be updated manually:\n";
    for my $ACLName ( sort keys %AffectedACLs ) {
        print "\t$ACLName\n";
    }
    return 1;
}

=item _UninstallMergedFeatureAddOns()

safe uninstall packages from the database.

    UninstallMergedFeatureAddOns($CommonObject);

=cut

sub _UninstallMergedFeatureAddOns {
    my $PackageObject = Kernel::System::Package->new();

    # Purge relevant caches before uninstalling to avoid errors because of
    #   inconsistent states.
    $Kernel::OM->Get('Kernel::System::Cache')->CleanUp(
        Type => 'RepositoryList',
    );
    $Kernel::OM->Get('Kernel::System::Cache')->CleanUp(
        Type => 'RepositoryGet',
    );
    $Kernel::OM->Get('Kernel::System::Cache')->CleanUp(
        Type => 'XMLParse',
    );

    # Uninstall FeatureAddons that were merged, keeping the DB structures intact.
    for my $PackageName (
        qw( OTRSGenericInterfaceREST OTRSMyServices OTRSStatsRestrictionByDateTimeDF OTRSTextRegex )
        )
    {
        my $Success = $PackageObject->_PackageUninstallMerged(
            Name => $PackageName,
        );
        if ( !$Success ) {
            print STDERR "There was an error uninstalling package $PackageName\n";
            return;
        }
    }

    # Perform a regular uninstall on the support assessment package which was replaced by the
    #    SupportData and SupportBundle features. Here we want to also remove the database tables.
    PACKAGE_NAME:
    for my $PackageName (qw( Support))
    {
        my @PackageList = $PackageObject->RepositoryList(
            Result => 'short',
        );

        my ($PackageEntry) = grep { $_->{Name} eq 'Support' } @PackageList;

        next PACKAGE_NAME if !$PackageEntry;

        my $Package = $PackageObject->RepositoryGet(
            Name    => 'Support',
            Version => $PackageEntry->{Version},
            Result  => 'SCALAR',
        );

        next PACKAGE_NAME if !$Package;

        my $Success = $PackageObject->PackageUninstall(
            String => $Package,
        );
        if ( !$Success ) {
            print STDERR "There was an error uninstalling package $PackageName\n";
            return;
        }
    }

    return 1;
}

=item _MigrateDTLInSysConfig()

migrate settings that contain DTL to TT.

    _MigrateDTLInSysConfig($CommonObject);

=cut

sub _MigrateDTLInSysConfig {

    # create needed objects
    my $ConfigObject    = $Kernel::OM->Get('Kernel::Config');
    my $SysConfigObject = $Kernel::OM->Get('Kernel::System::SysConfig');
    my $ProviderObject  = Kernel::Output::Template::Provider->new();

    # initialize error message
    my $ErrorMessage = '';

    # define setting for migrating
    my @SettingsToTT;

    for my $Item (
        qw(
        AgentTicketMove AgentTicketResponsible AgentTicketPriority
        AgentTicketPending AgentTicketOwner AgentTicketNote AgentTicketClose
        AgentTicketPhoneInbound AgentTicketPhoneOutbound AgentTicketFreeText
        )
        )
    {
        push @SettingsToTT,
            {
            Key    => 'Ticket::Frontend::' . $Item,
            SubKey => 'Subject'
            };
    }

    # include menu settings
    for my $Key (qw(PreMenuModule MenuModule)) {
        my $SettingName    = 'Ticket::Frontend::' . $Key;
        my $SysConfigEntry = $ConfigObject->Get($SettingName);

        if ( IsHashRefWithData($SysConfigEntry) ) {

            for my $Item ( sort keys %{$SysConfigEntry} ) {
                push @SettingsToTT,
                    {
                    Key    => $SettingName,
                    SubKey => $Item
                    };
            }
        }
    }

    # add no hash setting
    push @SettingsToTT,
        {
        Key    => 'Ticket::Frontend::ResponseFormat',
        SubKey => ''
        };

    SETTING:
    for my $Values (@SettingsToTT) {

        # initialize setting
        my $Key    = $Values->{Key}    || '';
        my $SubKey = $Values->{SubKey} || '';

        next SETTING if !$Key;

        # next SETTING if !$SubKey;

        # get setting's content
        my $Setting = $ConfigObject->Get($Key);
        next SETTING if !$Setting;

        my $SettingContent;
        if ( !$SubKey ) {
            $SettingContent = $Setting;
        }
        elsif ( $SubKey eq 'Subject' ) {
            $SettingContent = $Setting->{$SubKey} || '';
        }
        else {
            $SettingContent = $Setting->{$SubKey}->{Link} || '';
        }

        # do nothing no value for migrating
        next SETTING if !$SettingContent;

        my $TTContent;
        eval {
            $TTContent = $ProviderObject->MigrateDTLtoTT( Content => $SettingContent );
        };
        if ($@) {

            $ErrorMessage .= " $Key : $@ \n";
        }
        else {

            next SETTING if $TTContent eq $SettingContent;

            # set migrated value
            if ( !$SubKey ) {
                $Setting = $TTContent;
            }
            elsif ( $SubKey eq 'Subject' ) {
                $Setting->{$SubKey} = $TTContent;
            }
            else {
                $Setting->{$SubKey}->{Link} = $TTContent;
            }

            my $Success = $SysConfigObject->ConfigItemUpdate(
                Valid => 1,
                Key   => $Key,
                Value => $Setting,
            );

        }

    }

    # check if an error is present
    if ($ErrorMessage) {
        print STDERR <<EOF;

One or more sysconfig settings could not be automatically converted
from DTL to Template::Toolkit. The error was:
$ErrorMessage

The upgrading script will continue, please check and update this settings manually.
See also http://otrs.github.io/doc/manual/developer/4.0/en/html/package-porting.html#package-porting-template-engine.

EOF

        # Treat as success, the user should fix this manually.
    }

    return 1;

}

=item _MigrateDTLInDynamicFieldLinks()

migrate dynamic field links that contain DTL to TT.

    _MigrateDTLInDynamicFieldLinks ($CommonObject);

=cut

sub _MigrateDTLInDynamicFieldLinks {

    my $DynamicFieldObject = Kernel::System::DynamicField->new();

    # get a list of all Dynamic Fields
    my $DynamicFieldList = $DynamicFieldObject->DynamicFieldListGet(
        Valid => 0,
    );

    # return success if there are no dynamic fields in the system, nothing to do
    return 1 if !IsArrayRefWithData($DynamicFieldList);

    my $ProviderObject = Kernel::Output::Template::Provider->new();

    # initialize error message
    my $ErrorMessage = '';

    DYNAMICFIELD:
    for my $DynamicField ( @{$DynamicFieldList} ) {

        # validate only dynamic fields with links set are updated
        next DYNAMICFIELD if !$DynamicField->{Name};
        next DYNAMICFIELD if !$DynamicField->{Config};
        next DYNAMICFIELD if !IsHashRefWithData( $DynamicField->{Config} );
        next DYNAMICFIELD if !$DynamicField->{Config}->{Link};

        # make shortcuts
        my $DynamicFieldConfig = $DynamicField->{Config};

        my $TTLink;
        eval {
            $TTLink = $ProviderObject->MigrateDTLtoTT( Content => $DynamicFieldConfig->{Link} );
        };
        if ($@) {

            $ErrorMessage .= " Dynamic Field $DynamicField->{Name} : $@ \n";
        }
        else {

            # only update field where the link has been updated
            next DYNAMICFIELD if $TTLink eq $DynamicFieldConfig->{Link};

            # set new link in the dynamic field configuration
            $DynamicFieldConfig->{Link} = $TTLink;

            # update the field
            my $Success = $DynamicFieldObject->DynamicFieldUpdate(
                %{$DynamicField},
                Config  => $DynamicFieldConfig,
                Reorder => 0,
                UserID  => 1,
            );
        }
    }

    # check if an error is present
    if ($ErrorMessage) {
        print STDERR <<EOF;

One or more dynamic field links could not be automatically converted
from DTL to Template::Toolkit. The error was:
$ErrorMessage

The upgrading script will continue, please check and update this links in the dynamic fileds manually.
See also http://otrs.github.io/doc/manual/developer/4.0/en/html/package-porting.html#package-porting-template-engine.

EOF

        # Treat as success, the user should fix this manually.
    }

    return 1;
}

=item _MigrateGenericAgentProcessManagementDynamicFields()

migrate EntityIDs on Process Management fields in Generic Agent jobs.

    _MigrateGenericAgentProcessManagementDynamicFields();

=cut

sub _MigrateGenericAgentProcessManagementDynamicFields {

    # create needed objects
    my $ConfigObject = Kernel::Config->new();
    my $DBObject     = Kernel::System::DB->new();

    # get the name of the dynamic field to store process information
    my %DynamicFieldName = (
        Process => $ConfigObject->Get('Process::DynamicFieldProcessManagementProcessID')
            || 'ProcessManagementProcessID',
        Activity => $ConfigObject->Get('Process::DynamicFieldProcessManagementActivityID')
            || 'ProcessManagementActivityID',
    );

    for my $Part (qw(Process Activity)) {

        for my $Prefix (qw(DynamicField_ Search_DynamicField_)) {

            my $Field = $Prefix . $DynamicFieldName{$Part};

            ENTITYID:
            for my $OldEntityID ( sort keys( %{ $EntityLookup{$Part} } ) ) {

                next ENTITYID if $OldEntityID =~ m{\A $Part - [0-9a-f]{32} \z}msx;

                # update dynamic fields
                return if !$DBObject->Do(
                    SQL => '
                        UPDATE generic_agent_jobs
                        SET job_value = ?
                        WHERE job_key = ?
                            AND job_value = ?',
                    Bind => [
                        \$EntityLookup{$Part}->{$OldEntityID},
                        \$Field,
                        \$OldEntityID,
                    ],
                );
            }
        }
    }

    return 1;
}

=item _MigrateNotificationEventProcessManagementDynamicFields()

migrate EntityIDs on Process Management fields in event based notifications.

    _MigrateNotificationEventProcessManagementDynamicFields();

=cut

sub _MigrateNotificationEventProcessManagementDynamicFields {

    # create needed objects
    my $ConfigObject = Kernel::Config->new();
    my $DBObject     = Kernel::System::DB->new();

    # get the name of the dynamic field to store process information
    my %DynamicFieldName = (
        Process => $ConfigObject->Get('Process::DynamicFieldProcessManagementProcessID')
            || 'ProcessManagementProcessID',
        Activity => $ConfigObject->Get('Process::DynamicFieldProcessManagementActivityID')
            || 'ProcessManagementActivityID',
    );

    for my $Part (qw(Process Activity)) {

        my $Prefix = 'Search_DynamicField_';

        my $Field = $Prefix . $DynamicFieldName{$Part};

        ENTITYID:
        for my $OldEntityID ( sort keys( %{ $EntityLookup{$Part} } ) ) {

            next ENTITYID if $OldEntityID =~ m{\A $Part - [0-9a-f]{32} \z}msx;

            # update dynamic fields
            return if !$DBObject->Do(
                SQL => '
                    UPDATE notification_event_item
                    SET event_value = ?
                    WHERE event_key = ?
                        AND event_value = ?',
                Bind => [
                    \$EntityLookup{$Part}->{$OldEntityID},
                    \$Field,
                    \$OldEntityID,
                ],
            );
        }
    }

    return 1;
}
1;