LoadOptionsPane.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Cognos Analytics
  5. * Copyright IBM Corp. 2016, 2021
  6. * US Government Users Restricted Rights - Use, duplication or disclosure
  7. * restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['underscore', 'q', 'jquery', 'bi/admin/nls/StringResource', 'bi/admin/common/slideout/BasePane', 'bi/commons/ui/properties/PropertyUIControl', 'bi/admin/datasource/App'], function (_, Q, $, StringResource, BasePane, PropertyUIControl, App) {
  10. var STATUS_NOT_LOADED = 'not_loaded';
  11. var SCHEMA_POST_KEY_LIST = ['schema', 'catalog', 'defaultName', 'type', 'schemaType'];
  12. var SCHEMA_PUT_KEY_LIST = ['type'];
  13. var LoadOptionsPane = BasePane.extend({
  14. _state: null,
  15. //reduced props
  16. _prop: null,
  17. //props to be persisted.
  18. _currentProp: null,
  19. //original schemas
  20. _schemas: null,
  21. items: null,
  22. _isSingleSelection: true,
  23. init: function init(options) {
  24. LoadOptionsPane.inherited('init', this, arguments);
  25. _.extend(this, options);
  26. this._state = {
  27. 'loadBtnDisabled': false,
  28. 'includeOrExcludeSelection': 'include'
  29. };
  30. this._isSingleSelection = this.items.length === 1;
  31. this.$el.addClass('admin-schema-load-options-pane');
  32. },
  33. _getPropKeys: function _getPropKeys() {
  34. return ['dataSamplingSize', 'dataStatistics', 'excludedTables', 'importPrimaryForeignKeys'];
  35. },
  36. _updateProps: function _updateProps(schemas) {
  37. this._schemas = $.extend(true, {}, {
  38. 'toBeCloned': schemas
  39. }).toBeCloned;
  40. var props = _.map(schemas, function (s) {
  41. return s.specification;
  42. }.bind(this));
  43. this._prop = _.reduce(props, function (memo, prop) {
  44. for (var p in prop) {
  45. if (_.indexOf(this._getPropKeys(), p) > -1) {
  46. if (memo[p] !== prop[p]) {
  47. prop[p] = 'mixed';
  48. }
  49. } else {
  50. delete prop[p];
  51. }
  52. }
  53. return prop;
  54. }.bind(this));
  55. $.extend(true, this._state, this._prop);
  56. },
  57. updateState: function updateState(key, value, withoutRedraw) {
  58. var obj = {};
  59. if ($.type(key) !== 'string') {
  60. obj = key;
  61. } else {
  62. obj[key] = value;
  63. }
  64. $.extend(this._state, obj); //update prop
  65. this._currentProp = _.pick(this._state, _.keys(this._prop));
  66. if (!withoutRedraw) {
  67. this._redraw();
  68. }
  69. },
  70. _redraw: function _redraw() {
  71. var loadBtn = this._oPropertyUIControl.getProperty('footer')._oPropertyUIControl.getProperty('load');
  72. if (this._state.loadBtnDisabled) {
  73. loadBtn.disable();
  74. } else {
  75. loadBtn.enable();
  76. }
  77. },
  78. _checkForVendorSpec: function _checkForVendorSpec(item, prop) {
  79. if (item.specification && item.specification.vendor && item.specification.vendor.name) {
  80. return $.extend(item.specification, prop);
  81. } else {
  82. return prop;
  83. }
  84. },
  85. _loadBtnHandeller: function _loadBtnHandeller() {
  86. var schemaDefs = []; //pick up non-mixed from current prop, otherwise use the original prop.
  87. _.each(this.items, function (item, idx) {
  88. var prop = {};
  89. for (var p in this._currentProp) {
  90. if (this._currentProp[p] === 'mixed') {
  91. prop[p] = this._schemas[idx].specification[p];
  92. } else {
  93. prop[p] = this._currentProp[p];
  94. }
  95. } //Switch selectedTables list to either includeTables or excludedTables based on the radio button selected
  96. if (this._state.includeOrExcludeSelection === 'exclude') {
  97. prop.excludedTables = prop.selectedTables;
  98. } else if (this._state.includeOrExcludeSelection === 'include') {
  99. prop.includedTables = prop.selectedTables;
  100. }
  101. delete prop.selectedTables; //to update
  102. if (this._isLoaded(item)) {
  103. var schemaToPut = $.extend(true, _.pick(this._schemas[idx], SCHEMA_PUT_KEY_LIST), {
  104. 'specification': this._checkForVendorSpec(item, prop),
  105. 'defaultName': item.defaultName
  106. });
  107. schemaDefs.push(App.updateSchema(item.id, schemaToPut).then(function () {
  108. this.parentView.RefreshMetadata(item.id);
  109. }.bind(this))); //to create new
  110. } else {
  111. var schemaToPost = $.extend(true, _.pick(this._schemas[idx], SCHEMA_POST_KEY_LIST), {
  112. 'specification': this._checkForVendorSpec(item, prop),
  113. 'defaultName': item.defaultName,
  114. 'status': 'pending'
  115. });
  116. schemaDefs.push(App.createSchemaSource(item.connId, schemaToPost).then(function (response) {
  117. //retrieve cm storeId for newly created schema to post baseModule
  118. var location = response.getResponseHeader('Location');
  119. var arr = location.split('/');
  120. var schemaId = arr[arr.length - 1];
  121. $.extend(item, {
  122. 'id': schemaId
  123. });
  124. this.parentView.render(true);
  125. this.parentView.RefreshMetadata(item.id);
  126. }.bind(this)));
  127. }
  128. }.bind(this));
  129. $.when.apply(this, schemaDefs).then().fail();
  130. this.close();
  131. },
  132. _isLoaded: function _isLoaded(item) {
  133. return item.status && item.status !== STATUS_NOT_LOADED;
  134. },
  135. _cancelBtnHandeller: function _cancelBtnHandeller() {
  136. this.close();
  137. },
  138. _buildSelectedTableList: function _buildSelectedTableList() {
  139. if (this._prop.excludedTables) {
  140. this._state.selectedTables = this._prop.excludedTables;
  141. this._state.includeOrExcludeSelection = 'exclude';
  142. this._prop.selectedTables = this._prop.excludedTables;
  143. delete this._prop.excludedTables;
  144. this.updateState('loadBtnDisabled', false, true);
  145. } else if (this._prop.includedTables) {
  146. this._state.selectedTables = this._prop.includedTables;
  147. this._state.includeOrExcludeSelection = 'include';
  148. this._prop.selectedTables = this._prop.includedTables;
  149. delete this._prop.includedTables;
  150. this.updateState('loadBtnDisabled', true, true);
  151. }
  152. },
  153. renderBody: function renderBody() {
  154. var schemaDefs = [];
  155. _.each(this.items, function (item) {
  156. schemaDefs.push(App.getSchema(item.id, this.signonAndConnection));
  157. }.bind(this));
  158. return Promise.all(schemaDefs).then(function (data) {
  159. this._updateProps(_.values(data)); //Take the includedTables or excludedTables returned from Moser and put them into a new selectedTabled list
  160. this._buildSelectedTableList();
  161. var tabControlItems = [];
  162. tabControlItems.push({
  163. 'name': StringResource.get('loadOptions'),
  164. 'module': 'bi/admin/datasource/ui/LoadOptionsView',
  165. 'parentView': this,
  166. 'slideout': this.slideout,
  167. 'prop': _.omit(this._state, 'excludedTables')
  168. });
  169. if (this._isSingleSelection) {
  170. tabControlItems.push({
  171. 'name': StringResource.get('tables'),
  172. 'module': 'bi/admin/datasource/ui/TableListView',
  173. 'parentView': this,
  174. 'slideout': this.slideout,
  175. 'schemaId': this.items[0].id,
  176. 'prop': _.pick(this._state, ['selectedTables', 'includeOrExcludeSelection'])
  177. });
  178. }
  179. var items = [{
  180. 'type': 'TabControl',
  181. 'items': tabControlItems
  182. }, {
  183. 'name': 'footer',
  184. 'type': 'Footer',
  185. 'items': [{
  186. 'name': 'load',
  187. 'type': 'Button',
  188. 'label': StringResource.get('load'),
  189. 'onSelect': this._loadBtnHandeller.bind(this),
  190. 'primary': true
  191. }, {
  192. 'name': 'cancel',
  193. 'type': 'Button',
  194. 'label': StringResource.get('cancel'),
  195. 'onSelect': this._cancelBtnHandeller.bind(this)
  196. }]
  197. }];
  198. this._oPropertyUIControl = this._getNewPropUIcontrol(items);
  199. return this._oPropertyUIControl.render().then(function () {
  200. this._redraw();
  201. return this._oPropertyUIControl;
  202. }.bind(this));
  203. }.bind(this));
  204. },
  205. _getNewPropUIcontrol: function _getNewPropUIcontrol(items) {
  206. return new PropertyUIControl({
  207. 'glassContext': this.glassContext,
  208. 'el': this.$body,
  209. 'items': items
  210. });
  211. },
  212. setFocus: function setFocus() {
  213. this.$el.find('ul.nav-tabs li.active a').focus();
  214. }
  215. });
  216. return LoadOptionsPane;
  217. });