"use strict"; /* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Content Explorer *| (C) Copyright IBM Corp. 2015, 2016 *| *| 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 extension and theme options.glassContext - glass context **/ var ExtensionService = Class.extend({ themesUrl: 'v1/plugins/themes', extensionsUrl: 'v1/plugins/extensions', init: function init(options) { ExtensionService.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); }, /** Delete a theme or extension by name type is either theme or extension **/ delete: function _delete(type, name, tenantID) { var url; if (type === 'theme' || type === 'extension') { if (name) { url = this.getUrl(type, name, tenantID, 'delete'); } else { this.logError('Missing name for deletion'); } } else { this.logError('Incorrect extension type: ' + type); } var options = { 'dataType': 'json', 'type': 'DELETE', 'url': url }; return this.ajax(options); }, /** When options.isUpload === true upload an extension or theme, otherwise update **/ updateOrUpload: function updateOrUpload(options) { var url; var type; if (options.isUpload) { url = this.getUrl(options.type, null); type = 'POST'; } else { if (!options.name) { var err = 'Name is not specified for update.'; this.logError(err); var dfd = $.Deferred(); return dfd.reject(err); } url = this.getUrl(options.type, options.name, null, 'update'); 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); }, /** 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); } }, /** * * @param {url that represents the themes service or the extension service} serviceUrl * @param {name represents the name of the theme object or the extension object, name can be empty when uploading * since the file needs to be processed before it gets a name} name * @param {id of the users tenant} tenantID * @param {action in this case is update or delete since the url has a different format based on the action} action */ buildUrl: function buildUrl(serviceUrl, name, tenantID, action) { var url; tenantID = tenantID ? tenantID : 'global'; if (!name) { url = serviceUrl; } else { url = action !== 'update' ? serviceUrl + '/' + tenantID + '/' + name : serviceUrl + '/' + name; } return url; }, /** * Get url according to type and name * * @type {string} - theme or extension * @name {string} - name */ getUrl: function getUrl(type, name, tenantID, action) { var url; if (type === 'theme') { url = this.buildUrl(this.themesUrl, name, tenantID, action); } else if (type === 'extension') { url = this.buildUrl(this.extensionsUrl, name, tenantID, action); } else { this.logError('Incorrect extension type: ' + type); } if (!url) { this.logDebug('Empty url'); } return url; } }); return ExtensionService; });