ConversionService.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. 'use strict';
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * Licensed Materials - Property of IBM
  6. * IBM Watson Analytics (C) Copyright IBM Corp. 2018
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. *
  9. */
  10. define(['require', 'underscore', './ConverterInterface'], function (require, _, ConverterInterface) {
  11. var ConversionService = function () {
  12. function ConversionService() {
  13. _classCallCheck(this, ConversionService);
  14. // Map of srvc providers
  15. // Example of registered provider:
  16. // {
  17. // src: 'DASHBOARD',
  18. // target: 'EXPLORE',
  19. // converter: Class
  20. // }
  21. this._providers = [];
  22. this._glassContext = null;
  23. this.errLog = [];
  24. }
  25. /**
  26. * @param {object} glassContext - The glassContext when the service is created
  27. */
  28. _createClass(ConversionService, [{
  29. key: 'initialize',
  30. value: function initialize(glassContext) {
  31. var _this = this;
  32. this._glassContext = glassContext;
  33. return this._getGlassContextSrvcCollection().then(function (serviceProviders) {
  34. return Promise.all(serviceProviders.map(function (provider) {
  35. return _this._createServiceInstance(provider.class).then(function (providerClass) {
  36. return _this.registerProvider(providerClass);
  37. }).catch(function (err) {
  38. return _this.errLog.push(err);
  39. });
  40. } // we swallow errors from invalid providers for now
  41. ));
  42. });
  43. }
  44. /**
  45. * @return {Class} ConverterInterface to extend from for new converters
  46. */
  47. }, {
  48. key: 'getInterface',
  49. value: function getInterface() {
  50. return ConverterInterface;
  51. }
  52. /**
  53. * @param {Class} handler - conversion handler class. Needs a src, target, and functions convert and validate
  54. */
  55. }, {
  56. key: 'registerProvider',
  57. value: function registerProvider(handler) {
  58. var _this2 = this;
  59. return new Promise(function (resolve, reject) {
  60. var instance = new handler({ glassContext: _this2._glassContext });
  61. if (!_this2._isValidProvider(instance)) return reject({ msg: 'Invalid provider', moduleClass: handler });
  62. _this2._providers.push({
  63. src: instance.src,
  64. target: instance.target,
  65. converter: instance
  66. });
  67. return resolve();
  68. });
  69. }
  70. /**
  71. * @return {Promise} resolve with a list of errors we have from invalid providers
  72. */
  73. }, {
  74. key: 'getErrors',
  75. value: function getErrors() {
  76. return Promise.resolve(this.errLog);
  77. }
  78. /**
  79. * @param {String} src - source spec to convert from
  80. * @param {String} target - target spec to convert to
  81. * @return {Promise} will resolve with the registered conversion provider or null/undefined
  82. */
  83. }, {
  84. key: 'getProvider',
  85. value: function getProvider(src, target) {
  86. return this._getServiceProvider(src, target);
  87. }
  88. /**
  89. * @param {String} src - source spec to convert from
  90. * @param {String} target - target spec to convert to
  91. * @param {String} spec - spec to convert
  92. * @return {Promise} will find correct provider, convert, and resolve with converted spec
  93. */
  94. }, {
  95. key: 'convert',
  96. value: function convert(src, target, spec) {
  97. return this.getProvider(src, target).then(function (provider) {
  98. return provider.converter.convert(src, target, spec);
  99. });
  100. }
  101. /**
  102. * @private
  103. */
  104. }, {
  105. key: '_isValidProvider',
  106. value: function _isValidProvider(handler) {
  107. return handler && handler.convert && _.isFunction(handler.convert) && handler.validate && _.isFunction(handler.validate) && !_.isUndefined(handler.src) && !_.isNull(handler.src) && !_.isUndefined(handler.target) && !_.isNull(handler.target);
  108. }
  109. /**
  110. * @private
  111. */
  112. }, {
  113. key: '_getServiceProvider',
  114. value: function _getServiceProvider(src, target) {
  115. var _this3 = this;
  116. var provider = this._getProviderLocal(src, target);
  117. if (provider) {
  118. return Promise.resolve(provider);
  119. }
  120. return this._getConversionServiceProvider(src, target).then(function (provider) {
  121. if (!provider) {
  122. return Promise.reject({ errorKey: 'conversionProviderNotFound' });
  123. }
  124. return _this3._getNewServiceInstance(provider.class, src, target);
  125. });
  126. }
  127. /**
  128. * @private
  129. */
  130. }, {
  131. key: '_getProviderLocal',
  132. value: function _getProviderLocal(src, target) {
  133. return this._find(src, target, this._providers);
  134. }
  135. /**
  136. * @private
  137. */
  138. }, {
  139. key: '_getConversionServiceProvider',
  140. value: function _getConversionServiceProvider(src, target) {
  141. var _this4 = this;
  142. return this._getGlassContextSrvcCollection().then(function (serviceProviders) {
  143. return _this4._find(src, target, serviceProviders);
  144. });
  145. }
  146. /**
  147. * @private
  148. */
  149. }, {
  150. key: '_find',
  151. value: function _find(src, target, providers) {
  152. return providers.find(function (provider) {
  153. return provider.src == src && provider.target == target;
  154. });
  155. }
  156. /**
  157. * @private
  158. * @return Promise resolves with glass collection
  159. */
  160. }, {
  161. key: '_getGlassContextSrvcCollection',
  162. value: function _getGlassContextSrvcCollection() {
  163. return this._glassContext.appController.findCollection('com.ibm.bi.ConversionService.provider');
  164. }
  165. /**
  166. * @private
  167. */
  168. }, {
  169. key: '_createServiceInstance',
  170. value: function _createServiceInstance(moduleId) {
  171. return new Promise(function (resolve, reject) {
  172. if (typeof moduleId == 'function') return resolve(moduleId);
  173. try {
  174. require([moduleId], function (moduleClass) {
  175. return resolve(moduleClass);
  176. }, function (error) {
  177. return reject(error);
  178. });
  179. } catch (error) {
  180. return reject({ msg: 'Require failed', err: error, classPath: moduleId });
  181. }
  182. });
  183. }
  184. /**
  185. * @private
  186. * @param {string} moduleId moduleClassPath
  187. * @param {string} src converter of src to get
  188. * @param {string} target converter with this this.target
  189. * @return {Promise} moduleInstance an instance of the converter
  190. */
  191. }, {
  192. key: '_getNewServiceInstance',
  193. value: function _getNewServiceInstance(moduleId, src, target) {
  194. var _this5 = this;
  195. return this._createServiceInstance(moduleId).then(function (moduleClass) {
  196. return _this5.registerProvider(moduleClass);
  197. }).then(function () {
  198. return _this5.getProvider(src, target);
  199. }).catch(function (err) {
  200. return _this5.errLog.push(err);
  201. });
  202. }
  203. }]);
  204. return ConversionService;
  205. }();
  206. return ConversionService;
  207. });
  208. //# sourceMappingURL=ConversionService.js.map