/* * Licensed Materials - Property of IBM * * IBM Cognos Products: SHARE * * (C) Copyright IBM Corp. 2015, 2021 * * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define([ 'bi/glass/core/Events', 'jquery', 'bi/sharecommon/utils/translator' ], function(Events, $, t) { var DISPATCHER_URI = 'v1/disp'; var SERVER_URI = DISPATCHER_URI + '/rds/'; var PROMPT_PAGE_ENDPOINT = 'promptPage/report/'; var PROMPT_ANSWERS_ENDPOINT = 'promptAnswers/conversationID/'; //jQuery extension to parse xml properly $.fn.filterNode = function(name) { return this.find('*').filter(function() { return this.localName.toLowerCase() === name.toLowerCase(); }); }; var promptingService = Events.extend({ /** * @classdesc Prompting service class giving access to events related to prompting * @constructs * @public * @param {Object} * options - set of initial properties * glassContext */ init: function(options) { promptingService.inherited('init', this, arguments); $.extend(true, this, options); }, /** * Called by glass when service is registered */ initialize: function(glassContext) { this.glassContext = glassContext; }, /** This is the first call that should be made when trying to collect-parameter-values. * Starting with a storeId, it returns a unique id (to be reused later) and a URL. * Loading this URL in a window/iframe will start the prompt dance. * * If you are calling this yourself (and not through the PromptPickerView), then be aware * you must define this function on the window in order to know when the dance is over: * * FinishCollectPrompts: function(success) - success is 1 or 0 depending if there are prompt * values to retrieve with getPromptAnswers() * * @param reportId - the unique id of the report to prompt for * @returns { id: unique prompt id to use with getPromptAnswers, * url: where to go to start the dance * */ getPromptPageInfo: function(reportId) { var server_URL = 'v1/objects/' + reportId + '?fields=path,runInAdvancedViewer,base.runInAdvancedViewer'; return this.glassContext.getCoreSvc('.Ajax').ajax({ url: server_URL, type: 'GET', dataType: 'json' }).then(function(response) { var data = response.data; return (data.data && data.data[0]) || {}; }).catch(function(err) { var error = { message: this.retrieveErrorMessage({result: err, defaultMessage: t.translate('schedule_prompting_wrong_url')}) }; this.glassContext.appController.showErrorMessage(error.message, t.translate('error_label_share')); throw error; }.bind(this)); }, /** Retrieves the parameter values from the session and returns them to the caller, * in JSON format. This format can be passed to the scheduling service as is. * * @param promptId the unique prompt id returned by getPromptPageInfo() * @returns an array of parameter values. EG: [ * { * 'name': 'Parameter-retailer-B', * 'value': [{ * 'inclusive': true, * 'use': 'True', * 'display': 'Run report with data', * 'type': 'simpleParmValueItem' * }] * }] */ getPromptAnswers: function(promptId) { var server_URL = SERVER_URI + PROMPT_ANSWERS_ENDPOINT + promptId; return this.glassContext.getCoreSvc('.Ajax').ajax({ url: server_URL, type: 'GET', dataType: 'xml' }).then(function(response) { var data = response.data; return this._toAnswersJson(data); }.bind(this)).catch(function(err) { var error = { message: this.retrieveErrorMessage({result: err, defaultMessage: t.translate('schedule_prompting_wrong_url')}) }; this.glassContext.appController.showErrorMessage(error.message, t.translate('error_label_share')); throw error; }.bind(this)); }, _getXmlAsString: function(xmlDom) { return (typeof XMLSerializer !== 'undefined') ? (new window.XMLSerializer()).serializeToString(xmlDom) : xmlDom.xml; }, _toRelativePath: function(fullPath) { var index = fullPath.indexOf('?'); return DISPATCHER_URI + fullPath.substr(index); }, _toPagesJson: function(xmlData) { var response = {}; var $xml = $(xmlData); response.url = this._toRelativePath($xml.filterNode('url').text()); response.id = $xml.filterNode('promptID').text(); return response; }, _toAnswersJson: function(xmlData) { /* * * * Parameter-retailer-B * * * * true * [Sales].[Retailer type].[Retailer (by type)].[Retailer name]->[all].[7].[7351] * 4 Your Eyes * * * * * * * [{ * 'name': 'Parameter-retailer-B', * 'value': [{ * 'inclusive': true, * 'use': 'True', * 'display': 'Run report with data', * 'type': 'simpleParmValueItem' * }] * }] */ var response = []; var $xml = $(xmlData); var _self = this; $xml.filterNode('promptValues').each(function(index) { var answer = {}; answer.name = $(this).filterNode('name').text(); var values = []; $(this).filterNode('item').each(function(j) { var value = {}; var $firstChild = $(this).children().first(); var localName = $firstChild.prop('localName'); switch (localName) { case 'SimplePValue': { value.type = 'simpleParmValueItem'; value.inclusive = $firstChild.filterNode('inclusive').text().toUpperCase() == 'TRUE'; _self._setUse(value, $firstChild.filterNode('useValue')); _self._setDisplay(value, $firstChild.filterNode('displayValue')); break; } case 'RangePValue': { /* * true * * true * 2015-11-08T00:00:00.000 * Nov 8, 2015 12:00 AM * * * */ var inclusiveRangeNode = $firstChild.filterNode('inclusive').get(0); value.inclusive = $(inclusiveRangeNode).text().toUpperCase() == 'TRUE'; $firstChild.children().each(function(k) { if ($(this).prop('localName') == 'start') { value.start = {}; value.start.inclusive = $(this).filterNode('inclusive').text().toUpperCase() == 'TRUE'; _self._setUse(value.start, $(this).filterNode('useValue')); _self._setDisplay(value.start, $(this).filterNode('displayValue')); value.start.type = 'simpleParmValueItem'; } else if ($(this).prop('localName') == 'end') { value.end = {}; value.end.inclusive = $(this).filterNode('inclusive').text().toUpperCase() == 'TRUE'; _self._setUse(value.end, $(this).filterNode('useValue')); _self._setDisplay(value.end, $(this).filterNode('displayValue')); value.end.type = 'simpleParmValueItem'; } }); if (value.start && value.end) { value.type = 'boundRangeParmValueItem'; } else if (value.start) { value.type = 'unboundedEndRangeParmValueItem'; } else if (value.end) { value.type = 'unboundedStartRangeParmValueItem'; } else { value.type = 'rangeParmValueItem'; } break; } } values.push(value); }); answer.value = values; response.push(answer); }); return response; }, _getDisplayValuesForURL: function(parameterValues) { var p_url = ''; for (var i = 0; i < parameterValues.length; i++) { p_url += 'p_' + parameterValues[i].name + '='; p_url += parameterValues[i].value[0].display; //Put '&' in between if (i !== parameterValues.length - 1) { p_url += '&'; } } return p_url; }, _setDisplay: function(value, display) { if (display && display.length > 0) { value.display = (display.attr('nil') == 'true') ? null : display.text(); } }, _setUse: function(value, use) { var useValue = null; if (use && use.attr('nil') != 'true') { useValue = use.text(); } value.use = useValue; }, retrieveErrorMessage: function(input) { input = input || {}; var message = input.defaultMessage; try { var text = $.parseJSON(input.result.responseText); message = text.error || returnedResult.message; } catch (e) {} return message; } }); return promptingService; });