123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- 'use strict';
- /*
- *+------------------------------------------------------------------------+
- *| Licensed Materials - Property of IBM
- *| IBM Cognos Products: BI Dashboard
- *| (C) Copyright IBM Corp. 2017, 2019
- *|
- *| US Government Users Restricted Rights - Use, duplication or disclosure
- *| restricted by GSA ADP Schedule Contract with IBM Corp.
- *+------------------------------------------------------------------------+
- */
- /**
- * This Class encapsulates raw query result data returned from DSS and provides its accessors and iterators
- **/
- define(['../../../lib/@waca/core-client/js/core-client/ui/core/Class', 'underscore', './QueryResultDataUtils', './QueryResultDataItem'], function (BaseClass, _, Utils, DataItem) {
- 'use strict';
- var RowIterator = BaseClass.extend({
- _rows: null,
- _index: -1,
- _oQueryResultData: null,
- init: function init(rows) {
- this._index = -1;
- this._rows = rows;
- },
- setQueryResultDataRef: function setQueryResultDataRef(queryResultData) {
- this._oQueryResultData = queryResultData;
- },
- hasNext: function hasNext() {
- return this._rows && this._rows.length > 0 && this._index < this._rows.length - 1;
- },
- next: function next() {
- this._index++;
- },
- /**
- * @return the ordinal index of categorical value or the continuous value itself
- **/
- getValue: function getValue(datapointIndex) {
- var oDatapoint = this._getDatapoint(datapointIndex);
- if (_.isObject(oDatapoint) && oDatapoint.v !== undefined) {
- return oDatapoint.v;
- }
- return oDatapoint;
- },
- /**
- * @return the resolved value
- **/
- getResolvedValue: function getResolvedValue(datapointIndex) {
- var oDatapoint = this._getDatapoint(datapointIndex);
- if (_.isObject(oDatapoint) && oDatapoint.v !== undefined) {
- Utils.normalizeData(oDatapoint);
- return oDatapoint;
- }
- if (this._oQueryResultData) {
- return this._oQueryResultData.getResultDataItem(datapointIndex).getTuple(oDatapoint);
- }
- return null;
- },
- _getDatapoint: function _getDatapoint(datapointIndex) {
- if (this._index >= 0) {
- var oDatapoint = this._rows[this._index];
- if (oDatapoint && oDatapoint.pt && oDatapoint.pt.length > 0) {
- return oDatapoint.pt[datapointIndex];
- }
- }
- return null;
- }
- });
- var QueryResultData = BaseClass.extend({
- /**
- * Internal value that refers to the original query result data object
- * @private
- * @member {Object}
- **/
- _resultData: null,
- _aQueryResultDataItems: null, //Array of QueryResultDataItem Class. Init to null to indicate initial state
- init: function init(data) {
- QueryResultData.inherited('init', this, arguments);
- this._resultData = data;
- },
- /**
- * @param {Integer} rowIndex row index
- * @param {Integer} colIndex column index
- * @return The referenced tuple/value that composes the data point located by the indexes
- **/
- getCellValue: function getCellValue(rowIndex, colIndex) {
- var result;
- var value = this._getCellRefByIndexes(rowIndex, colIndex);
- if (Utils.isRectifyNeeded(value)) {
- result = Utils.getRectifiedData(value);
- } else if (value.v !== undefined || value.value !== undefined) {
- result = value;
- Utils.normalizeData(result);
- } else {
- result = this.getResultDataItem(colIndex).getTuple(value) || []; /*Categorical data item object*/
- }
- return result;
- },
- /**
- * @param {Integer} rowIndex row index
- * @param {Integer} colIndex column index
- * @return The index/value that composes the data point located by the indexes
- **/
- _getCellRefByIndexes: function _getCellRefByIndexes(rowIndex, colIndex) {
- var oPoint = this._getDatapointEntry(rowIndex);
- if (oPoint && oPoint.pt) {
- var aPoint = oPoint.pt;
- if (aPoint && aPoint.length > 0 && colIndex !== undefined && colIndex < aPoint.length) {
- return aPoint[colIndex];
- }
- }
- return null;
- },
- _getDatapointEntry: function _getDatapointEntry(rowIndex) {
- if (rowIndex >= 0 && this._resultData && this._resultData.data) {
- return this._resultData.data[rowIndex];
- }
- return null;
- },
- getDatapoints: function getDatapoints() {
- return this._resultData.data;
- },
- /**
- * @return {Integer} the count of datapoints in the query result data response
- **/
- getDatapointCount: function getDatapointCount() {
- if (this._resultData && this._resultData.data) {
- return this._resultData.data.length;
- }
- return 0;
- },
- /**
- * Reset the data items object array
- * This is called after post-processing when the data is restructured
- */
- resetDataItemArray: function resetDataItemArray() {
- this._generateDataItemArray();
- },
- _isValidDataItem: function _isValidDataItem(dataItem) {
- var _fIsValidItemClass = function _fIsValidItemClass(itemClassEntry) {
- return itemClassEntry.h[0].d !== null && itemClassEntry.h[0].u !== null;
- };
- //A data item could have multiple item class sections, while each item Class section could have multiple item class enties
- if (_.isArray(dataItem.itemClass)) {
- var bResult = true;
- _.each(dataItem.itemClass, function (itemClassEntry) {
- if (!_fIsValidItemClass(itemClassEntry)) {
- bResult = false;
- return;
- }
- });
- return bResult;
- } else if (_.isObject(dataItem.itemClass)) {
- return _fIsValidItemClass(dataItem.itemClass);
- }
- return false;
- },
- _generateDataItemArray: function _generateDataItemArray() {
- this._aQueryResultDataItems = [];
- if (this._resultData && this._resultData.dataItems && this._resultData.dataItems.length > 0) {
- _.each(this._resultData.dataItems, function (dataItem) {
- if (this._isValidDataItem(dataItem)) {
- this._aQueryResultDataItems.push(new DataItem(dataItem));
- }
- }.bind(this));
- }
- },
- /**
- * @return {Number} the Query result data format version
- **/
- getVersion: function getVersion() {
- if (this._resultData) {
- return this._resultData.version;
- }
- return null;
- },
- /**
- * @return {Integer} the count of the data items
- **/
- getResultDataItemCount: function getResultDataItemCount() {
- if (this._resultData) {
- return (_.filter(this._resultData.dataItems, function (dataItem) {
- return this._isValidDataItem(dataItem);
- }.bind(this)) || []).length;
- }
- return 0;
- },
- /**
- * @return {QueryResultDataItem} One QueryResultDataItem Object located at the index
- */
- getResultDataItem: function getResultDataItem(index) {
- if (!this._aQueryResultDataItems) {
- this._generateDataItemArray();
- }
- return this._aQueryResultDataItems[index];
- },
- /**
- * @return {Array} An array of all QueryResultDataItem objects
- **/
- getResultDataItems: function getResultDataItems() {
- if (this._resultData) {
- if (!this._aQueryResultDataItems) {
- this._generateDataItemArray();
- }
- return this._aQueryResultDataItems;
- }
- return [];
- },
- /**
- * @return {Array} An array of all QueryResultDataItem objects
- **/
- getResultDataItemsNoValidation: function getResultDataItemsNoValidation() {
- var aQRDataItems = [];
- this._resultData.dataItems.forEach(function (dataItem) {
- aQRDataItems.push(new DataItem(dataItem));
- });
- return aQRDataItems;
- },
- /**
- * @param {String} dataItemRefID Data item reference ID, Unique ID A.K.A Projection ID
- * @return {Integer} the index number of the DataItem
- **/
- getDataItemIndex: function getDataItemIndex(dataItemRefID) {
- var nIndex = -1;
- if (this._resultData) {
- _.find(this.getResultDataItems(), function (dataItem, index) {
- if (dataItem.getTupleHeaderId() === dataItemRefID) {
- nIndex = index;
- return true;
- }
- });
- }
- return nIndex;
- },
- /**
- * @param {String} dataItemRefID Data item reference ID, Unique ID A.K.A Projection ID
- * @return {QueryResultDataItem} QueryResultDataItem Object with data item reference ID
- */
- getDataItem: function getDataItem(dataItemRefID) {
- return this.getResultDataItem(this.getDataItemIndex(dataItemRefID));
- },
- /**
- * Create and return a data row iterator instance
- * @return {DataRowIterator}
- **/
- getDataRowIterator: function getDataRowIterator() {
- var oIterator = new RowIterator(this._resultData.data);
- oIterator.setQueryResultDataRef(this);
- return oIterator;
- },
- /**
- * @return {Array} two dimensions array that stores the resolved values from query result data datapoints
- **/
- getResolvedDataRows: function getResolvedDataRows() {
- var nDatapointCount = this.getDatapointCount();
- var nDataItemCount = this.getResultDataItemCount();
- var aResult = [];
- for (var i = 0; i < nDatapointCount; i++) {
- aResult[i] = [];
- for (var j = 0; j < nDataItemCount; j++) {
- aResult[i].push(this.getCellValue(i, j));
- }
- }
- return aResult;
- },
- /**
- * @return {boolean} true if resultData includes hasNext or hasPrev true
- **/
- hasMoreData: function hasMoreData() {
- if (this._resultData) {
- return !!(this._resultData.hasPrev || this._resultData.hasNext);
- }
- return false;
- }
- });
- return QueryResultData;
- });
- //# sourceMappingURL=QueryResultData.js.map
|