"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: admin * Copyright IBM Corp. 2019 * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['bi/glass/core/Class', 'jquery'], function (Class, $) { 'use strict'; //NOSONAR: meant to be strict /** Handle all ajax requests for custom visualizations options.glassContext - glass context **/ var CustomVisualizationService = Class.extend({ // /bi/v1/visualizations?maskResponses=true&updateAction=UPDATE customVizUrl: 'v1/visualizations', init: function init(options) { CustomVisualizationService.inherited('init', this, arguments); this.logger = options.glassContext.appController.logger; this.ajaxService = options.glassContext.getCoreSvc('.Ajax'); }, /** Do ajax request via glass ajax service **/ ajax: function ajax(options) { var dfd = $.Deferred(); if (this.ajaxService) { this.logDebug(options); return this.ajaxService.ajax(options, dfd); } var err = 'There is no ajax service from glass context.'; this.logError(err); return dfd.reject(err); }, /** When options.isUpload === true upload an custom visualization, otherwise update **/ updateOrUpload: function updateOrUpload(options) { var url; var type; if (options.isUpload) { url = this.getUrl(null); // for upload there are extra parameters - need to fail duplicates url = url + "?maskResponses=true&updateAction=FAIL"; type = 'POST'; } else { if (!options.id) { var err = 'Name is not specified for update.'; this.logError(err); var dfd = $.Deferred(); return dfd.reject(err); } url = this.getUrl(options.id); // for update there are extra parameters type = 'PUT'; } var requestOptions = { 'headers': { 'Accept': 'application/json', 'Content-Type': 'application/zip' }, 'url': url, 'type': type, 'data': options.data, 'Content-Length': options.byteLength, 'processData': false }; return this.ajax(requestOptions).then(function (response) { if (response.data) { var data = JSON.parse(response.data); if (data.Status > 299) { throw response; } } // if an upload just return as normal return response; }); }, /** log an error message **/ logError: function logError(msg) { if (this.logger) { this.logger.error(msg); } }, /** log a debug message **/ logDebug: function logDebug(msg) { if (this.logger) { this.logger.debug(msg); } }, /** * Get url according to type and name * * @name {string} id id(schematic)/name(cm) of the item being updated */ getUrl: function getUrl(id) { var url; if (!id) { url = this.customVizUrl; } else { url = this.customVizUrl + '/id/' + id; } if (!url) { this.logDebug('Empty url'); } return url; } }); return CustomVisualizationService; });