123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- "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;
- });
|