diff --git a/lib/DataHarmonizer.js b/lib/DataHarmonizer.js index 1bc3311e..6023a237 100644 --- a/lib/DataHarmonizer.js +++ b/lib/DataHarmonizer.js @@ -776,17 +776,43 @@ class DataHarmonizer { // Get the starting row index where the new rows will be added const startRowIndex = this.hot.countRows(); - // Insert the new rows below the last existing row - this.hot.alter('insert_row_below', startRowIndex, numRows); // Validate and process the current selection - if ( - this.context.currentSelection !== null && - this.context.currentSelection.valueToMatch !== null - ) { - // Find the nearest index after the last non-empty row in the specified column - this.populateNewRows(numRows, startRowIndex); + const is1mAndHasParent = (class_assignment) => { + console.log(this.context.currentSelection, this.context.oneToManyAppContext.appContext[class_assignment]); + const unique_keys = this.context.oneToManyAppContext.appContext[class_assignment].unique_keys; + for (let key in unique_keys) { + if ('foreign_key' in unique_keys[key]) { + return unique_keys[key].foreign_key; + } + } + return false; + }; + const classIsForeignKeyForClassAndCurrentSelection = (maybe_child_class, currentSelection) => { + const unique_keys = this.context.oneToManyAppContext.appContext[maybe_child_class].unique_keys; + return !isEmptyUnitVal(unique_keys[currentSelection.shared_key_name]) && maybe_child_class !== currentSelection.source; + + } + // check if the DH refers to a parent class or class with no children. + // if it has a parent, ensure a foreign key is selected in some parent + // if it doesn't have a parent, use regular add rows implementation + if (is1mAndHasParent(this.class_assignment)) { + if ( + !isEmptyUnitVal(this.context.currentSelection) && + !isEmptyUnitVal(this.context.currentSelection.valueToMatch) && + classIsForeignKeyForClassAndCurrentSelection(this.class_assignment, this.context.currentSelection) + ) { + + // Insert the new rows below the last existing row + this.hot.alter('insert_row_below', startRowIndex, numRows); + // Find the nearest index after the last non-empty row in the specified column + this.populateNewRows(numRows, startRowIndex); + } else { + console.warn('No current selection to populate the new rows.'); + $('#empty-parent-key-modal').modal('show'); + } } else { - console.warn('No current selection to populate the new rows.'); + // Insert the new rows below the last existing row + this.hot.alter('insert_row_below', startRowIndex, numRows); } } diff --git a/lib/Toolbar.js b/lib/Toolbar.js index b69031f2..7f16b1b6 100644 --- a/lib/Toolbar.js +++ b/lib/Toolbar.js @@ -35,6 +35,7 @@ import pkg from '../package.json'; const VERSION = pkg.version; + class Toolbar { constructor(root, context, options = {}) { this.context = context;