"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: Cognos Analytics * Copyright IBM Corp. 2015, 2019 * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['underscore', 'bi/commons/ui/core/Class', 'doT', 'bi/admin/globalparameters/services/GlobalParametersService', 'bi/admin/multitenancy/services/TenantsCustomizationService', 'text!bi/admin/common/templates/systemUserProfileSettings.xml', 'text!bi/admin/common/templates/updateSystemUserProfileSettings.xml', 'text!bi/admin/common/templates/queryForTenant.xml', 'bi/admin/globalparameters/helpers/SoapHelper', 'bi/admin/common/utils/XMLUtils'], function (_, Class, doT, GlobalParametersService, TenantsCustomizationService, GetSystemUserProfileSettings, UpdateSystemUserProfileSettings, QueryForTenant, SoapHelper) { var _tenantId = null; var GlobalParameters = Class.extend({ init: function init(options) { GlobalParameters.inherited('init', this, arguments); _.extend(this, options); this._systemUserProfileSettings = null; this._tenantProfileSettings = null; this._globalParametersService = this._getNewGlobalParametersService({ glassContext: this.glassContext }); }, getParameters: function getParameters(isSystemAdmin, isPortalAdmin, tenantID) { if (!isSystemAdmin && isPortalAdmin && tenantID) { _tenantId = tenantID; return this.getTenantParameters(); } else { return this.getSystemParameters(); } }, updateParameters: function updateParameters(parameters, isSystemAdmin, isPortalAdmin, tenantID) { var convertedParameters = {}; for (var i = 0; i < parameters.length; ++i) { convertedParameters[parameters[i].name] = parameters[i]; convertedParameters[parameters[i].name].ordinal = i; } if (!isSystemAdmin && isPortalAdmin && tenantID) { this._getNewTenantsCustomizationService({ glassContext: this.glassContext }).setParameters(tenantID, convertedParameters); } else { this.updateSystemParameters(convertedParameters); } }, _getNewGlobalParametersService: function _getNewGlobalParametersService(options) { return new GlobalParametersService(options); }, _getSystemUserProfileSettings: function _getSystemUserProfileSettings() { var getSystemUserProfileSettingsRequest = doT.template(GetSystemUserProfileSettings)({ 'cafContextId': this.glassContext.authInfo.cafContextId }); return this._globalParametersService.getSystemUserProfileSettings(getSystemUserProfileSettingsRequest).then(function (response) { var $userProfileSettingsNode = $(response.data).selectNode('Envelope').selectNode('Body').selectNode('queryResponse').selectNode('result').selectNode('item').selectNode('userProfileSettings').selectNode('value'); var userProfileSettingsText = $userProfileSettingsNode.text(); if (userProfileSettingsText !== '') { this._systemUserProfileSettings = JSON.parse(userProfileSettingsText); } else { this._systemUserProfileSettings = {}; } return this._systemUserProfileSettings; }.bind(this)).catch(function (response) { return {}; }.bind(this)); }, _getNewTenantsCustomizationService: function _getNewTenantsCustomizationService(options) { return new TenantsCustomizationService(options); }, _getTenantUserProfileSettings: function _getTenantUserProfileSettings() { return this._getTenantId().then(function (tenantId) { if (tenantId === '') { return {}; } else { var tenantsController = this._getNewTenantsCustomizationService({ glassContext: this.glassContext }); return tenantsController.getCustomizations(tenantId); } }.bind(this)).then(function (tenantCustomizations) { this._tenantProfileSettings = tenantCustomizations; return this._tenantProfileSettings; }.bind(this)); }, getSystemParameters: function getSystemParameters() { return Promise.try(function () { if (this._systemUserProfileSettings) { return this._systemUserProfileSettings; } else { return this._getSystemUserProfileSettings(); } }.bind(this)).then(this._getParameters.bind(this)); }, getTenantParameters: function getTenantParameters() { return Promise.try(function () { if (this._tenantProfileSettings) { return this._tenantProfileSettings; } else { return this._getTenantUserProfileSettings(); } }.bind(this)).then(this._getParameters.bind(this)); }, updateSystemParameters: function updateSystemParameters(systemParameters) { return this._getSystemUserProfileSettings().then(function (userProfileSettings) { userProfileSettings.parameters = systemParameters; var updateSystemUserProfileSettingsRequest = doT.template(UpdateSystemUserProfileSettings)({ 'cafContextId': this.glassContext.authInfo.cafContextId, 'userProfileSettings': SoapHelper.xml_encode(JSON.stringify(userProfileSettings)) }); return this._globalParametersService.getSystemUserProfileSettings(updateSystemUserProfileSettingsRequest).catch(function (response) { return null; }.bind(this)); }.bind(this)); }, _getTenantId: function _getTenantId() { return Promise.try(function () { if (_tenantId === null) { var queryForTenant = doT.template(QueryForTenant)({ 'cafContextId': this.glassContext.authInfo.cafContextId }); return this._globalParametersService.getSystemUserProfileSettings(queryForTenant).then(function (response) { var $envelope = $(response.data).selectNode('Envelope'); var $tenantValue = $envelope.selectNode('Body').find('*').find('*').selectNode('item').selectNode('tenantID').selectNode('value'); _tenantId = $tenantValue.text(); }); } }.bind(this)).then(function () { return _tenantId; }); }, getDictionaryValues: function getDictionaryValues() { var systemParametersPromise = this.getSystemParameters(); var tenantParametersPromise = this.getTenantParameters(); return Promise.all([tenantParametersPromise, systemParametersPromise]).then(function (parameterEntries) { var dictionary = {}; _.each(parameterEntries, function (parameterEntry) { _.each(parameterEntry, function (parameter) { if (!dictionary[parameter.name]) { dictionary[parameter.name] = parameter; } }); }); return dictionary; }); }, _getParameters: function _getParameters(userProfileSettings) { var parameters = _.map(userProfileSettings.parameters, function (parameter) { return parameter; }); return parameters; } }); return GlobalParameters; });