Code coverage report for app/views/common/controls_view.js

Statements: 37.66% (177 / 470)      Branches: 30.12% (97 / 322)      Functions: 33.33% (40 / 120)      Lines: 38.43% (176 / 458)      Ignored: none     

All files » app/views/common/ » controls_view.js
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    1   1                                     1 1   1   1 1 1                   1                                                                                             1                     7 7 7                               7                     7 7 7 7 21 19 11     8 8 2   8     13                         1                                                           1                                   1                             1                                                                         1                                                                           1                       1                                               1                       1                                                                               1                     1       1                                                                                                                                                                                                     1                                   1                                               1                                                   13                 19 19 19 16   3                     9                                   11   11 11     11 11 11               39   39         4 4 3 2 2   1 1     4               37               50 50             50                                                 64   64                 165     99   66                 39 78 78                                   13 2   11 11   11 11   11   11   11 11 11   11             11   6   11 11   11 11   11                   3     11 11 11     11 11 11           1 6       6 5   5 2   5   6               1                                                         9 5 5 5 5 5                                       7       1                   1                                           1                           1                                           1                                     1                                                         1                                                                 1                                                       1                                                                                             5 5           5       2     2 2 2   2       4             4       2 2 6     2                       2                                                                                                                                                                                                                                                                                                                                                                 2 2 2                                                                                                                                                                               15 15 15 15 15 15 10 10                                                                           9 8 8 8 2   8   1 1     8 4       4       4   8 8                 1                                                             1                                               7 1   6   7               2       1               1      
'use strict';
 
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
 
