GlobalParameters.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Cognos Analytics
  5. * Copyright IBM Corp. 2015, 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure
  7. * restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. 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) {
  10. var _tenantId = null;
  11. var GlobalParameters = Class.extend({
  12. init: function init(options) {
  13. GlobalParameters.inherited('init', this, arguments);
  14. _.extend(this, options);
  15. this._systemUserProfileSettings = null;
  16. this._tenantProfileSettings = null;
  17. this._globalParametersService = this._getNewGlobalParametersService({
  18. glassContext: this.glassContext
  19. });
  20. },
  21. getParameters: function getParameters(isSystemAdmin, isPortalAdmin, tenantID) {
  22. if (!isSystemAdmin && isPortalAdmin && tenantID) {
  23. _tenantId = tenantID;
  24. return this.getTenantParameters();
  25. } else {
  26. return this.getSystemParameters();
  27. }
  28. },
  29. updateParameters: function updateParameters(parameters, isSystemAdmin, isPortalAdmin, tenantID) {
  30. var convertedParameters = {};
  31. for (var i = 0; i < parameters.length; ++i) {
  32. convertedParameters[parameters[i].name] = parameters[i];
  33. convertedParameters[parameters[i].name].ordinal = i;
  34. }
  35. if (!isSystemAdmin && isPortalAdmin && tenantID) {
  36. this._getNewTenantsCustomizationService({
  37. glassContext: this.glassContext
  38. }).setParameters(tenantID, convertedParameters);
  39. } else {
  40. this.updateSystemParameters(convertedParameters);
  41. }
  42. },
  43. _getNewGlobalParametersService: function _getNewGlobalParametersService(options) {
  44. return new GlobalParametersService(options);
  45. },
  46. _getSystemUserProfileSettings: function _getSystemUserProfileSettings() {
  47. var getSystemUserProfileSettingsRequest = doT.template(GetSystemUserProfileSettings)({
  48. 'cafContextId': this.glassContext.authInfo.cafContextId
  49. });
  50. return this._globalParametersService.getSystemUserProfileSettings(getSystemUserProfileSettingsRequest).then(function (response) {
  51. var $userProfileSettingsNode = $(response.data).selectNode('Envelope').selectNode('Body').selectNode('queryResponse').selectNode('result').selectNode('item').selectNode('userProfileSettings').selectNode('value');
  52. var userProfileSettingsText = $userProfileSettingsNode.text();
  53. if (userProfileSettingsText !== '') {
  54. this._systemUserProfileSettings = JSON.parse(userProfileSettingsText);
  55. } else {
  56. this._systemUserProfileSettings = {};
  57. }
  58. return this._systemUserProfileSettings;
  59. }.bind(this)).catch(function (response) {
  60. return {};
  61. }.bind(this));
  62. },
  63. _getNewTenantsCustomizationService: function _getNewTenantsCustomizationService(options) {
  64. return new TenantsCustomizationService(options);
  65. },
  66. _getTenantUserProfileSettings: function _getTenantUserProfileSettings() {
  67. return this._getTenantId().then(function (tenantId) {
  68. if (tenantId === '') {
  69. return {};
  70. } else {
  71. var tenantsController = this._getNewTenantsCustomizationService({
  72. glassContext: this.glassContext
  73. });
  74. return tenantsController.getCustomizations(tenantId);
  75. }
  76. }.bind(this)).then(function (tenantCustomizations) {
  77. this._tenantProfileSettings = tenantCustomizations;
  78. return this._tenantProfileSettings;
  79. }.bind(this));
  80. },
  81. getSystemParameters: function getSystemParameters() {
  82. return Promise.try(function () {
  83. if (this._systemUserProfileSettings) {
  84. return this._systemUserProfileSettings;
  85. } else {
  86. return this._getSystemUserProfileSettings();
  87. }
  88. }.bind(this)).then(this._getParameters.bind(this));
  89. },
  90. getTenantParameters: function getTenantParameters() {
  91. return Promise.try(function () {
  92. if (this._tenantProfileSettings) {
  93. return this._tenantProfileSettings;
  94. } else {
  95. return this._getTenantUserProfileSettings();
  96. }
  97. }.bind(this)).then(this._getParameters.bind(this));
  98. },
  99. updateSystemParameters: function updateSystemParameters(systemParameters) {
  100. return this._getSystemUserProfileSettings().then(function (userProfileSettings) {
  101. userProfileSettings.parameters = systemParameters;
  102. var updateSystemUserProfileSettingsRequest = doT.template(UpdateSystemUserProfileSettings)({
  103. 'cafContextId': this.glassContext.authInfo.cafContextId,
  104. 'userProfileSettings': SoapHelper.xml_encode(JSON.stringify(userProfileSettings))
  105. });
  106. return this._globalParametersService.getSystemUserProfileSettings(updateSystemUserProfileSettingsRequest).catch(function (response) {
  107. return null;
  108. }.bind(this));
  109. }.bind(this));
  110. },
  111. _getTenantId: function _getTenantId() {
  112. return Promise.try(function () {
  113. if (_tenantId === null) {
  114. var queryForTenant = doT.template(QueryForTenant)({
  115. 'cafContextId': this.glassContext.authInfo.cafContextId
  116. });
  117. return this._globalParametersService.getSystemUserProfileSettings(queryForTenant).then(function (response) {
  118. var $envelope = $(response.data).selectNode('Envelope');
  119. var $tenantValue = $envelope.selectNode('Body').find('*').find('*').selectNode('item').selectNode('tenantID').selectNode('value');
  120. _tenantId = $tenantValue.text();
  121. });
  122. }
  123. }.bind(this)).then(function () {
  124. return _tenantId;
  125. });
  126. },
  127. getDictionaryValues: function getDictionaryValues() {
  128. var systemParametersPromise = this.getSystemParameters();
  129. var tenantParametersPromise = this.getTenantParameters();
  130. return Promise.all([tenantParametersPromise, systemParametersPromise]).then(function (parameterEntries) {
  131. var dictionary = {};
  132. _.each(parameterEntries, function (parameterEntry) {
  133. _.each(parameterEntry, function (parameter) {
  134. if (!dictionary[parameter.name]) {
  135. dictionary[parameter.name] = parameter;
  136. }
  137. });
  138. });
  139. return dictionary;
  140. });
  141. },
  142. _getParameters: function _getParameters(userProfileSettings) {
  143. var parameters = _.map(userProfileSettings.parameters, function (parameter) {
  144. return parameter;
  145. });
  146. return parameters;
  147. }
  148. });
  149. return GlobalParameters;
  150. });