SynchronizeDataHelper.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Watson Analytics (C) Copyright IBM Corp. 2017, 2018
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['underscore', './SynchContextHelper', '../../utils/DatasourceUtil'], function (_, SynchContextHelper, DatasourceUtil) {
  9. var getMetadataColumnsCallback = function getMetadataColumnsCallback() {
  10. return true;
  11. };
  12. var isString = function isString(value) {
  13. return typeof value === 'string';
  14. };
  15. var isNumber = function isNumber(value) {
  16. return Number.isInteger(value);
  17. };
  18. var compareValue = function compareValue(value1, value2) {
  19. if (isString(value1) && isString(value2) || isNumber(value1) && isNumber(value2)) {
  20. return value1 === value2;
  21. } else {
  22. var numericValue1 = isString(value1) ? parseInt(value1, 10) : value1;
  23. var numericValue2 = isString(value2) ? parseInt(value2, 10) : value2;
  24. return numericValue1 === numericValue2;
  25. }
  26. };
  27. var SynchronizeDataHelper = function () {
  28. function SynchronizeDataHelper() {
  29. _classCallCheck(this, SynchronizeDataHelper);
  30. }
  31. /**
  32. * Retrieve and return a matched column by name.
  33. *
  34. * Order of matching by column name precedence:
  35. *
  36. * 1. If there a projected item that matched the column name return that column
  37. * OR
  38. * 2. If there is an column name that matched and is in one of the projected tables return that column
  39. * OR
  40. * 3. Return the first matched column name found regardless which table it belongs to
  41. *
  42. * @param {object} sourceModule - the source module
  43. * @param {object} targetModule - the target module
  44. * @param {object} column - the column whose's name the target module attempt to match
  45. * @param {object} context - context provied to match the column by name
  46. *
  47. * @return {object} mathed column if one found else return undefined
  48. */
  49. SynchronizeDataHelper.matchColumByName = function matchColumByName(sourceModule, targetModule, column) {
  50. var context = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  51. var tableRef;
  52. var nameToMatch = column.getLabel().toLowerCase();
  53. var sourceSourceId = sourceModule.getSourceId();
  54. var targetSourceId = targetModule.getSourceId();
  55. //1. if there is an existing column projected then map to that column first
  56. if (!context.projectedItems || context.projectedItems.length === 0) {
  57. throw new Error('cannot match column when no items are projected');
  58. }
  59. var matchedColumn = _.find(context.projectedItems, function (projectedItem) {
  60. return projectedItem.getLabel().toLowerCase() === nameToMatch;
  61. });
  62. if (matchedColumn) {
  63. return targetModule.getMetadataColumn(matchedColumn.getItemId());
  64. } else {
  65. //Get the tableRef location to match later
  66. tableRef = DatasourceUtil.getTableRef(targetModule, context.projectedItems.map(function (item) {
  67. return item.getItemId();
  68. }));
  69. }
  70. //Not found in 1 so now search over the module and return the first one found that matches the column name
  71. var columns = targetModule.getMetadataColumns(getMetadataColumnsCallback);
  72. for (var index = 0; index < columns.length; index++) {
  73. var columnToCheck = columns[index];
  74. var check = false;
  75. if (sourceSourceId !== targetSourceId) {
  76. check = true;
  77. } else if (column.getTableName() !== columnToCheck.getTableName()) {
  78. if (!DatasourceUtil.haveTableJoinsInSameDataSource(targetModule, [column.getTableName()], [columnToCheck.getTableName()])) {
  79. //This check is to ensure the columnToCheck belongs to the same set of tables of the projected items,
  80. //Otherwise run into found arbitrary table wich may not be joined with set of tables resulint in DSS throw exception
  81. check = tableRef.indexOf(columnToCheck.getTableName()) !== -1;
  82. }
  83. }
  84. if (check && columnToCheck.getLabel().toLocaleLowerCase() === nameToMatch) {
  85. return columnToCheck;
  86. }
  87. }
  88. };
  89. SynchronizeDataHelper.matchItemByValue = function matchItemByValue(values, value) {
  90. for (var index = 0; index < values.length; index++) {
  91. //Example; compare Year '2010' and Year 2010 are equal
  92. if (compareValue(value.d, values[index].d)) {
  93. value.u = values[index].u;
  94. return true;
  95. }
  96. }
  97. return false;
  98. };
  99. SynchronizeDataHelper.clone = function clone(instance) {
  100. return JSON.parse(JSON.stringify(instance));
  101. };
  102. SynchronizeDataHelper.mergeDuplicates = function mergeDuplicates(sourceId, tableRef, synchronizeItems, synchronizeDataEntries) {
  103. _.each(synchronizeItems, function (synchronizeItem) {
  104. var items = synchronizeItem.items;
  105. _.each(items, function (item) {
  106. var otherSynchronizeDataEntry = synchronizeDataEntries[SynchContextHelper.getKey(item.sourceId, item.tableRef)];
  107. if (otherSynchronizeDataEntry) {
  108. var otherSynchronizeItems = otherSynchronizeDataEntry.getSynchronizeItems();
  109. var foundSynchronizeItem = _.find(otherSynchronizeItems, function (otherSynchronizeItem) {
  110. return otherSynchronizeItem.itemId === item.itemId;
  111. });
  112. if (foundSynchronizeItem) {
  113. var foundIndex;
  114. var otherItems = foundSynchronizeItem.items;
  115. var duplicate = _.find(otherItems, function (otherItem, index) {
  116. if (otherItem.sourceId === sourceId && otherItem.itemId === synchronizeItem.itemId && DatasourceUtil.haveATableReference(tableRef, otherItem.tableRef, true)) {
  117. foundIndex = index;
  118. return true;
  119. }
  120. return false;
  121. });
  122. if (duplicate) {
  123. otherItems.splice(foundIndex, 1); //remove it
  124. }
  125. }
  126. }
  127. });
  128. });
  129. };
  130. SynchronizeDataHelper.isBoolean = function isBoolean(objectToVerify) {
  131. return typeof objectToVerify == 'boolean' || objectToVerify instanceof Boolean;
  132. };
  133. return SynchronizeDataHelper;
  134. }();
  135. return SynchronizeDataHelper;
  136. });
  137. //# sourceMappingURL=SynchronizeDataHelper.js.map