;require.register("views/common/controls_view", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
 
  var App = require('app');
  var validator = require('utils/validator');
 
  var dbInfo = require('data/db_properties_info') || {};
 
  var delay = function () {
    var timer = 0;
    return function (callback, ms) {
      clearTimeout(timer);
      timer = setTimeout(callback, ms);
    };
  }();
 
  /**
   * Abstract view for config fields.
   * Add popover support to control
   */
  App.ServiceConfigPopoverSupport = Ember.Mixin.create({
 
    /**
     * Config object. It will instance of App.ServiceConfigProperty
     */
    serviceConfig: null,
    attributeBindings: ['readOnly'],
    isPopoverEnabled: true,
    popoverPlacement: 'auto right',
 
    didInsertElement: function didInsertElement() {
      App.tooltip(this.$('[data-toggle=tooltip]'), { placement: 'top' });
      // if description for this serviceConfig not exist, then no need to show popover
      if (this.get('isPopoverEnabled') !== 'false' && this.get('serviceConfig.description')) {
        this.addPopover();
      }
    },
 
    /**
     * Element where popover is "appended"
     */
    elementForPopover: function () {
      return this.$();
    }.property(),
 
    /**
     * Add Popover for config-boxes
     *
     * @method addPopover
     */
    addPopover: function addPopover() {
      App.popover(this.get('elementForPopover'), {
        title: Em.I18n.t('installer.controls.serviceConfigPopover.title').format(this.get('serviceConfig.displayName'), this.get('serviceConfig.displayName') === this.get('serviceConfig.name') ? '' : this.get('serviceConfig.name')),
        content: this.get('serviceConfig.description'),
        placement: this.get('popoverPlacement'),
        trigger: 'hover',
        html: true
      });
    },
 
    willDestroyElement: function willDestroyElement() {
      this.$().popover('destroy');
    },
 
    readOnly: Em.computed.not('serviceConfig.isEditable')
  });
 
  App.SupportsDependentConfigs = Ember.Mixin.create({
 
    /**
     * method send request to check if some of dependent configs was changes
     * and in case there was changes shows popup with info about changed configs
     *
     * @param {App.ServiceConfigProperty} config
     * @param [controller]
     * @returns {$.Deferred}
     */
    sendRequestRorDependentConfigs: function sendRequestRorDependentConfigs(config, controller) {
      Iif (!config || !config.get('isValid') && config.get('isNotDefaultValue')) return $.Deferred().resolve().promise();
      controller = controller || this.get('controller');
      Iif (controller && ['mainServiceInfoConfigsController', 'wizardStep7Controller'].contains(controller.get('name'))) {
        var name = config.get('name');
        var saveRecommended = config.get('value') === config.get('recommendedValue');
        var type = App.config.getConfigTagFromFileName(config.get('filename'));
        var p = App.configsCollection.getConfig(App.config.configId(name, type));
        controller.removeCurrentFromDependentList(config, saveRecommended);
        if (p && Em.get(p, 'propertyDependedBy.length') > 0 || config.get('displayType') === 'user' && config.get('oldValue') !== config.get('value')) {
          var old = config.get('oldValue');
          config.set('oldValue', config.get('value'));
          return controller.loadConfigRecommendations([{
            "type": type,
            "name": name,
            "old_value": Em.isNone(old) ? config.get('initialValue') : old
          }]);
        }
      }
      return $.Deferred().resolve().promise();
    },
 
    /**
     * Restore values for dependent configs by parent config info.
     * NOTE: If dependent config inherited from multiply configs its
     * value will be restored only when all parent configs are being restored.
     *
     * @param {App.ServiceConfigProperty} parentConfig
     */
    restoreDependentConfigs: function restoreDependentConfigs(parentConfig) {
      var controller = this.get('controller');
      var dependentConfigs = controller.get('recommendations');
      try {
        controller.set('recommendations', dependentConfigs.reject(function (item) {
          if (item.parentConfigs.contains(parentConfig.get('name'))) {
            if (item.parentConfigs.length > 1) {
              item.parentConfigs.removeObject(parentConfig.get('name'));
            } else {
              // reset property value
              var property = App.config.findConfigProperty(controller.get('stepConfigs'), item.propertyName, App.config.getOriginalFileName(item.fileName));
              if (property) {
                property.set('value', property.get('savedValue') || property.get('initialValue'));
              }
              return true;
            }
          }
          return false;
        }));
      } catch (e) {
        console.warn('Dependent properties popup was not cleared');
      }
    }
 
  });
 
  /**
   * mixin is used to send request for recommendations
   * when config value is updated by user
   */
  App.ValueObserver = Em.Mixin.create(App.SupportsDependentConfigs, {
 
    selected: false,
 
    focusOut: function focusOut() {
      this.set('selected', false);
    },
 
    focusIn: function focusIn() {
      this.set('selected', true);
    },
 
    onValueUpdate: function () {
      if (!this.get('isVisible')) return;
      if (this.get('selected') || this.get('serviceConfig.changedViaUndoValue')) {
        var self = this,
            config = this.get('serviceConfig'),
            controller = this.get('controller');
        Em.set(config, 'didUserOverrideValue', true);
        delay(function () {
          self.sendRequestRorDependentConfigs(config, controller);
        }, 500);
      }
    }.observes('serviceConfig.value')
  });
 
  /**
   * mixin is used to send request for recommendations
   * when config value is updated by user
   */
  App.WidgetValueObserver = Em.Mixin.create(App.ValueObserver, {
    onValueUpdate: function () {
      if (this.get('selected')) {
        var self = this,
            config = this.get('config'),
            controller = this.get('controller');
        Em.set(config, 'didUserOverrideValue', true);
        delay(function () {
          self.sendRequestRorDependentConfigs(config, controller);
        }, 500);
      }
    }.observes('config.value')
  });
 
  /**
   * mixin set class that serve as unique element identifier,
   * id not used in order to avoid collision with ember ids
   */
  App.ServiceConfigCalculateId = Ember.Mixin.create({
    'data-qa': Ember.computed(function () {
      var config = this.get('config') && this.get('config.widget') ? this.get('config') : this.get('serviceConfig') || {};
      var configName = Em.get(config, 'name') || '';
      if (configName === 'content') {
        return Em.get(config, 'id') || '';
      }
      return configName;
    })
  });
 
  /**
   * Default input control
   * @type {*}
   */
  App.ServiceConfigTextField = Ember.TextField.extend(App.ServiceConfigPopoverSupport, App.ServiceConfigCalculateId, App.ValueObserver, {
    classNames: ['form-control'],
    valueBinding: 'serviceConfig.value',
    classNameBindings: 'textFieldClassName',
    placeholderBinding: 'serviceConfig.placeholder',
 
    //Set editDone true for last edited config text field parameter
    focusOut: function focusOut() {
      this._super();
      this.get('serviceConfig').set("editDone", true);
    },
    //Set editDone false for all current category config text field parameter
    focusIn: function focusIn() {
      this._super();
      if (!this.get('serviceConfig.isOverridden') && !this.get('serviceConfig.isComparison')) {
        if (this.get('parentView.categoryConfigsAll')) {
          this.get("parentView.categoryConfigsAll").setEach("editDone", false);
        }
      }
    },
 
    textFieldClassName: function () {
      if (this.get('serviceConfig.unit')) {
        return ['input-sm'];
      } else if (this.get('serviceConfig.displayType') === 'principal') {
        return ['col-md-12'];
      } else {
        return ['col-md-9 long-input'];
      }
    }.property('serviceConfig.displayType', 'serviceConfig.unit')
 
  });
 
  /**
   * Customized input control for user/group configs with corresponding uid/gid specified
   * @type {Em.View}
   */
  App.ServiceConfigTextFieldUserGroupWithID = Ember.View.extend(App.ServiceConfigPopoverSupport, {
    valueBinding: 'serviceConfig.value',
    placeholderBinding: 'serviceConfig.savedValue',
    classNamesBindings: 'view.fullWidth::display-inline-block',
    isPopoverEnabled: 'false',
 
    fullWidth: false,
 
    templateName: require('templates/wizard/controls_service_config_usergroup_with_id'),
 
    isUIDGIDVisible: function () {
      var serviceConfigs = this.get('parentView').serviceConfigs;
      var overrideUid = serviceConfigs && serviceConfigs.findProperty('name', 'override_uid');
      //don't display the ugid field if there is no uid/gid for this property or override_uid is unchecked
      if (Em.isNone(this.get('serviceConfig.ugid')) || overrideUid && overrideUid.value === 'false') {
        return false;
      }
 
      var serviceName = this.get('serviceConfig').name.substr(0, this.get('serviceConfig').name.indexOf('_')).toUpperCase();
      if (serviceName === 'ZK') {
        serviceName = 'ZOOKEEPER';
      }
      if (serviceName === 'MAPRED') {
        serviceName = 'YARN';
      }
      //addServiceController and service already installed or Hadoop user group
      if (App.Service.find(serviceName).get('isLoaded') || serviceName === 'USER') {
        return false;
      }
 
      return this.get('parentView.isUIDGIDVisible');
    }.property('parentView.isUIDGIDVisible')
  });
 
  /**
   * Customized input control with Units type specified
   * @type {Em.View}
   */
  App.ServiceConfigTextFieldWithUnit = Ember.View.extend(App.ServiceConfigPopoverSupport, App.ValueObserver, {
    valueBinding: 'serviceConfig.value',
    classNames: ['input-group', 'with-unit', 'col-md-4'],
    placeholderBinding: 'serviceConfig.savedValue',
 
    templateName: require('templates/wizard/controls_service_config_textfield_with_unit')
  });
 
  /**
   * Password control
   * @type {*}
   */
  App.ServiceConfigPasswordField = Ember.View.extend(App.ServiceConfigPopoverSupport, App.ServiceConfigCalculateId, {
 
    serviceConfig: null,
 
    placeholder: Em.I18n.t('form.item.placeholders.typePassword'),
 
    templateName: require('templates/common/configs/widgets/service_config_password_field'),
 
    classNames: ['password-field-wrapper'],
 
    readOnly: Em.computed.not('serviceConfig.isEditable'),
 
    keyPress: function keyPress(event) {
      if (event.keyCode == 13) {
        return false;
      }
    }
 
  });
 
  /**
   * Textarea control
   * @type {*}
   */
  App.ServiceConfigTextArea = Ember.TextArea.extend(App.ServiceConfigPopoverSupport, App.ServiceConfigCalculateId, App.ValueObserver, {
 
    valueBinding: 'serviceConfig.value',
    rows: 4,
    classNames: ['directories', 'form-control'],
    classNameBindings: ['widthClass'],
    widthClass: 'col-md-9'
  });
 
  /**
   * Special config type for Capacity Scheduler
   */
  App.CapacitySceduler = App.ServiceConfigTextArea.extend({
 
    rows: 16,
 
    /**
     * specific property handling for cs
     *
     * @param {App.ServiceConfigProperty} config
     * @param [controller]
     * @returns {$.Deferred}
     * @override
     */
    sendRequestRorDependentConfigs: function sendRequestRorDependentConfigs(config, controller) {
      if (!config.get('isValid') && config.get('isNotDefaultValue')) return $.Deferred().resolve().promise();
      controller = controller || this.get('controller');
      if (controller && ['mainServiceInfoConfigsController', 'wizardStep7Controller'].contains(controller.get('name'))) {
        var schedulerConfigs = config.get('value').split('\n').map(function (_property) {
          return {
            "type": 'capacity-scheduler',
            "name": _property.split('=')[0]
          };
        });
        if (!schedulerConfigs.someProperty('name', 'capacity-scheduler')) {
          schedulerConfigs.push({
            "type": 'capacity-scheduler',
            "name": 'capacity-scheduler'
          });
        }
        return controller.loadConfigRecommendations(schedulerConfigs);
      }
 
      return $.Deferred().resolve().promise();
    }
 
  });
 
  /**
   * Textarea control for content type
   * @type {*}
   */
  App.ServiceConfigTextAreaContent = Ember.TextArea.extend(App.ServiceConfigPopoverSupport, App.ServiceConfigCalculateId, App.ValueObserver, {
 
    valueBinding: 'serviceConfig.value',
    rows: 20,
    classNames: ['col-md-10', 'form-control']
  });
 
  /**
   * Textarea control with bigger height
   * @type {*}
   */
  App.ServiceConfigBigTextArea = App.ServiceConfigTextArea.extend(App.ServiceConfigCalculateId, {
    rows: 10
  });
 
  var checkboxConfigView = Ember.Checkbox.extend(App.ServiceConfigPopoverSupport, App.ServiceConfigCalculateId, App.SupportsDependentConfigs, {
    allowedPairs: {
      'trueFalse': ["true", "false"],
      'YesNo': ["Yes", "No"],
      'YESNO': ["YES", "NO"],
      'yesNo': ["yes", "no"]
    },
 
    trueValue: true,
    falseValue: false,
 
    checked: false,
 
    elementForPopover: function () {
      return this.$().next();
    }.property(),
 
    /**
     * set appropriate config values pair
     * to define which value is positive (checked) property
     * and what value is negative (unchecked) property
     */
    didInsertElement: function didInsertElement() {
      var self = this;
      if (this.get('serviceConfig').isDestroyed) return;
      this._super();
      this.addObserver('serviceConfig.value', this, 'toggleChecker');
      var isInverted = this.get('serviceConfig.displayType') === 'boolean-inverted';
      Object.keys(this.get('allowedPairs')).forEach(function (key) {
        if (this.get('allowedPairs')[key].contains(this.get('serviceConfig.value'))) {
          this.set('trueValue', isInverted ? this.get('allowedPairs')[key][1] : this.get('allowedPairs')[key][0]);
          this.set('falseValue', isInverted ? this.get('allowedPairs')[key][0] : this.get('allowedPairs')[key][1]);
        }
      }, this);
      this.set('checked', this.get('serviceConfig.value') === this.get('trueValue'));
      this.propertyDidChange('checked');
      Em.run.next(function () {
        if (self.$()) {
          self.propertyDidChange('elementForPopover');
          self.addPopover();
        }
      });
    },
 
    willDestroyElement: function willDestroyElement() {
      this.removeObserver('serviceConfig.value', this, 'checkedBinding');
    },
 
    /***
     * defines if checkbox value appropriate to the config value
     * @returns {boolean}
     */
    isNotAppropriateValue: function isNotAppropriateValue() {
      return this.get('serviceConfig.value') !== this.get(this.get('checked') + 'Value');
    },
 
    /**
     * change service config value if click on checkbox
     */
    toggleValue: function () {
      if (this.isNotAppropriateValue()) {
        this.set('serviceConfig.value', this.get(this.get('checked') + 'Value'));
        this.get('serviceConfig').set("editDone", true);
        this.sendRequestRorDependentConfigs(this.get('serviceConfig'));
 
        //if the checkbox being toggled is the 'Have Ambari manage UIDs' in Misc Tab, show/hide uid/gid column accordingly
        if (this.get('serviceConfig.name') === 'override_uid') {
          this.set('parentView.isUIDGIDVisible', this.get('checked'));
        }
      }
    }.observes('checked'),
 
    /**
     * change checkbox value if click on undo
     */
    toggleChecker: function toggleChecker() {
      if (this.isNotAppropriateValue()) {
        this.set('checked', !this.get('checked'));
      }
    },
 
    disabled: Em.computed.not('serviceConfig.isEditable'),
 
    disabledDidChange: function () {
      if (!this.get('checkboxInstance')) return;
    }.observes('disabled'),
 
    //Set editDone false for all current category config text field parameter
    focusIn: function focusIn(event) {
      if (!this.get('serviceConfig.isOverridden') && !this.get('serviceConfig.isComparison')) {
        this.get("parentView.categoryConfigsAll").setEach("editDone", false);
      }
    }
  });
 
  /**
   * Checkbox control
   * @type {*}
   */
  App.ServiceConfigCheckbox = App.CheckboxView.extend({
    classNames: ['display-inline-block'],
    classNameBindings: ['containerClassName'],
    containerClassName: 'checkbox',
    categoryConfigsAllBinding: 'parentView.categoryConfigsAll',
    checkboxView: checkboxConfigView.extend({
      serviceConfigBinding: 'parentView.serviceConfig',
      didInsertElement: function didInsertElement() {
        this.set('parentView.checkboxId', this.get('elementId'));
        this._super();
      }
    })
  });
 
  /**
   * Checkbox control which can hide or show dependent  properties
   * @type {*|void}
   */
  App.ServiceConfigCheckboxWithDependencies = App.ServiceConfigCheckbox.extend({
 
    toggleDependentConfigs: function () {
      if (this.get('serviceConfig.dependentConfigPattern')) {
        if (this.get('serviceConfig.dependentConfigPattern') === "CATEGORY") {
          this.disableEnableCategoryConfigs();
        } else {
          this.showHideDependentConfigs();
        }
      }
    }.observes('checked'),
 
    disableEnableCategoryConfigs: function disableEnableCategoryConfigs() {
      this.get('categoryConfigsAll').setEach('isEditable', this.get('checked'));
      this.set('serviceConfig.isEditable', true);
    },
 
    showHideDependentConfigs: function showHideDependentConfigs() {
      this.get('categoryConfigsAll').forEach(function (c) {
        if (c.get('name').match(this.get('serviceConfig.dependentConfigPattern')) && c.get('name') != this.get('serviceConfig.name')) c.set('isVisible', this.get('checked'));
      }, this);
    }
  });
 
  App.ServiceConfigRadioButtons = Ember.View.extend(App.ServiceConfigCalculateId, App.SupportsDependentConfigs, {
    templateName: require('templates/wizard/controls_service_config_radio_buttons'),
 
    didInsertElement: function didInsertElement() {
      // on page render, automatically populate JDBC URLs only for default database settings
      // so as to not lose the user's customizations on these fields
      if (this.get('hostNameProperty')) {
        this.get('hostNameProperty').set('isEditable', !this.get('isNewDb'));
      }
      if (['addServiceController', 'installerController'].contains(this.get('controller.wizardController.name')) && !App.StackService.find(this.get('serviceConfig.serviceName')).get('isInstalled')) {
        if (this.get('isNewDb') || this.get('dontUseHandleDbConnection').contains(this.get('serviceConfig.name'))) {
          this.onOptionsChange();
        } else {
          if (App.get('isHadoopWindowsStack') && this.get('inMSSQLWithIA') || this.get('serviceConfig.name') === 'DB_FLAVOR') {
            this.onOptionsChange();
          }
        }
        this.handleDBConnectionProperty();
      }
    },
 
    /**
     * Radio buttons that are not DB options and should not trigger any observer or change any other property's value
     * Ranger service -> "Authentication method" property is an example for non DB related radio button
     */
    nonDBRadioButtons: function () {
      return this.get('dontUseHandleDbConnection').without('DB_FLAVOR');
    }.property('dontUseHandleDbConnection'),
 
    /**
     * properties with these names don'use handleDBConnectionProperty method
     */
    dontUseHandleDbConnection: function () {
      // functionality added in Ranger 0.5
      // remove DB_FLAVOR so it can handle DB Connection checks
      var rangerService = App.StackService.find().findProperty('serviceName', 'RANGER');
      var rangerVersion = rangerService ? rangerService.get('serviceVersion') : '';
      if (rangerVersion && rangerVersion.split('.')[0] < 1 && rangerVersion.split('.')[1] < 5) {
        return ['DB_FLAVOR', 'authentication_method'];
      }
      return ['ranger.authentication.method'];
    }.property('App.currentStackName'),
 
    serviceConfig: null,
    categoryConfigsAll: null,
 
    /**
     * defines if new db is selected;
     * @type {boolean}
     */
    isNewDb: function () {
      return (/New /g.test(this.get('serviceConfig.value'))
      );
    }.property('serviceConfig.serviceName', 'serviceConfig.value'),
 
    /**
     * defines if 'Existing MSSQL Server database with integrated authentication' is selected
     * in this case some properties can have different behaviour
     * @type {boolean}
     */
    inMSSQLWithIA: Em.computed.equal('serviceConfig.value', 'Existing MSSQL Server database with integrated authentication'),
 
    /**
     * Radio button has very uncomfortable values for managing it's state
     * so it's better to use code values that easier to manipulate. Ex:
     * "Existing MySQL Database" transforms to "MYSQL"
     * @type {string}
     */
    getDbTypeFromRadioValue: function () {
      var currentValue = this.get('serviceConfig.value');
      /** TODO: Remove SQLA from the list of databases once Ranger DB_FLAVOR=SQLA is replaced with SQL Anywhere */
      var databases = /MySQL|Postgres|Oracle|Derby|MSSQL|SQLA|Anywhere/gi;
      Iif (this.get('inMSSQLWithIA')) {
        return 'MSSQL2';
      } else {
        var matches = currentValue.match(databases);
        Eif (matches) {
          return currentValue.match(databases)[0].toUpperCase();
        } else {
          return "MYSQL";
        }
      }
    }.property('serviceConfig.serviceName', 'serviceConfig.value'),
 
    onOptionsChange: function () {
      Eif (!this.get('nonDBRadioButtons').contains(this.get('serviceConfig.name'))) {
        /** if new db is selected host name must be same as master of selected service (and can't be changed)**/
        this.handleSpecialUserPassProperties();
      }
    }.observes('databaseProperty.value', 'hostNameProperty.value', 'serviceConfig.value'),
 
    name: function () {
      var name = this.get('serviceConfig.radioName');
      if (!this.get('serviceConfig.isOriginalSCP')) {
        if (this.get('serviceConfig.isComparison')) {
          var version = this.get('serviceConfig.compareConfigs') ? this.get('controller.selectedVersion') : this.get('version');
          name += '-v' + version;
        } else {
          var group = this.get('serviceConfig.group.name');
          name += '-' + group;
        }
      }
      return name;
    }.property('serviceConfig.radioName'),
 
    /**
     * Just property object for database name
     * @type {App.ServiceConfigProperty}
     */
    databaseProperty: function () {
      return this.getPropertyByType('db_name');
    }.property('serviceConfig.serviceName'),
 
    /**
     * Just property object for host name
     * @type {App.ServiceConfigProperty}
     */
    hostNameProperty: function () {
      var host = this.getPropertyByType('host_name');
      Iif (host && !host.get('value')) {
        if (host.get('savedValue')) {
          host.set('value', host.get('savedValue'));
        } else if (host.get('recommendedValue')) {
          host.set('value', host.get('recommendedValue'));
        }
      }
      return host;
    }.property('serviceConfig.serviceName', 'serviceConfig.value'),
 
    /**
     * Just property object for database name
     * @type {App.ServiceConfigProperty}
     */
    userProperty: function () {
      return this.getPropertyByType('user_name');
    }.property('serviceConfig.serviceName'),
 
    /**
     * Just property object for database name
     * @type {App.ServiceConfigProperty}
     */
    passwordProperty: function () {
      return this.getPropertyByType('password');
    }.property('serviceConfig.serviceName'),
 
    /**
     *
     * @param propertyType
     * @returns {*}
     */
    getDefaultPropertyValue: function getDefaultPropertyValue(propertyType) {
      var dbProperties = dbInfo.dpPropertiesMap[this.get('getDbTypeFromRadioValue')],
          serviceName = this.get('serviceConfig.serviceName');
      return dbProperties[serviceName] && dbProperties[serviceName][propertyType] ? dbProperties[serviceName][propertyType] : dbProperties[propertyType];
    },
 
    /**
     *
     * @param propertyType
     * @returns {*|Object}
     */
    getPropertyByType: function getPropertyByType(propertyType) {
      if (dbInfo.dpPropertiesByServiceMap[this.get('serviceConfig.serviceName')]) {
        //@TODO: dbInfo.dpPropertiesByServiceMap has corresponding property name but does not have filenames with it. this can cause issue when there are multiple db properties with same name belonging to different files
        /** check if selected service has db properties**/
        return this.get('controller.selectedService.configs').findProperty('name', dbInfo.dpPropertiesByServiceMap[this.get('serviceConfig.serviceName')][propertyType]);
      }
      return null;
    },
 
    /**
     * This method hides properties <code>user_name<code> and <code>password<code> in case selected db is
     * "Existing MSSQL Server database with integrated authentication" or similar
     * @method handleSpecialUserPassProperties
     */
    handleSpecialUserPassProperties: function handleSpecialUserPassProperties() {
      ['user_name', 'password'].forEach(function (pType) {
        var property = this.getPropertyByType(pType);
        Iif (property) {
          property.setProperties({
            'isVisible': !this.get('inMSSQLWithIA'),
            'isRequired': !this.get('inMSSQLWithIA')
          });
        }
      }, this);
    },
 
    /**
     * `Observer` that add <code>additionalView</code> to <code>App.ServiceConfigProperty</code>
     * that responsible for (if existing db selected)
     * 1. checking database connection
     * 2. showing jdbc driver setup warning msg.
     *
     * @method handleDBConnectionProperty
     **/
    handleDBConnectionProperty: function () {
      if (this.get('dontUseHandleDbConnection').contains(this.get('serviceConfig.name'))) {
        return;
      }
      var handledProperties = ['oozie_database', 'hive_database', 'DB_FLAVOR'];
      var currentValue = this.get('serviceConfig.value');
      /** TODO: Remove SQLA from the list of databases once Ranger DB_FLAVOR=SQLA is replaced with SQL Anywhere */
      var databases = /MySQL|PostgreSQL|Postgres|Oracle|Derby|MSSQL|SQLA|Anywhere/gi;
      var currentDB = currentValue.match(databases)[0];
      /** TODO: Remove SQLA from the list of databases once Ranger DB_FLAVOR=SQLA is replaced with SQL Anywhere */
      var checkDatabase = /existing/gi.test(currentValue);
      // db connection check button show up if existed db selected
      var propertyAppendTo1 = this.get('categoryConfigsAll').findProperty('displayName', 'Database URL');
      // warning msg under database type radio buttons, to warn the user to setup jdbc driver if existed db selected
      var propertyHive = this.get('categoryConfigsAll').findProperty('displayName', 'Hive Database');
      var propertyOozie = this.get('categoryConfigsAll').findProperty('displayName', 'Oozie Database');
      var propertyAppendTo2 = propertyHive ? propertyHive : propertyOozie;
      // RANGER specific
      Iif (this.get('serviceConfig.serviceName') === 'RANGER') {
        propertyAppendTo1 = this.get('categoryConfigsAll').findProperty('name', 'ranger.jpa.jdbc.url');
        propertyAppendTo2 = this.get('categoryConfigsAll').findProperty('name', 'DB_FLAVOR');
        // check for all db types when installing Ranger - not only for existing ones
        checkDatabase = true;
      }
      // Hive specific
      if (this.get('serviceConfig.serviceName') === 'HIVE') {
        // check for all db types when installing Hive - not only for existing ones
        checkDatabase = true;
      }
      Eif (propertyAppendTo1) {
        propertyAppendTo1.set('additionalView', null);
      }
      Eif (propertyAppendTo2) {
        propertyAppendTo2.set('additionalView', null);
      }
      var shouldAdditionalViewsBeSet = currentDB && checkDatabase && handledProperties.contains(this.get('serviceConfig.name')),
          driver = this.getDefaultPropertyValue('sql_jar_connector') ? this.getDefaultPropertyValue('sql_jar_connector').split("/").pop() : 'driver.jar',
          dbType = this.getDefaultPropertyValue('db_type'),
          dbName = this.getDefaultPropertyValue('db_name'),
          driverName = this.getDefaultPropertyValue('driver_name'),
          driverDownloadUrl = this.getDefaultPropertyValue('driver_download_url'),
          additionalView1 = shouldAdditionalViewsBeSet && !this.get('isNewDb') ? App.CheckDBConnectionView.extend({ databaseName: dbType }) : null,
          additionalView2 = shouldAdditionalViewsBeSet ? Ember.View.extend({
        template: Ember.Handlebars.compile('<div class="alert alert-warning">{{{view.message}}}</div>'),
        message: function () {
          return Em.I18n.t('services.service.config.database.msg.jdbcSetup.detailed').format(dbName, dbType, driver, driverDownloadUrl, driverName);
        }.property()
      }) : null;
      Eif (propertyAppendTo1) {
        Em.run.next(function () {
          propertyAppendTo1.set('additionalView', additionalView1);
        });
      }
      Eif (propertyAppendTo2) {
        Em.run.next(function () {
          propertyAppendTo2.set('additionalView', additionalView2);
        });
      }
    }.observes('serviceConfig.value'),
 
    options: function () {
      return this.get('serviceConfig.options').map(function (option) {
        var dbTypePattern = /mysql|postgres|oracle|derby|mssql|sql\s?a/i,
            className = '',
            displayName = Em.get(option, 'displayName'),
            dbTypeMatch = displayName.match(dbTypePattern);
        if (dbTypeMatch) {
          var dbSourcePattern = /new/i,
              newDbMatch = displayName.match(dbSourcePattern);
          if (newDbMatch) {
            className += 'new-';
          }
          className += dbTypeMatch[0].replace(' ', '').toLowerCase();
        }
        return className ? Em.Object.create(option, {
          className: className,
          radioId: ''
        }) : option;
      });
    }.property('serviceConfig.options')
  });
 
  App.ServiceConfigRadioButton = Ember.Checkbox.extend(App.SupportsDependentConfigs, {
    tagName: 'input',
    attributeBindings: ['type', 'name', 'value', 'checked', 'disabled'],
    checked: false,
    clicked: false,
    type: 'radio',
    name: null,
    value: null,
    /**
     * Element id passed to label's <code>for</code> attribute.
     * @type {String}
     */
    radioId: '',
 
    didInsertElement: function didInsertElement() {
      this.set('radioId', this.get('elementId'));
      this.set('checked', this.get('parentView.serviceConfig.value') === this.get('value'));
    },
 
    click: function click() {
      this.set('clicked', true);
      this.set('checked', true);
      this.onChecked();
    },
 
    onChecked: function () {
      // Wrapping the call with Ember.run.next prevents a problem where setting isVisible on component
      // causes JS error due to re-rendering.  For example, this occurs when switching the Config Group
      // in Service Config page
      if (this.get('clicked')) {
        Em.run.next(this, function () {
          this.set('parentView.serviceConfig.value', this.get('value'));
          this.sendRequestRorDependentConfigs(this.get('parentView.serviceConfig'));
          this.set('clicked', false);
          this.updateForeignKeys();
        });
      }
    }.observes('checked'),
 
    updateCheck: function () {
      if (!this.get('clicked')) {
        this.set('checked', this.get('parentView.serviceConfig.value') === this.get('value'));
        this.updateForeignKeys();
      }
    }.observes('parentView.serviceConfig.value'),
 
    updateForeignKeys: function updateForeignKeys() {
      var components = this.get('parentView.serviceConfig.options');
      if (components && components.someProperty('foreignKeys')) {
        this.get('controller.stepConfigs').findProperty('serviceName', this.get('parentView.serviceConfig.serviceName')).propertyDidChange('errorCount');
      }
    },
 
    disabled: function () {
      return !this.get('parentView.serviceConfig.isEditable') || !['addServiceController', 'installerController'].contains(this.get('controller.wizardController.name')) && /^New\s\w+\sDatabase$/.test(this.get('value'));
    }.property('parentView.serviceConfig.isEditable')
  });
 
  App.ServiceConfigComboBox = Ember.Select.extend(App.ServiceConfigPopoverSupport, App.ServiceConfigCalculateId, App.SupportsDependentConfigs, {
    contentBinding: 'serviceConfig.options',
    selectionBinding: 'serviceConfig.value',
    placeholderBinding: 'serviceConfig.savedValue',
    classNames: ['col-md-3']
  });
 
  /**
   * Base component for host config with popover support
   */
  App.ServiceConfigHostPopoverSupport = Ember.Mixin.create({
 
    /**
     * Config object. It will instance of App.ServiceConfigProperty
     */
    serviceConfig: null,
 
    didInsertElement: function didInsertElement() {
      App.popover(this.$(), {
        title: this.get('serviceConfig.displayName'),
        content: this.get('serviceConfig.description'),
        placement: 'right',
        trigger: 'hover'
      });
    }
  });
 
  /**
   * Master host component.
   * Show hostname without ability to edit it
   * @type {*}
   */
  App.ServiceConfigMasterHostView = Ember.View.extend(App.ServiceConfigHostPopoverSupport, App.ServiceConfigCalculateId, {
 
    classNames: ['master-host', 'col-md-6'],
    valueBinding: 'serviceConfig.value',
 
    template: Ember.Handlebars.compile('{{view.value}}')
 
  });
 
  /**
   * text field property view that enables possibility
   * for check connection
   * @type {*}
   */
  App.checkConnectionView = App.ServiceConfigTextField.extend({
    didInsertElement: function didInsertElement() {
      this._super();
      if (this.get('controller.isHostsConfigsPage')) {
        return;
      }
      var kdc = this.get('categoryConfigsAll').findProperty('name', 'kdc_type');
      var propertyAppendTo = this.get('categoryConfigsAll').findProperty('name', 'domains');
      if (propertyAppendTo) {
        try {
          propertyAppendTo.set('additionalView', App.CheckDBConnectionView.extend({ databaseName: kdc && kdc.get('value') }));
        } catch (e) {
          console.error('error while adding "Test connection button"');
        }
      }
    }
  });
 
  /**
   * Show value as plain label in italics
   * @type {*}
   */
  App.ServiceConfigLabelView = Ember.View.extend(App.ServiceConfigHostPopoverSupport, App.ServiceConfigCalculateId, {
 
    classNames: ['master-host', 'col-md-6'],
    valueBinding: 'serviceConfig.value',
    unitBinding: 'serviceConfig.unit',
 
    fullValue: function () {
      var value = this.get('value');
      var unit = this.get('unit');
      return unit ? value + ' ' + unit : value;
    }.property('value', 'unit'),
 
    template: Ember.Handlebars.compile('<i>{{formatWordBreak view.fullValue}}</i>')
  });
 
  /**
   * Base component to display Multiple hosts
   * @type {*}
   */
  App.ServiceConfigMultipleHostsDisplay = Ember.Mixin.create(App.ServiceConfigHostPopoverSupport, App.ServiceConfigCalculateId, {
 
    hasNoHosts: function () {
      if (!this.get('value')) {
        return true;
      }
      return !this.get('value.length');
    }.property('value'),
 
    formatValue: Em.computed.ifThenElseByKeys('hasOneHost', 'value.firstObject', 'value'),
 
    hasOneHost: Em.computed.equal('value.length', 1),
 
    hasMultipleHosts: Em.computed.gt('value.length', 1),
 
    otherLength: function () {
      var len = this.get('value.length');
      if (len > 2) {
        return Em.I18n.t('installer.controls.serviceConfigMultipleHosts.others').format(len - 1);
      }
      return Em.I18n.t('installer.controls.serviceConfigMultipleHosts.other');
    }.property('value')
 
  });
 
  /**
   * Multiple Slave Hosts component
   * @type {*}
   */
  App.ServiceConfigComponentHostsView = Ember.View.extend(App.ServiceConfigMultipleHostsDisplay, App.ServiceConfigCalculateId, {
 
    viewName: 'serviceConfigSlaveHostsView',
 
    classNames: ['component-hosts', 'form-text'],
 
    valueBinding: 'serviceConfig.value',
 
    templateName: require('templates/wizard/component_hosts'),
 
    firstHost: Em.computed.firstNotBlank('value.firstObject', 'serviceConfig.value.firstObject'),
 
    /**
     * Onclick handler for link
     */
    showHosts: function showHosts() {
      var serviceConfig = this.get('serviceConfig');
      App.ModalPopup.show({
        header: Em.I18n.t('installer.controls.serviceConfigMasterHosts.header').format(serviceConfig.category),
        bodyClass: Ember.View.extend({
          serviceConfig: serviceConfig,
          templateName: require('templates/wizard/component_hosts_popup')
        }),
        secondary: null
      });
    }
 
  });
 
  /**
   * DropDown component for <code>select hosts for groups</code> popup
   * @type {*}
   */
  App.SlaveComponentDropDownGroupView = Ember.View.extend(App.ServiceConfigCalculateId, {
 
    viewName: "slaveComponentDropDownGroupView",
 
    /**
     * On change handler for <code>select hosts for groups</code> popup
     * @param event
     */
    changeGroup: function changeGroup(event) {
      var host = this.get('content');
      var groupName = $('#' + this.get('elementId') + ' select').val();
      this.get('controller').changeHostGroup(host, groupName);
    },
 
    optionTag: Ember.View.extend({
 
      /**
       * Whether current value(OptionTag value) equals to host value(assigned to SlaveComponentDropDownGroupView.content)
       */
      selected: function () {
        return this.get('parentView.content.group') === this.get('content');
      }.property('content')
    })
  });
 
  /**
   * View for testing connection to database.
   **/
  App.CheckDBConnectionView = Ember.View.extend({
    templateName: require('templates/common/form/check_db_connection'),
    /** @property {string} btnCaption - text for button **/
    btnCaption: function () {
      return this.get('parentView.service.serviceName') === 'KERBEROS' ? Em.I18n.t('services.service.config.kdc.btn.idle') : Em.I18n.t('services.service.config.database.btn.idle');
    }.property('parentView.service.serviceName'),
    /** @property {string} responseCaption - text for status link **/
    responseCaption: null,
    /** @property {boolean} isConnecting - is request to server activated **/
    isConnecting: false,
    /** @property {boolean} isValidationPassed - check validation for required fields **/
    isValidationPassed: null,
    /** @property {string} databaseName- name of current database **/
    databaseName: null,
    /** @property {boolean} isRequestResolved - check for finished request to server **/
    isRequestResolved: false,
    /** @property {boolean} isConnectionSuccess - check for successful connection to database **/
    isConnectionSuccess: null,
    /** @property {string} responseFromServer - message from server response **/
    responseFromServer: null,
    /** @property {Object} ambariRequiredProperties - properties that need for custom action request **/
    ambariRequiredProperties: null,
    /** @property {Number} currentRequestId - current custom action request id **/
    currentRequestId: null,
    /** @property {Number} currentTaskId - current custom action task id **/
    currentTaskId: null,
    /** @property {jQuery.Deferred} request - current $.ajax request **/
    request: null,
    /** @property {Number} pollInterval - timeout interval for ajax polling **/
    pollInterval: 3000,
    /** @property {Object} logsPopup - popup with DB connection check info **/
    logsPopup: null,
    /** @property {string} hostNameProperty - host name property based on service and database names **/
    hostNameProperty: function () {
      if (!/wizard/i.test(this.get('controller.name')) && this.get('parentView.service.serviceName') === 'HIVE') {
        return this.get('parentView.service.serviceName').toLowerCase() + '_hostname';
      } else if (this.get('parentView.service.serviceName') === 'KERBEROS') {
        return 'kdc_hosts';
      } else if (this.get('parentView.service.serviceName') === 'RANGER') {
        return '{0}_{1}_host'.format(this.get('parentView.service.serviceName').toLowerCase(), this.get('databaseName').toLowerCase());
      }
      return '{0}_existing_{1}_host'.format(this.get('parentView.service.serviceName').toLowerCase(), this.get('databaseName').toLowerCase());
    }.property('databaseName'),
    /** @property {boolean} isBtnDisabled - disable button on failed validation or active request **/
    isBtnDisabled: Em.computed.or('!isValidationPassed', 'isConnecting'),
    /** @property {object} requiredProperties - properties that necessary for database connection **/
    requiredProperties: function () {
      var ranger = App.StackService.find().findProperty('serviceName', 'RANGER');
      var propertiesMap = {
        OOZIE: ['oozie.db.schema.name', 'oozie.service.JPAService.jdbc.username', 'oozie.service.JPAService.jdbc.password', 'oozie.service.JPAService.jdbc.driver', 'oozie.service.JPAService.jdbc.url'],
        HIVE: ['ambari.hive.db.schema.name', 'javax.jdo.option.ConnectionUserName', 'javax.jdo.option.ConnectionPassword', 'javax.jdo.option.ConnectionDriverName', 'javax.jdo.option.ConnectionURL'],
        KERBEROS: ['kdc_hosts'],
        RANGER: ranger && App.StackService.find().findProperty('serviceName', 'RANGER').compareCurrentVersion('0.5') > -1 ? ['db_user', 'db_password', 'db_name', 'ranger.jpa.jdbc.url', 'ranger.jpa.jdbc.driver'] : ['db_user', 'db_password', 'db_name', 'ranger_jdbc_connection_url', 'ranger_jdbc_driver']
      };
      return propertiesMap[this.get('parentView.service.serviceName')];
    }.property(),
    /** @property {Object} propertiesPattern - check pattern according to type of connection properties **/
    propertiesPattern: function () {
      var patterns = {
        db_connection_url: /jdbc\.url|connection_url|connectionurl|kdc_hosts/ig
      };
      Eif (this.get('parentView.service.serviceName') != "KERBEROS") {
        patterns.user_name = /(username|dblogin|db_user)$/ig;
        patterns.user_passwd = /(dbpassword|password|db_password)$/ig;
      }
      return patterns;
    }.property('parentView.service.serviceName'),
    /** @property {String} masterHostName - host name location of Master Component related to Service **/
    masterHostName: function () {
      var serviceMasterMap = {
        'OOZIE': 'oozie_server_hosts',
        'HDFS': 'hadoop_host',
        'HIVE': 'hive_metastore_hosts',
        'KERBEROS': 'kdc_hosts',
        'RANGER': 'ranger_server_hosts'
      };
      return this.get('parentView.categoryConfigsAll').findProperty('name', serviceMasterMap[this.get('parentView.service.serviceName')]).get('value');
    }.property('parentView.service.serviceName', 'parentView.categoryConfigsAll.@each.value'),
    /** @property {Object} connectionProperties - service specific config values mapped for custom action request **/
    connectionProperties: function () {
      var propObj = {};
      for (var key in this.get('propertiesPattern')) {
        propObj[key] = this.getConnectionProperty(this.get('propertiesPattern')[key]);
      }
 
      Iif (this.get('parentView.service.serviceName') === 'RANGER') {
        var dbFlavor = this.get('parentView.categoryConfigsAll').findProperty('name', 'DB_FLAVOR').get('value'),
 
        /** TODO: Remove SQLA from the list of databases once Ranger DB_FLAVOR=SQLA is replaced with SQL Anywhere */
        databasesTypes = /MYSQL|POSTGRES|ORACLE|MSSQL|SQLA|Anywhere/gi,
            dbType = dbFlavor.match(databasesTypes) ? dbFlavor.match(databasesTypes)[0].toLowerCase() : '';
 
        if (dbType === 'oracle') {
          // fixes oracle SYSDBA issue
          propObj['user_name'] = "\'%@ as sysdba\'".fmt(propObj['user_name']);
        }
      }
      return propObj;
    }.property('parentView.categoryConfigsAll.@each.value'),
    /**
     * Properties that stores in local storage used for handling
     * last success connection.
     *
     * @property {Object} preparedDBProperties
     **/
    preparedDBProperties: function () {
      var propObj = {};
      for (var key in this.get('propertiesPattern')) {
        var propName = this.getConnectionProperty(this.get('propertiesPattern')[key], true);
        propObj[propName] = this.get('parentView.categoryConfigsAll').findProperty('name', propName).get('value');
      }
      return propObj;
    }.property(),
    /** Check validation and load ambari properties **/
    didInsertElement: function didInsertElement() {
      var kdc = this.get('parentView.categoryConfigsAll').findProperty('name', 'kdc_type');
      if (kdc) {
        var name = kdc.get('value');
        if (name == 'Existing MIT KDC') {
          name = 'KDC';
        } else if (name == 'Existing IPA') {
          name = 'IPA';
        } else {
          name = 'AD';
        }
        App.popover(this.$('.connection-result'), {
          title: Em.I18n.t('services.service.config.database.btn.idle'),
          content: Em.I18n.t('installer.controls.checkConnection.popover').format(name),
          placement: 'right',
          trigger: 'hover'
        });
      }
      this.handlePropertiesValidation();
      this.getAmbariProperties();
    },
    /** On view destroy **/
    willDestroyElement: function willDestroyElement() {
      this.set('isConnecting', false);
      this._super();
    },
    /**
     * Observer that take care about enabling/disabling button based on required properties validation.
     *
     * @method handlePropertiesValidation
     **/
    handlePropertiesValidation: function () {
      this.restore();
      var isValid = true;
      var properties = [].concat(this.get('requiredProperties'));
      properties.push(this.get('hostNameProperty'));
      properties.forEach(function (propertyName) {
        var property = this.get('parentView.categoryConfigsAll').findProperty('name', propertyName);
        if (property && !property.get('isValid')) isValid = false;
      }, this);
      this.set('isValidationPassed', isValid);
    }.observes('parentView.categoryConfigsAll.@each.isValid', 'parentView.categoryConfigsAll.@each.value', 'databaseName'),
 
    getConnectionProperty: function getConnectionProperty(regexp, isGetName) {
      var _this = this;
      var propertyName = _this.get('requiredProperties').filter(function (item) {
        return regexp.test(item);
      })[0];
      return isGetName ? propertyName : _this.get('parentView.categoryConfigsAll').findProperty('name', propertyName).get('value');
    },
    /**
     * Set up ambari properties required for custom action request
     *
     * @method getAmbariProperties
     **/
    getAmbariProperties: function getAmbariProperties() {
      var clusterController = App.router.get('clusterController');
      var _this = this;
      if (!App.isEmptyObject(App.db.get('tmp', 'ambariProperties')) && !this.get('ambariProperties')) {
        this.set('ambariProperties', App.db.get('tmp', 'ambariProperties'));
        return;
      }
      if (App.isEmptyObject(clusterController.get('ambariProperties'))) {
        clusterController.loadAmbariProperties().done(function (data) {
          _this.formatAmbariProperties(data.RootServiceComponents.properties);
        });
      } else {
        this.formatAmbariProperties(clusterController.get('ambariProperties'));
      }
    },
 
    formatAmbariProperties: function formatAmbariProperties(properties) {
      var defaults = {
        threshold: "60",
        ambari_server_host: location.hostname,
        check_execute_list: "db_connection_check"
      };
      var properties = App.permit(properties, ['jdk.name', 'jdk_location', 'java.home']);
      var renameKey = function renameKey(oldKey, newKey) {
        if (properties[oldKey]) {
          defaults[newKey] = properties[oldKey];
          delete properties[oldKey];
        }
      };
      renameKey('java.home', 'java_home');
      renameKey('jdk.name', 'jdk_name');
      $.extend(properties, defaults);
      App.db.set('tmp', 'ambariProperties', properties);
      this.set('ambariProperties', properties);
    },
    /**
     * `Action` method for starting connect to current database.
     *
     * @method connectToDatabase
     **/
    connectToDatabase: function connectToDatabase() {
      if (this.get('isBtnDisabled')) return;
      this.set('isRequestResolved', false);
      App.db.set('tmp', this.get('parentView.service.serviceName') + '_connection', {});
      this.setConnectingStatus(true);
      if (App.get('testMode')) {
        this.startPolling();
      } else {
        this.runCheckConnection();
      }
    },
 
    /**
     * runs check connections methods depending on service
     * @return {void}
     * @method runCheckConnection
     */
    runCheckConnection: function runCheckConnection() {
      if (this.get('parentView.service.serviceName') === 'KERBEROS') {
        this.runKDCCheck();
      } else {
        this.createCustomAction();
      }
    },
 
    /**
     * send ajax request to perforn kdc host check
     * @return {App.ajax}
     * @method runKDCCheck
     */
    runKDCCheck: function runKDCCheck() {
      return App.ajax.send({
        name: 'admin.kerberos_security.test_connection',
        sender: this,
        data: {
          kdcHostname: this.get('masterHostName')
        },
        success: 'onRunKDCCheckSuccess',
        error: 'onCreateActionError'
      });
    },
 
    /**
     *
     * @param data
     */
    onRunKDCCheckSuccess: function onRunKDCCheckSuccess(data) {
      var statusCode = {
        success: 'REACHABLE',
        failed: 'UNREACHABLE'
      };
      if (data == statusCode['success']) {
        this.setResponseStatus('success');
      } else {
        this.setResponseStatus('failed');
      }
      this.set('responseFromServer', data);
    },
 
    /**
     * Run custom action for database connection.
     *
     * @method createCustomAction
     **/
    createCustomAction: function createCustomAction() {
      var isServiceInstalled = App.Service.find(this.get('parentView.service.serviceName')).get('isLoaded');
      var params = $.extend(true, {}, { db_name: this.get('databaseName').toLowerCase() }, this.get('connectionProperties'), this.get('ambariProperties'));
      App.ajax.send({
        name: isServiceInstalled ? 'cluster.custom_action.create' : 'custom_action.create',
        sender: this,
        data: {
          requestInfo: {
            parameters: params
          },
          filteredHosts: [this.get('masterHostName')]
        },
        success: 'onCreateActionSuccess',
        error: 'onCreateActionError'
      });
    },
    /**
     * Run updater if task is created successfully.
     *
     * @method onConnectActionS
     **/
    onCreateActionSuccess: function onCreateActionSuccess(data) {
      this.set('currentRequestId', data.Requests.id);
      App.ajax.send({
        name: 'custom_action.request',
        sender: this,
        data: {
          requestId: this.get('currentRequestId')
        },
        success: 'setCurrentTaskId'
      });
    },
 
    setCurrentTaskId: function setCurrentTaskId(data) {
      this.set('currentTaskId', data.items[0].Tasks.id);
      this.startPolling();
    },
 
    startPolling: function startPolling() {
      if (this.get('isConnecting')) this.getTaskInfo();
    },
 
    getTaskInfo: function getTaskInfo() {
      var request = App.ajax.send({
        name: 'custom_action.request',
        sender: this,
        data: {
          requestId: this.get('currentRequestId'),
          taskId: this.get('currentTaskId')
        },
        success: 'getTaskInfoSuccess'
      });
      this.set('request', request);
    },
 
    getTaskInfoSuccess: function getTaskInfoSuccess(data) {
      var task = data.Tasks;
      this.set('responseFromServer', {
        stderr: task.stderr,
        stdout: task.stdout
      });
      if (task.status === 'COMPLETED') {
        var structuredOut = task.structured_out.db_connection_check;
        if (structuredOut.exit_code != 0) {
          this.set('responseFromServer', {
            stderr: task.stderr,
            stdout: task.stdout,
            structuredOut: structuredOut.message
          });
          this.setResponseStatus('failed');
        } else {
          App.db.set('tmp', this.get('parentView.service.serviceName') + '_connection', this.get('preparedDBProperties'));
          this.setResponseStatus('success');
        }
      }
      if (task.status === 'FAILED') {
        this.setResponseStatus('failed');
      }
      if (/PENDING|QUEUED|IN_PROGRESS/.test(task.status)) {
        Em.run.later(this, function () {
          this.startPolling();
        }, this.get('pollInterval'));
      }
    },
 
    onCreateActionError: function onCreateActionError(jqXhr, status, errorMessage) {
      this.setResponseStatus('failed');
      this.set('responseFromServer', errorMessage);
    },
 
    setResponseStatus: function setResponseStatus(isSuccess) {
      var isSuccess = isSuccess == 'success';
      this.setConnectingStatus(false);
      this.set('responseCaption', isSuccess ? Em.I18n.t('services.service.config.database.connection.success') : Em.I18n.t('services.service.config.database.connection.failed'));
      this.set('isConnectionSuccess', isSuccess);
      this.set('isRequestResolved', true);
      if (this.get('logsPopup')) {
        var statusString = isSuccess ? 'common.success' : 'common.error';
        this.set('logsPopup.header', Em.I18n.t('services.service.config.connection.logsPopup.header').format(this.get('databaseName'), Em.I18n.t(statusString)));
      }
    },
    /**
     * Switch captions and statuses for active/non-active request.
     *
     * @method setConnectionStatus
     * @param {Boolean} [active]
     */
    setConnectingStatus: function setConnectingStatus(active) {
      if (active) {
        this.set('responseCaption', Em.I18n.t('services.service.config.database.connection.inProgress'));
      }
      this.set('controller.testConnectionInProgress', !!active);
      this.set('btnCaption', !!active ? Em.I18n.t('services.service.config.database.btn.connecting') : Em.I18n.t('services.service.config.database.btn.idle'));
      this.set('isConnecting', !!active);
    },
    /**
     * Set view to init status.
     *
     * @method restore
     **/
    restore: function restore() {
      if (this.get('request')) {
        this.get('request').abort();
        this.set('request', null);
      }
      this.set('responseCaption', null);
      this.set('responseFromServer', null);
      this.setConnectingStatus(false);
      this.set('isRequestResolved', false);
    },
    /**
     * `Action` method for showing response from server in popup.
     *
     * @method showLogsPopup
     **/
    showLogsPopup: function showLogsPopup() {
      if (this.get('isConnectionSuccess')) return;
      var _this = this;
      var statusString = this.get('isRequestResolved') ? 'common.error' : 'common.testing';
      var popup = App.showAlertPopup(Em.I18n.t('services.service.config.connection.logsPopup.header').format(this.get('databaseName'), Em.I18n.t(statusString)), null, function () {
        _this.set('logsPopup', null);
      });
      popup.reopen({
        onClose: function onClose() {
          this._super();
          _this.set('logsPopup', null);
        }
      });
      if (_typeof(this.get('responseFromServer')) == 'object') {
        popup.set('bodyClass', Em.View.extend({
          checkDBConnectionView: _this,
          templateName: require('templates/common/error_log_body'),
          openedTask: function () {
            return this.get('checkDBConnectionView.responseFromServer');
          }.property('checkDBConnectionView.responseFromServer.stderr', 'checkDBConnectionView.responseFromServer.stdout', 'checkDBConnectionView.responseFromServer.structuredOut')
        }));
      } else {
        popup.set('body', this.get('responseFromServer'));
      }
      this.set('logsPopup', popup);
      return popup;
    }
  });
 
  /**
   * View for switch group text
   *
   * @type {Em.View}
   */
  App.SwitchToGroupView = Em.View.extend({
 
    group: null,
 
    tagName: 'a',
 
    classNames: ['action'],
 
    template: Ember.Handlebars.compile('{{ view.group.switchGroupTextShort }}'),
 
    didInsertElement: function didInsertElement() {
      var self = this;
      App.tooltip($(self.get('element')), {
        placement: 'top',
        title: self.get('group.switchGroupTextFull')
      });
    },
 
    willDestroyElement: function willDestroyElement() {
      $(this.get('element')).tooltip('destroy');
    },
 
    click: function click() {
      this.get('controller').selectConfigGroup({ context: this.get('group') });
    }
  });
 
  /**
   * View with input field used to repo-version URLs
   * @type {*}
   */
  App.BaseUrlTextField = Ember.TextField.extend({
 
    layout: Ember.Handlebars.compile('<div class="pull-left">{{yield}}</div> {{#if view.valueWasChanged}}<div class="pull-right"><a class="btn-sm" {{action "restoreValue" target="view"}}><i class="icon-undo"></i></a></div>{{/if}}'),
 
    /**
     * Binding in the template
     * @type {App.RepositoryVersion}
     */
    repository: null,
 
    /**
     * @type {string}
     */
    valueBinding: 'repository.baseUrl',
 
    /**
     * @type {string}
     */
    defaultValue: '',
 
    /**
     *  validate base URL
     */
    validate: function () {
      if (this.get('repository.skipValidation')) {
        this.set('repository.hasError', false);
      } else {
        this.set('repository.hasError', !validator.isValidBaseUrl(this.get('value')));
      }
      this.get('parentView').uiValidation();
    }.observes('value', 'repository.skipValidation'),
 
    /**
     * Determines if user have put some new value
     * @type {boolean}
     */
    valueWasChanged: function () {
      return this.get('value') !== this.get('defaultValue');
    }.property('value', 'defaultValue'),
 
    didInsertElement: function didInsertElement() {
      this.set('defaultValue', this.get('value'));
    },
 
    /**
     * Restore value and unset error-flag
     * @method restoreValue
     */
    restoreValue: function restoreValue() {
      this.set('value', this.get('defaultValue'));
    }
  });
});