123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- /*
- * 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) {
- /*
- * <promptAnswers>
- * <promptValues>
- * <name>Parameter-retailer-B</name>
- * <values>
- * <item>
- * <SimplePValue>
- * <inclusive>true</inclusive>
- * <useValue>[Sales].[Retailer type].[Retailer (by type)].[Retailer name]->[all].[7].[7351]</useValue>
- * <displayValue>4 Your Eyes</displayValue>
- * </SimplePValue>
- * </item>
- * </values>
- * </promptValues>
- * </promptAnswers>
- *
- * [{
- * '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':
- {
- /* <rds:RangePValue>
- * <rds:inclusive>true</rds:inclusive>
- * <rds:start>
- * <rds:inclusive>true</rds:inclusive>
- * <rds:useValue>2015-11-08T00:00:00.000</rds:useValue>
- * <rds:displayValue>Nov 8, 2015 12:00 AM</rds:displayValue>
- * </rds:start>
- * </rds:RangePValue></rds:item></rds:values></rds:promptValues>
- * */
- 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;
- });
|