"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: Cognos Analytics * Copyright IBM Corp. 2016, 2021 * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ 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) { var STATUS_NOT_LOADED = 'not_loaded'; var SCHEMA_POST_KEY_LIST = ['schema', 'catalog', 'defaultName', 'type', 'schemaType']; var SCHEMA_PUT_KEY_LIST = ['type']; var LoadOptionsPane = BasePane.extend({ _state: null, //reduced props _prop: null, //props to be persisted. _currentProp: null, //original schemas _schemas: null, items: null, _isSingleSelection: true, init: function init(options) { LoadOptionsPane.inherited('init', this, arguments); _.extend(this, options); this._state = { 'loadBtnDisabled': false, 'includeOrExcludeSelection': 'include' }; this._isSingleSelection = this.items.length === 1; this.$el.addClass('admin-schema-load-options-pane'); }, _getPropKeys: function _getPropKeys() { return ['dataSamplingSize', 'dataStatistics', 'excludedTables', 'importPrimaryForeignKeys']; }, _updateProps: function _updateProps(schemas) { this._schemas = $.extend(true, {}, { 'toBeCloned': schemas }).toBeCloned; var props = _.map(schemas, function (s) { return s.specification; }.bind(this)); this._prop = _.reduce(props, function (memo, prop) { for (var p in prop) { if (_.indexOf(this._getPropKeys(), p) > -1) { if (memo[p] !== prop[p]) { prop[p] = 'mixed'; } } else { delete prop[p]; } } return prop; }.bind(this)); $.extend(true, this._state, this._prop); }, updateState: function updateState(key, value, withoutRedraw) { var obj = {}; if ($.type(key) !== 'string') { obj = key; } else { obj[key] = value; } $.extend(this._state, obj); //update prop this._currentProp = _.pick(this._state, _.keys(this._prop)); if (!withoutRedraw) { this._redraw(); } }, _redraw: function _redraw() { var loadBtn = this._oPropertyUIControl.getProperty('footer')._oPropertyUIControl.getProperty('load'); if (this._state.loadBtnDisabled) { loadBtn.disable(); } else { loadBtn.enable(); } }, _checkForVendorSpec: function _checkForVendorSpec(item, prop) { if (item.specification && item.specification.vendor && item.specification.vendor.name) { return $.extend(item.specification, prop); } else { return prop; } }, _loadBtnHandeller: function _loadBtnHandeller() { var schemaDefs = []; //pick up non-mixed from current prop, otherwise use the original prop. _.each(this.items, function (item, idx) { var prop = {}; for (var p in this._currentProp) { if (this._currentProp[p] === 'mixed') { prop[p] = this._schemas[idx].specification[p]; } else { prop[p] = this._currentProp[p]; } } //Switch selectedTables list to either includeTables or excludedTables based on the radio button selected if (this._state.includeOrExcludeSelection === 'exclude') { prop.excludedTables = prop.selectedTables; } else if (this._state.includeOrExcludeSelection === 'include') { prop.includedTables = prop.selectedTables; } delete prop.selectedTables; //to update if (this._isLoaded(item)) { var schemaToPut = $.extend(true, _.pick(this._schemas[idx], SCHEMA_PUT_KEY_LIST), { 'specification': this._checkForVendorSpec(item, prop), 'defaultName': item.defaultName }); schemaDefs.push(App.updateSchema(item.id, schemaToPut).then(function () { this.parentView.RefreshMetadata(item.id); }.bind(this))); //to create new } else { var schemaToPost = $.extend(true, _.pick(this._schemas[idx], SCHEMA_POST_KEY_LIST), { 'specification': this._checkForVendorSpec(item, prop), 'defaultName': item.defaultName, 'status': 'pending' }); schemaDefs.push(App.createSchemaSource(item.connId, schemaToPost).then(function (response) { //retrieve cm storeId for newly created schema to post baseModule var location = response.getResponseHeader('Location'); var arr = location.split('/'); var schemaId = arr[arr.length - 1]; $.extend(item, { 'id': schemaId }); this.parentView.render(true); this.parentView.RefreshMetadata(item.id); }.bind(this))); } }.bind(this)); $.when.apply(this, schemaDefs).then().fail(); this.close(); }, _isLoaded: function _isLoaded(item) { return item.status && item.status !== STATUS_NOT_LOADED; }, _cancelBtnHandeller: function _cancelBtnHandeller() { this.close(); }, _buildSelectedTableList: function _buildSelectedTableList() { if (this._prop.excludedTables) { this._state.selectedTables = this._prop.excludedTables; this._state.includeOrExcludeSelection = 'exclude'; this._prop.selectedTables = this._prop.excludedTables; delete this._prop.excludedTables; this.updateState('loadBtnDisabled', false, true); } else if (this._prop.includedTables) { this._state.selectedTables = this._prop.includedTables; this._state.includeOrExcludeSelection = 'include'; this._prop.selectedTables = this._prop.includedTables; delete this._prop.includedTables; this.updateState('loadBtnDisabled', true, true); } }, renderBody: function renderBody() { var schemaDefs = []; _.each(this.items, function (item) { schemaDefs.push(App.getSchema(item.id, this.signonAndConnection)); }.bind(this)); return Promise.all(schemaDefs).then(function (data) { this._updateProps(_.values(data)); //Take the includedTables or excludedTables returned from Moser and put them into a new selectedTabled list this._buildSelectedTableList(); var tabControlItems = []; tabControlItems.push({ 'name': StringResource.get('loadOptions'), 'module': 'bi/admin/datasource/ui/LoadOptionsView', 'parentView': this, 'slideout': this.slideout, 'prop': _.omit(this._state, 'excludedTables') }); if (this._isSingleSelection) { tabControlItems.push({ 'name': StringResource.get('tables'), 'module': 'bi/admin/datasource/ui/TableListView', 'parentView': this, 'slideout': this.slideout, 'schemaId': this.items[0].id, 'prop': _.pick(this._state, ['selectedTables', 'includeOrExcludeSelection']) }); } var items = [{ 'type': 'TabControl', 'items': tabControlItems }, { 'name': 'footer', 'type': 'Footer', 'items': [{ 'name': 'load', 'type': 'Button', 'label': StringResource.get('load'), 'onSelect': this._loadBtnHandeller.bind(this), 'primary': true }, { 'name': 'cancel', 'type': 'Button', 'label': StringResource.get('cancel'), 'onSelect': this._cancelBtnHandeller.bind(this) }] }]; this._oPropertyUIControl = this._getNewPropUIcontrol(items); return this._oPropertyUIControl.render().then(function () { this._redraw(); return this._oPropertyUIControl; }.bind(this)); }.bind(this)); }, _getNewPropUIcontrol: function _getNewPropUIcontrol(items) { return new PropertyUIControl({ 'glassContext': this.glassContext, 'el': this.$body, 'items': items }); }, setFocus: function setFocus() { this.$el.find('ul.nav-tabs li.active a').focus(); } }); return LoadOptionsPane; });