RelinkItemIdInSpec.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. 'use strict';
  2. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  3. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. /**
  6. * Licensed Materials - Property of IBM
  7. * IBM Watson Analytics (C) Copyright IBM Corp. 2018, 2020
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. define(['underscore', './DashboardSpecUtil'], function (_, DashboardSpecUtil) {
  11. var WIDGET_TYPE = {
  12. LIVEWIDGET: 'live',
  13. FILTER: 'filter'
  14. };
  15. var escapeRegExp = function escapeRegExp(string) {
  16. return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
  17. };
  18. var replaceItemIdInString = function replaceItemIdInString() {
  19. var string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  20. var oldItemId = arguments[1];
  21. var newItemId = arguments[2];
  22. var regExpr = new RegExp(escapeRegExp(oldItemId), 'g');
  23. return string.replace(regExpr, newItemId);
  24. };
  25. var replaceIdsInArray = function replaceIdsInArray(jsonSpec, collection, index, oldItemId, newItemId) {
  26. var string = JSON.stringify(jsonSpec);
  27. string = replaceItemIdInString(string, oldItemId, newItemId);
  28. collection[index] = JSON.parse(string);
  29. };
  30. var BaseRelinkItemId = function () {
  31. function BaseRelinkItemId(jsonSpec, sourceId) {
  32. _classCallCheck(this, BaseRelinkItemId);
  33. if (!jsonSpec) {
  34. throw new Error('Invalid JSON specification');
  35. }
  36. this.sourceId = sourceId;
  37. this.jsonSpec = jsonSpec;
  38. }
  39. BaseRelinkItemId.prototype.getItemIds = function getItemIds() {
  40. return [];
  41. };
  42. BaseRelinkItemId.prototype.replaceItemIds = function replaceItemIds() {};
  43. BaseRelinkItemId.prototype.postReplaceItemIds = function postReplaceItemIds() {};
  44. BaseRelinkItemId.prototype.validSourceId = function validSourceId(idToCheck) {
  45. return this.sourceId === idToCheck;
  46. };
  47. return BaseRelinkItemId;
  48. }();
  49. var RelinkPageContext = function (_BaseRelinkItemId) {
  50. _inherits(RelinkPageContext, _BaseRelinkItemId);
  51. function RelinkPageContext() {
  52. _classCallCheck(this, RelinkPageContext);
  53. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  54. args[_key] = arguments[_key];
  55. }
  56. return _possibleConstructorReturn(this, _BaseRelinkItemId.call.apply(_BaseRelinkItemId, [this].concat(args)));
  57. }
  58. RelinkPageContext.prototype.getItemIds = function getItemIds() {
  59. var resultSet = [];
  60. for (var _iterator = this.jsonSpec, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  61. var _ref;
  62. if (_isArray) {
  63. if (_i >= _iterator.length) break;
  64. _ref = _iterator[_i++];
  65. } else {
  66. _i = _iterator.next();
  67. if (_i.done) break;
  68. _ref = _i.value;
  69. }
  70. var entry = _ref;
  71. if (this.validSourceId(entry.sourceId) && entry.hierarchyUniqueNames) {
  72. resultSet = resultSet.concat(entry.hierarchyUniqueNames);
  73. }
  74. }
  75. return resultSet;
  76. };
  77. RelinkPageContext.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) {
  78. var _this2 = this;
  79. this.jsonSpec.forEach(function (entry, index, collection) {
  80. if (_this2.validSourceId(entry.sourceId)) {
  81. var spec = JSON.stringify(entry);
  82. spec = replaceItemIdInString(spec, oldItemId, newItemId);
  83. collection[index] = JSON.parse(spec);
  84. }
  85. });
  86. };
  87. return RelinkPageContext;
  88. }(BaseRelinkItemId);
  89. /**
  90. * update mappings
  91. * @example
  92. * [{
  93. "name": "Product_line",
  94. "modelFilterItem": "[Sales (query)].[Products].[Product line]",
  95. "mapTo": "QC_Data_xlsx.Product_line"
  96. },
  97. {
  98. "name": "Order_Method",
  99. "modelFilterItem": "[Sales (query)].[Order method].[Order method type]",
  100. "mapTo": "QC_Data_xlsx.Order_method"
  101. }]
  102. Updated "mapTo" part only and we should never change the modelFilterItem part since it comes from report.
  103. After update the mappings:
  104. [ {
  105. "name": "Product_line",
  106. "modelFilterItem": "[Sales (query)].[Products].[Product line]",
  107. "mapTo": "dataSetDemo_2_csv.Product_line"
  108. },
  109. {
  110. "name": "Order_Method",
  111. "modelFilterItem": "[Sales (query)].[Order method].[Order method type]",
  112. "mapTo": "dataSetDemo_2_csv.Order_method"
  113. }]
  114. */
  115. var RelinkDrillThrough = function (_BaseRelinkItemId2) {
  116. _inherits(RelinkDrillThrough, _BaseRelinkItemId2);
  117. function RelinkDrillThrough() {
  118. _classCallCheck(this, RelinkDrillThrough);
  119. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  120. args[_key2] = arguments[_key2];
  121. }
  122. return _possibleConstructorReturn(this, _BaseRelinkItemId2.call.apply(_BaseRelinkItemId2, [this].concat(args)));
  123. }
  124. RelinkDrillThrough.prototype.getItemIds = function getItemIds() {
  125. var _this4 = this;
  126. var resultSet = [];
  127. this.jsonSpec.forEach(function (entry) {
  128. var sourceId = entry.modelRefs[0];
  129. if (_this4.validSourceId(sourceId)) {
  130. var mappings = entry.mappings;
  131. for (var _iterator2 = mappings, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  132. var _ref2;
  133. if (_isArray2) {
  134. if (_i2 >= _iterator2.length) break;
  135. _ref2 = _iterator2[_i2++];
  136. } else {
  137. _i2 = _iterator2.next();
  138. if (_i2.done) break;
  139. _ref2 = _i2.value;
  140. }
  141. var mapping = _ref2;
  142. if (mapping.mapTo && mapping.mapTo !== 'none') {
  143. resultSet.push(mapping.mapTo);
  144. }
  145. }
  146. }
  147. });
  148. return resultSet;
  149. };
  150. RelinkDrillThrough.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) {
  151. var _this5 = this;
  152. this.jsonSpec.forEach(function (entry) {
  153. if (_this5.validSourceId(entry.modelRefs[0])) {
  154. var mappings = entry.mappings || [];
  155. for (var _iterator3 = mappings, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
  156. var _ref3;
  157. if (_isArray3) {
  158. if (_i3 >= _iterator3.length) break;
  159. _ref3 = _iterator3[_i3++];
  160. } else {
  161. _i3 = _iterator3.next();
  162. if (_i3.done) break;
  163. _ref3 = _i3.value;
  164. }
  165. var mapping = _ref3;
  166. if (mapping.mapTo === oldItemId) {
  167. mapping.mapTo = newItemId;
  168. }
  169. }
  170. }
  171. });
  172. };
  173. return RelinkDrillThrough;
  174. }(BaseRelinkItemId);
  175. var RelinkLayout = function (_BaseRelinkItemId3) {
  176. _inherits(RelinkLayout, _BaseRelinkItemId3);
  177. function RelinkLayout() {
  178. _classCallCheck(this, RelinkLayout);
  179. for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  180. args[_key3] = arguments[_key3];
  181. }
  182. var _this6 = _possibleConstructorReturn(this, _BaseRelinkItemId3.call.apply(_BaseRelinkItemId3, [this].concat(args)));
  183. _this6._widgets = _this6._getWidgetModelsObject(_this6.jsonSpec);
  184. _this6.relinkWidget = new RelinkWidget(_this6._widgets, _this6.sourceId);
  185. return _this6;
  186. }
  187. RelinkLayout.prototype.getItemIds = function getItemIds() {
  188. return this.relinkWidget.getItemIds();
  189. };
  190. RelinkLayout.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) {
  191. return this.relinkWidget.replaceItemIds(oldItemId, newItemId);
  192. };
  193. RelinkLayout.prototype._getWidgetModelsObject = function _getWidgetModelsObject(layout) {
  194. var o = {};
  195. //Take list of widget models and convert it to an object keyed by the model ids
  196. DashboardSpecUtil.getWidgetModelList(layout).forEach(function (widgetModel) {
  197. return o[widgetModel.id] = widgetModel;
  198. });
  199. return o;
  200. };
  201. return RelinkLayout;
  202. }(BaseRelinkItemId);
  203. var RelinkWidget = function (_BaseRelinkItemId4) {
  204. _inherits(RelinkWidget, _BaseRelinkItemId4);
  205. function RelinkWidget() {
  206. _classCallCheck(this, RelinkWidget);
  207. for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
  208. args[_key4] = arguments[_key4];
  209. }
  210. return _possibleConstructorReturn(this, _BaseRelinkItemId4.call.apply(_BaseRelinkItemId4, [this].concat(args)));
  211. }
  212. RelinkWidget.prototype.getItemIds = function getItemIds() {
  213. var resultSet = [];
  214. for (var prop in this.jsonSpec) {
  215. var widget = this.jsonSpec[prop];
  216. var itemIds = (widget.type === WIDGET_TYPE.LIVEWIDGET ? this._getLiveWidgetItemIds(widget) : this._getFilterWidgetItemIds(widget)) || [];
  217. resultSet = resultSet.concat(itemIds);
  218. }
  219. return resultSet;
  220. };
  221. RelinkWidget.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) {
  222. for (var prop in this.jsonSpec) {
  223. var updatedWidget = void 0;
  224. var widget = this.jsonSpec[prop];
  225. if (widget.type === WIDGET_TYPE.LIVEWIDGET) {
  226. updatedWidget = this._updateLiveWidgetItemIds(widget, oldItemId, newItemId);
  227. } else {
  228. updatedWidget = this._updateFilterWidgetItemIds(widget, oldItemId, newItemId);
  229. }
  230. this.jsonSpec[prop] = updatedWidget;
  231. }
  232. };
  233. RelinkWidget.prototype._getLiveWidgetItemIds = function _getLiveWidgetItemIds(widget) {
  234. var resultSet = [];
  235. var dataViews = widget.data && widget.data.dataViews ? widget.data && widget.data.dataViews : [];
  236. var validSourceId = false;
  237. for (var _iterator4 = dataViews, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
  238. var _ref4;
  239. if (_isArray4) {
  240. if (_i4 >= _iterator4.length) break;
  241. _ref4 = _iterator4[_i4++];
  242. } else {
  243. _i4 = _iterator4.next();
  244. if (_i4.done) break;
  245. _ref4 = _i4.value;
  246. }
  247. var dataView = _ref4;
  248. validSourceId = this.validSourceId(dataView.modelRef);
  249. if (validSourceId) {
  250. var dataItems = dataView.dataItems || [];
  251. for (var _iterator5 = dataItems, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
  252. var _ref5;
  253. if (_isArray5) {
  254. if (_i5 >= _iterator5.length) break;
  255. _ref5 = _iterator5[_i5++];
  256. } else {
  257. _i5 = _iterator5.next();
  258. if (_i5.done) break;
  259. _ref5 = _i5.value;
  260. }
  261. var dataItem = _ref5;
  262. resultSet.push(dataItem.itemId);
  263. }
  264. }
  265. }
  266. if (validSourceId) {
  267. resultSet = resultSet.concat(this._getLocalFilter(widget.localFilters || []));
  268. }
  269. return resultSet;
  270. };
  271. RelinkWidget.prototype._getLocalFilter = function _getLocalFilter(localFilters) {
  272. return _.pluck(localFilters, 'columnId');
  273. };
  274. RelinkWidget.prototype._getFilterWidgetItemIds = function _getFilterWidgetItemIds(widget) {
  275. var resultSet = [];
  276. var properties = widget.properties || {};
  277. if (this.validSourceId(properties.sourceId)) {
  278. resultSet.push(properties.itemId);
  279. }
  280. return resultSet;
  281. };
  282. RelinkWidget.prototype._updateLiveWidgetItemIds = function _updateLiveWidgetItemIds(widget, oldItemId, newItemId) {
  283. var _this8 = this;
  284. var validDataSource = false;
  285. var dataViews = widget.data && widget.data.dataViews ? widget.data.dataViews : [];
  286. dataViews.forEach(function (dataView, index, collection) {
  287. validDataSource = _this8.validSourceId(dataView.modelRef);
  288. if (validDataSource) {
  289. replaceIdsInArray(dataView, collection, index, oldItemId, newItemId);
  290. }
  291. });
  292. if (validDataSource) {
  293. var localFilters = widget.localFilters || [];
  294. localFilters.forEach(function (filter, index, collection) {
  295. replaceIdsInArray(filter, collection, index, oldItemId, newItemId);
  296. });
  297. }
  298. return widget;
  299. };
  300. RelinkWidget.prototype._updateFilterWidgetItemIds = function _updateFilterWidgetItemIds(widget, oldItemId, newItemId) {
  301. var properties = widget.properties || {};
  302. if (this.validSourceId(properties.sourceId)) {
  303. if (properties.itemId === oldItemId) {
  304. properties.itemId = newItemId;
  305. }
  306. }
  307. return widget;
  308. };
  309. return RelinkWidget;
  310. }(BaseRelinkItemId);
  311. /**
  312. * datasetShaping is obsolete in R7.
  313. * During upgrade from R6 to R7, gemini/dashboard/loader/upgrader/waca_shaping.js is responsbile for upgrading this model to an embbedded shaping module.
  314. * When upgrade from R6 fail, this datasetShaping model still exist in the dashboard spec and therefore relink must handle this case.
  315. * After relink, the dashboard spec goes through the upgrade process one more time. If succeed the datasetShaping is replaced by the embedded module in dashboard spec
  316. */
  317. var RelinkDatasetShaping = function (_BaseRelinkItemId5) {
  318. _inherits(RelinkDatasetShaping, _BaseRelinkItemId5);
  319. function RelinkDatasetShaping() {
  320. _classCallCheck(this, RelinkDatasetShaping);
  321. for (var _len5 = arguments.length, args = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
  322. args[_key5] = arguments[_key5];
  323. }
  324. var _this9 = _possibleConstructorReturn(this, _BaseRelinkItemId5.call.apply(_BaseRelinkItemId5, [this].concat(args)));
  325. _this9.oldCMAssetId = args[2];
  326. _this9.newCMAssetId = args[3];
  327. return _this9;
  328. }
  329. RelinkDatasetShaping.prototype.getItemIds = function getItemIds() {
  330. var resultSet = [];
  331. for (var prop in this.jsonSpec) {
  332. var shaping = this.jsonSpec[prop];
  333. if (shaping.id === this.oldCMAssetId) {
  334. resultSet = resultSet.concat(this._getFilters(shaping.filters || []));
  335. resultSet = resultSet.concat(this._getCalculations(shaping.calculations || []));
  336. }
  337. }
  338. return resultSet;
  339. };
  340. RelinkDatasetShaping.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) {
  341. var _this10 = this;
  342. var jsonSpec = this.jsonSpec || [];
  343. jsonSpec.forEach(function (shaping, index, collection) {
  344. if (shaping.id === _this10.oldCMAssetId) {
  345. replaceIdsInArray(shaping, collection, index, oldItemId, newItemId);
  346. }
  347. });
  348. };
  349. RelinkDatasetShaping.prototype.postReplaceItemIds = function postReplaceItemIds() {
  350. var _this11 = this;
  351. var jsonSpec = this.jsonSpec || [];
  352. jsonSpec.forEach(function (shaping) {
  353. if (shaping.id === _this11.oldCMAssetId) {
  354. shaping.id = _this11.newCMAssetId;
  355. }
  356. });
  357. };
  358. RelinkDatasetShaping.prototype._getFilters = function _getFilters(filters) {
  359. return _.pluck(filters, 'columnId');
  360. };
  361. RelinkDatasetShaping.prototype._getCalculations = function _getCalculations(calculations) {
  362. var expr = _.pluck(calculations, 'expr') || [];
  363. var params = _.flatten(_.pluck(expr, 'params') || []);
  364. return _.pluck(params, 'col') || [];
  365. };
  366. return RelinkDatasetShaping;
  367. }(BaseRelinkItemId);
  368. var RelinkConditionalFormatting = function (_BaseRelinkItemId6) {
  369. _inherits(RelinkConditionalFormatting, _BaseRelinkItemId6);
  370. function RelinkConditionalFormatting() {
  371. _classCallCheck(this, RelinkConditionalFormatting);
  372. for (var _len6 = arguments.length, args = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
  373. args[_key6] = arguments[_key6];
  374. }
  375. return _possibleConstructorReturn(this, _BaseRelinkItemId6.call.apply(_BaseRelinkItemId6, [this].concat(args)));
  376. }
  377. RelinkConditionalFormatting.prototype._getConditionalFormattingItemIds = function _getConditionalFormattingItemIds(location) {
  378. var resultSet = [];
  379. var conditionalFormats = location.content && location.content.conditionalFormatting && location.content.conditionalFormatting.conditionalFormats ? location.content.conditionalFormatting.conditionalFormats : [];
  380. for (var _iterator6 = conditionalFormats, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
  381. var _ref6;
  382. if (_isArray6) {
  383. if (_i6 >= _iterator6.length) break;
  384. _ref6 = _iterator6[_i6++];
  385. } else {
  386. _i6 = _iterator6.next();
  387. if (_i6.done) break;
  388. _ref6 = _i6.value;
  389. }
  390. var conditionalFormat = _ref6;
  391. resultSet.push(conditionalFormat.dataItemId);
  392. }
  393. return resultSet;
  394. };
  395. RelinkConditionalFormatting.prototype.findDataItem = function findDataItem(path) {
  396. var _this13 = this;
  397. var resultSet = [];
  398. var items = path.items || [];
  399. items.forEach(function (item) {
  400. if (item.type === 'widget') {
  401. var itemIds = _this13._getConditionalFormattingItemIds(item);
  402. resultSet = resultSet.concat(itemIds);
  403. } else {
  404. resultSet = resultSet.concat(_this13.findDataItem(item));
  405. }
  406. });
  407. return resultSet;
  408. };
  409. RelinkConditionalFormatting.prototype.getItemIds = function getItemIds() {
  410. var resultSet = [];
  411. var itemIds = this.findDataItem(this.jsonSpec);
  412. resultSet = resultSet.concat(itemIds);
  413. return resultSet;
  414. };
  415. RelinkConditionalFormatting.prototype.replaceDataItem = function replaceDataItem(path, oldItemId, newItemId) {
  416. var _this14 = this;
  417. var items = path.items || [];
  418. items.forEach(function (item) {
  419. if (item.type === 'widget') {
  420. var conditionalFormats = item.content && item.content.conditionalFormatting && item.content.conditionalFormatting.conditionalFormats ? item.content.conditionalFormatting.conditionalFormats : [];
  421. conditionalFormats.forEach(function (conditionalFormat, index, collection) {
  422. replaceIdsInArray(conditionalFormat, collection, index, oldItemId, newItemId);
  423. });
  424. } else {
  425. _this14.replaceDataItem(item, oldItemId, newItemId);
  426. }
  427. });
  428. };
  429. RelinkConditionalFormatting.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) {
  430. this.replaceDataItem(this.jsonSpec, oldItemId, newItemId);
  431. };
  432. return RelinkConditionalFormatting;
  433. }(BaseRelinkItemId);
  434. var RelinkObjectClass = {
  435. 'pageContext': [RelinkPageContext],
  436. 'drillThrough': [RelinkDrillThrough],
  437. 'layout': [RelinkLayout, RelinkConditionalFormatting],
  438. 'widgets': [RelinkWidget],
  439. 'datasetShaping': [RelinkDatasetShaping] //@todo Investigate this: is it still needed? }
  440. };
  441. var RelinkItemIdInSpec = function () {
  442. function RelinkItemIdInSpec() {
  443. var jsonSpec = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  444. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  445. var logger = arguments[2];
  446. _classCallCheck(this, RelinkItemIdInSpec);
  447. if (!options.sourceId) {
  448. throw new Error('Invalid sourceId provided');
  449. }
  450. if (!options.oldCMAssetId) {
  451. throw new Error('Invalid oldCMAssetId provided');
  452. }
  453. if (!options.newCMAssetId) {
  454. throw new Error('Invalid newCMAssetId provided');
  455. }
  456. this.sourceId = options.sourceId;
  457. this.oldCMAssetId = options.oldCMAssetId;
  458. this.newCMAssetId = options.newCMAssetId;
  459. this.jsonSpec = jsonSpec;
  460. this.objectsToRelink = [];
  461. this.logger = logger;
  462. this._initialize();
  463. return {
  464. getItemIds: this.getItemIds.bind(this),
  465. replaceItemIds: this.replaceItemIds.bind(this)
  466. };
  467. }
  468. RelinkItemIdInSpec.prototype._initialize = function _initialize() {
  469. var _this15 = this;
  470. var keys = Object.keys(this.jsonSpec);
  471. keys.forEach(function (key) {
  472. var classesToInstantiate = RelinkObjectClass[key];
  473. if (classesToInstantiate) {
  474. classesToInstantiate.forEach(function (classToInstantiate) {
  475. return _this15.objectsToRelink.push(new classToInstantiate(_this15.jsonSpec[key], _this15.sourceId, _this15.oldCMAssetId, _this15.newCMAssetId));
  476. });
  477. }
  478. });
  479. };
  480. RelinkItemIdInSpec.prototype.getItemIds = function getItemIds() {
  481. var _this16 = this;
  482. if (this.itemIds) {
  483. return this.itemIds;
  484. }
  485. this.itemIds = [];
  486. this.objectsToRelink.forEach(function (objectToRelink) {
  487. _this16.itemIds = _this16.itemIds.concat(objectToRelink.getItemIds());
  488. });
  489. this.itemIds = _.uniq(this.itemIds);
  490. return this.itemIds;
  491. };
  492. RelinkItemIdInSpec.prototype.replaceItemIds = function replaceItemIds() {
  493. var _this17 = this;
  494. var newItemIds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  495. if (!this.itemIds || this.itemIds.length !== newItemIds.length) {
  496. throw new Error('Invalid item Ids to replace');
  497. }
  498. var nonUpdate_ableItemIds = [];
  499. var oldItemIds = this.itemIds;
  500. oldItemIds.forEach(function (oldItemId, index) {
  501. var newItemId = newItemIds[index];
  502. if (newItemId) {
  503. _this17.objectsToRelink.forEach(function (objectToRelink) {
  504. objectToRelink.replaceItemIds(oldItemId, newItemId);
  505. });
  506. } else {
  507. nonUpdate_ableItemIds.push(oldItemId);
  508. }
  509. });
  510. this.objectsToRelink.forEach(function (objectToRelink) {
  511. objectToRelink.postReplaceItemIds();
  512. });
  513. if (nonUpdate_ableItemIds.length > 0) {
  514. this.logger.info('Cannot update the following columns: \'' + nonUpdate_ableItemIds.join(',') + '\'');
  515. }
  516. return this.jsonSpec;
  517. };
  518. return RelinkItemIdInSpec;
  519. }();
  520. return RelinkItemIdInSpec;
  521. });
  522. //# sourceMappingURL=RelinkItemIdInSpec.js.map