123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- 'use strict';
- 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; }; }();
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- /**
- * Licensed Materials - Property of IBM
- * IBM Watson Analytics (C) Copyright IBM Corp. 2018
- * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- *
- */
- define(['require', 'underscore', './ConverterInterface'], function (require, _, ConverterInterface) {
- var ConversionService = function () {
- function ConversionService() {
- _classCallCheck(this, ConversionService);
- // Map of srvc providers
- // Example of registered provider:
- // {
- // src: 'DASHBOARD',
- // target: 'EXPLORE',
- // converter: Class
- // }
- this._providers = [];
- this._glassContext = null;
- this.errLog = [];
- }
- /**
- * @param {object} glassContext - The glassContext when the service is created
- */
- _createClass(ConversionService, [{
- key: 'initialize',
- value: function initialize(glassContext) {
- var _this = this;
- this._glassContext = glassContext;
- return this._getGlassContextSrvcCollection().then(function (serviceProviders) {
- return Promise.all(serviceProviders.map(function (provider) {
- return _this._createServiceInstance(provider.class).then(function (providerClass) {
- return _this.registerProvider(providerClass);
- }).catch(function (err) {
- return _this.errLog.push(err);
- });
- } // we swallow errors from invalid providers for now
- ));
- });
- }
- /**
- * @return {Class} ConverterInterface to extend from for new converters
- */
- }, {
- key: 'getInterface',
- value: function getInterface() {
- return ConverterInterface;
- }
- /**
- * @param {Class} handler - conversion handler class. Needs a src, target, and functions convert and validate
- */
- }, {
- key: 'registerProvider',
- value: function registerProvider(handler) {
- var _this2 = this;
- return new Promise(function (resolve, reject) {
- var instance = new handler({ glassContext: _this2._glassContext });
- if (!_this2._isValidProvider(instance)) return reject({ msg: 'Invalid provider', moduleClass: handler });
- _this2._providers.push({
- src: instance.src,
- target: instance.target,
- converter: instance
- });
- return resolve();
- });
- }
- /**
- * @return {Promise} resolve with a list of errors we have from invalid providers
- */
- }, {
- key: 'getErrors',
- value: function getErrors() {
- return Promise.resolve(this.errLog);
- }
- /**
- * @param {String} src - source spec to convert from
- * @param {String} target - target spec to convert to
- * @return {Promise} will resolve with the registered conversion provider or null/undefined
- */
- }, {
- key: 'getProvider',
- value: function getProvider(src, target) {
- return this._getServiceProvider(src, target);
- }
- /**
- * @param {String} src - source spec to convert from
- * @param {String} target - target spec to convert to
- * @param {String} spec - spec to convert
- * @return {Promise} will find correct provider, convert, and resolve with converted spec
- */
- }, {
- key: 'convert',
- value: function convert(src, target, spec) {
- return this.getProvider(src, target).then(function (provider) {
- return provider.converter.convert(src, target, spec);
- });
- }
- /**
- * @private
- */
- }, {
- key: '_isValidProvider',
- value: function _isValidProvider(handler) {
- return handler && handler.convert && _.isFunction(handler.convert) && handler.validate && _.isFunction(handler.validate) && !_.isUndefined(handler.src) && !_.isNull(handler.src) && !_.isUndefined(handler.target) && !_.isNull(handler.target);
- }
- /**
- * @private
- */
- }, {
- key: '_getServiceProvider',
- value: function _getServiceProvider(src, target) {
- var _this3 = this;
- var provider = this._getProviderLocal(src, target);
- if (provider) {
- return Promise.resolve(provider);
- }
- return this._getConversionServiceProvider(src, target).then(function (provider) {
- if (!provider) {
- return Promise.reject({ errorKey: 'conversionProviderNotFound' });
- }
- return _this3._getNewServiceInstance(provider.class, src, target);
- });
- }
- /**
- * @private
- */
- }, {
- key: '_getProviderLocal',
- value: function _getProviderLocal(src, target) {
- return this._find(src, target, this._providers);
- }
- /**
- * @private
- */
- }, {
- key: '_getConversionServiceProvider',
- value: function _getConversionServiceProvider(src, target) {
- var _this4 = this;
- return this._getGlassContextSrvcCollection().then(function (serviceProviders) {
- return _this4._find(src, target, serviceProviders);
- });
- }
- /**
- * @private
- */
- }, {
- key: '_find',
- value: function _find(src, target, providers) {
- return providers.find(function (provider) {
- return provider.src == src && provider.target == target;
- });
- }
- /**
- * @private
- * @return Promise resolves with glass collection
- */
- }, {
- key: '_getGlassContextSrvcCollection',
- value: function _getGlassContextSrvcCollection() {
- return this._glassContext.appController.findCollection('com.ibm.bi.ConversionService.provider');
- }
- /**
- * @private
- */
- }, {
- key: '_createServiceInstance',
- value: function _createServiceInstance(moduleId) {
- return new Promise(function (resolve, reject) {
- if (typeof moduleId == 'function') return resolve(moduleId);
- try {
- require([moduleId], function (moduleClass) {
- return resolve(moduleClass);
- }, function (error) {
- return reject(error);
- });
- } catch (error) {
- return reject({ msg: 'Require failed', err: error, classPath: moduleId });
- }
- });
- }
- /**
- * @private
- * @param {string} moduleId moduleClassPath
- * @param {string} src converter of src to get
- * @param {string} target converter with this this.target
- * @return {Promise} moduleInstance an instance of the converter
- */
- }, {
- key: '_getNewServiceInstance',
- value: function _getNewServiceInstance(moduleId, src, target) {
- var _this5 = this;
- return this._createServiceInstance(moduleId).then(function (moduleClass) {
- return _this5.registerProvider(moduleClass);
- }).then(function () {
- return _this5.getProvider(src, target);
- }).catch(function (err) {
- return _this5.errLog.push(err);
- });
- }
- }]);
- return ConversionService;
- }();
- return ConversionService;
- });
- //# sourceMappingURL=ConversionService.js.map
|