'use strict'; 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. 2017, 2018 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['underscore', './SynchContextHelper', '../../utils/DatasourceUtil'], function (_, SynchContextHelper, DatasourceUtil) { var getMetadataColumnsCallback = function getMetadataColumnsCallback() { return true; }; var isString = function isString(value) { return typeof value === 'string'; }; var isNumber = function isNumber(value) { return Number.isInteger(value); }; var compareValue = function compareValue(value1, value2) { if (isString(value1) && isString(value2) || isNumber(value1) && isNumber(value2)) { return value1 === value2; } else { var numericValue1 = isString(value1) ? parseInt(value1, 10) : value1; var numericValue2 = isString(value2) ? parseInt(value2, 10) : value2; return numericValue1 === numericValue2; } }; var SynchronizeDataHelper = function () { function SynchronizeDataHelper() { _classCallCheck(this, SynchronizeDataHelper); } /** * Retrieve and return a matched column by name. * * Order of matching by column name precedence: * * 1. If there a projected item that matched the column name return that column * OR * 2. If there is an column name that matched and is in one of the projected tables return that column * OR * 3. Return the first matched column name found regardless which table it belongs to * * @param {object} sourceModule - the source module * @param {object} targetModule - the target module * @param {object} column - the column whose's name the target module attempt to match * @param {object} context - context provied to match the column by name * * @return {object} mathed column if one found else return undefined */ SynchronizeDataHelper.matchColumByName = function matchColumByName(sourceModule, targetModule, column) { var context = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; var tableRef; var nameToMatch = column.getLabel().toLowerCase(); var sourceSourceId = sourceModule.getSourceId(); var targetSourceId = targetModule.getSourceId(); //1. if there is an existing column projected then map to that column first if (!context.projectedItems || context.projectedItems.length === 0) { throw new Error('cannot match column when no items are projected'); } var matchedColumn = _.find(context.projectedItems, function (projectedItem) { return projectedItem.getLabel().toLowerCase() === nameToMatch; }); if (matchedColumn) { return targetModule.getMetadataColumn(matchedColumn.getItemId()); } else { //Get the tableRef location to match later tableRef = DatasourceUtil.getTableRef(targetModule, context.projectedItems.map(function (item) { return item.getItemId(); })); } //Not found in 1 so now search over the module and return the first one found that matches the column name var columns = targetModule.getMetadataColumns(getMetadataColumnsCallback); for (var index = 0; index < columns.length; index++) { var columnToCheck = columns[index]; var check = false; if (sourceSourceId !== targetSourceId) { check = true; } else if (column.getTableName() !== columnToCheck.getTableName()) { if (!DatasourceUtil.haveTableJoinsInSameDataSource(targetModule, [column.getTableName()], [columnToCheck.getTableName()])) { //This check is to ensure the columnToCheck belongs to the same set of tables of the projected items, //Otherwise run into found arbitrary table wich may not be joined with set of tables resulint in DSS throw exception check = tableRef.indexOf(columnToCheck.getTableName()) !== -1; } } if (check && columnToCheck.getLabel().toLocaleLowerCase() === nameToMatch) { return columnToCheck; } } }; SynchronizeDataHelper.matchItemByValue = function matchItemByValue(values, value) { for (var index = 0; index < values.length; index++) { //Example; compare Year '2010' and Year 2010 are equal if (compareValue(value.d, values[index].d)) { value.u = values[index].u; return true; } } return false; }; SynchronizeDataHelper.clone = function clone(instance) { return JSON.parse(JSON.stringify(instance)); }; SynchronizeDataHelper.mergeDuplicates = function mergeDuplicates(sourceId, tableRef, synchronizeItems, synchronizeDataEntries) { _.each(synchronizeItems, function (synchronizeItem) { var items = synchronizeItem.items; _.each(items, function (item) { var otherSynchronizeDataEntry = synchronizeDataEntries[SynchContextHelper.getKey(item.sourceId, item.tableRef)]; if (otherSynchronizeDataEntry) { var otherSynchronizeItems = otherSynchronizeDataEntry.getSynchronizeItems(); var foundSynchronizeItem = _.find(otherSynchronizeItems, function (otherSynchronizeItem) { return otherSynchronizeItem.itemId === item.itemId; }); if (foundSynchronizeItem) { var foundIndex; var otherItems = foundSynchronizeItem.items; var duplicate = _.find(otherItems, function (otherItem, index) { if (otherItem.sourceId === sourceId && otherItem.itemId === synchronizeItem.itemId && DatasourceUtil.haveATableReference(tableRef, otherItem.tableRef, true)) { foundIndex = index; return true; } return false; }); if (duplicate) { otherItems.splice(foundIndex, 1); //remove it } } } }); }); }; SynchronizeDataHelper.isBoolean = function isBoolean(objectToVerify) { return typeof objectToVerify == 'boolean' || objectToVerify instanceof Boolean; }; return SynchronizeDataHelper; }(); return SynchronizeDataHelper; }); //# sourceMappingURL=SynchronizeDataHelper.js.map