ExtensionService.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Content Explorer
  6. *| (C) Copyright IBM Corp. 2015, 2016
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['bi/glass/core/Class', 'jquery'], function (Class, $) {
  13. 'use strict'; //NOSONAR: meant to be strict
  14. /**
  15. Handle all ajax requests for extension and theme
  16. options.glassContext - glass context
  17. **/
  18. var ExtensionService = Class.extend({
  19. themesUrl: 'v1/plugins/themes',
  20. extensionsUrl: 'v1/plugins/extensions',
  21. init: function init(options) {
  22. ExtensionService.inherited('init', this, arguments);
  23. this.logger = options.glassContext.appController.logger;
  24. this.ajaxService = options.glassContext.getCoreSvc('.Ajax');
  25. },
  26. /**
  27. Do ajax request via glass ajax service
  28. **/
  29. ajax: function ajax(options) {
  30. var dfd = $.Deferred();
  31. if (this.ajaxService) {
  32. this.logDebug(options);
  33. return this.ajaxService.ajax(options, dfd);
  34. }
  35. var err = 'There is no ajax service from glass context.';
  36. this.logError(err);
  37. return dfd.reject(err);
  38. },
  39. /**
  40. Delete a theme or extension by name
  41. type is either theme or extension
  42. **/
  43. delete: function _delete(type, name, tenantID) {
  44. var url;
  45. if (type === 'theme' || type === 'extension') {
  46. if (name) {
  47. url = this.getUrl(type, name, tenantID, 'delete');
  48. } else {
  49. this.logError('Missing name for deletion');
  50. }
  51. } else {
  52. this.logError('Incorrect extension type: ' + type);
  53. }
  54. var options = {
  55. 'dataType': 'json',
  56. 'type': 'DELETE',
  57. 'url': url
  58. };
  59. return this.ajax(options);
  60. },
  61. /**
  62. When options.isUpload === true upload an extension or theme, otherwise update
  63. **/
  64. updateOrUpload: function updateOrUpload(options) {
  65. var url;
  66. var type;
  67. if (options.isUpload) {
  68. url = this.getUrl(options.type, null);
  69. type = 'POST';
  70. } else {
  71. if (!options.name) {
  72. var err = 'Name is not specified for update.';
  73. this.logError(err);
  74. var dfd = $.Deferred();
  75. return dfd.reject(err);
  76. }
  77. url = this.getUrl(options.type, options.name, null, 'update');
  78. type = 'PUT';
  79. }
  80. var requestOptions = {
  81. 'headers': {
  82. 'Accept': 'application/json',
  83. 'Content-Type': 'application/zip'
  84. },
  85. 'url': url,
  86. 'type': type,
  87. 'data': options.data,
  88. 'Content-Length': options.byteLength,
  89. 'processData': false
  90. };
  91. return this.ajax(requestOptions);
  92. },
  93. /**
  94. log an error message
  95. **/
  96. logError: function logError(msg) {
  97. if (this.logger) {
  98. this.logger.error(msg);
  99. }
  100. },
  101. /**
  102. log a debug message
  103. **/
  104. logDebug: function logDebug(msg) {
  105. if (this.logger) {
  106. this.logger.debug(msg);
  107. }
  108. },
  109. /**
  110. *
  111. * @param {url that represents the themes service or the extension service} serviceUrl
  112. * @param {name represents the name of the theme object or the extension object, name can be empty when uploading
  113. * since the file needs to be processed before it gets a name} name
  114. * @param {id of the users tenant} tenantID
  115. * @param {action in this case is update or delete since the url has a different format based on the action} action
  116. */
  117. buildUrl: function buildUrl(serviceUrl, name, tenantID, action) {
  118. var url;
  119. tenantID = tenantID ? tenantID : 'global';
  120. if (!name) {
  121. url = serviceUrl;
  122. } else {
  123. url = action !== 'update' ? serviceUrl + '/' + tenantID + '/' + name : serviceUrl + '/' + name;
  124. }
  125. return url;
  126. },
  127. /**
  128. * Get url according to type and name
  129. *
  130. * @type {string} - theme or extension
  131. * @name {string} - name
  132. */
  133. getUrl: function getUrl(type, name, tenantID, action) {
  134. var url;
  135. if (type === 'theme') {
  136. url = this.buildUrl(this.themesUrl, name, tenantID, action);
  137. } else if (type === 'extension') {
  138. url = this.buildUrl(this.extensionsUrl, name, tenantID, action);
  139. } else {
  140. this.logError('Incorrect extension type: ' + type);
  141. }
  142. if (!url) {
  143. this.logDebug('Empty url');
  144. }
  145. return url;
  146. }
  147. });
  148. return ExtensionService;
  149. });