CustomVisualizationService.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: admin
  5. * Copyright IBM Corp. 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure
  7. * restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['bi/glass/core/Class', 'jquery'], function (Class, $) {
  10. 'use strict'; //NOSONAR: meant to be strict
  11. /**
  12. Handle all ajax requests for custom visualizations
  13. options.glassContext - glass context
  14. **/
  15. var CustomVisualizationService = Class.extend({
  16. // /bi/v1/visualizations?maskResponses=true&updateAction=UPDATE
  17. customVizUrl: 'v1/visualizations',
  18. init: function init(options) {
  19. CustomVisualizationService.inherited('init', this, arguments);
  20. this.logger = options.glassContext.appController.logger;
  21. this.ajaxService = options.glassContext.getCoreSvc('.Ajax');
  22. },
  23. /**
  24. Do ajax request via glass ajax service
  25. **/
  26. ajax: function ajax(options) {
  27. var dfd = $.Deferred();
  28. if (this.ajaxService) {
  29. this.logDebug(options);
  30. return this.ajaxService.ajax(options, dfd);
  31. }
  32. var err = 'There is no ajax service from glass context.';
  33. this.logError(err);
  34. return dfd.reject(err);
  35. },
  36. /**
  37. When options.isUpload === true upload an custom visualization, otherwise update
  38. **/
  39. updateOrUpload: function updateOrUpload(options) {
  40. var url;
  41. var type;
  42. if (options.isUpload) {
  43. url = this.getUrl(null); // for upload there are extra parameters - need to fail duplicates
  44. url = url + "?maskResponses=true&updateAction=FAIL";
  45. type = 'POST';
  46. } else {
  47. if (!options.id) {
  48. var err = 'Name is not specified for update.';
  49. this.logError(err);
  50. var dfd = $.Deferred();
  51. return dfd.reject(err);
  52. }
  53. url = this.getUrl(options.id); // for update there are extra parameters
  54. type = 'PUT';
  55. }
  56. var requestOptions = {
  57. 'headers': {
  58. 'Accept': 'application/json',
  59. 'Content-Type': 'application/zip'
  60. },
  61. 'url': url,
  62. 'type': type,
  63. 'data': options.data,
  64. 'Content-Length': options.byteLength,
  65. 'processData': false
  66. };
  67. return this.ajax(requestOptions).then(function (response) {
  68. if (response.data) {
  69. var data = JSON.parse(response.data);
  70. if (data.Status > 299) {
  71. throw response;
  72. }
  73. } // if an upload just return as normal
  74. return response;
  75. });
  76. },
  77. /**
  78. log an error message
  79. **/
  80. logError: function logError(msg) {
  81. if (this.logger) {
  82. this.logger.error(msg);
  83. }
  84. },
  85. /**
  86. log a debug message
  87. **/
  88. logDebug: function logDebug(msg) {
  89. if (this.logger) {
  90. this.logger.debug(msg);
  91. }
  92. },
  93. /**
  94. * Get url according to type and name
  95. *
  96. * @name {string} id id(schematic)/name(cm) of the item being updated
  97. */
  98. getUrl: function getUrl(id) {
  99. var url;
  100. if (!id) {
  101. url = this.customVizUrl;
  102. } else {
  103. url = this.customVizUrl + '/id/' + id;
  104. }
  105. if (!url) {
  106. this.logDebug('Empty url');
  107. }
  108. return url;
  109. }
  110. });
  111. return CustomVisualizationService;
  112. });