@@ -776,17 +776,43 @@ class DataHarmonizer {
776
776
// Get the starting row index where the new rows will be added
777
777
const startRowIndex = this . hot . countRows ( ) ;
778
778
779
- // Insert the new rows below the last existing row
780
- this . hot . alter ( 'insert_row_below' , startRowIndex , numRows ) ;
781
779
// Validate and process the current selection
782
- if (
783
- this . context . currentSelection !== null &&
784
- this . context . currentSelection . valueToMatch !== null
785
- ) {
786
- // Find the nearest index after the last non-empty row in the specified column
787
- this . populateNewRows ( numRows , startRowIndex ) ;
780
+ const is1mAndHasParent = ( class_assignment ) => {
781
+ console . log ( this . context . currentSelection , this . context . oneToManyAppContext . appContext [ class_assignment ] ) ;
782
+ const unique_keys = this . context . oneToManyAppContext . appContext [ class_assignment ] . unique_keys ;
783
+ for ( let key in unique_keys ) {
784
+ if ( 'foreign_key' in unique_keys [ key ] ) {
785
+ return unique_keys [ key ] . foreign_key ;
786
+ }
787
+ }
788
+ return false ;
789
+ } ;
790
+ const classIsForeignKeyForClassAndCurrentSelection = ( maybe_child_class , currentSelection ) => {
791
+ const unique_keys = this . context . oneToManyAppContext . appContext [ maybe_child_class ] . unique_keys ;
792
+ return ! isEmptyUnitVal ( unique_keys [ currentSelection . shared_key_name ] ) && maybe_child_class !== currentSelection . source ;
793
+
794
+ }
795
+ // check if the DH refers to a parent class or class with no children.
796
+ // if it has a parent, ensure a foreign key is selected in some parent
797
+ // if it doesn't have a parent, use regular add rows implementation
798
+ if ( is1mAndHasParent ( this . class_assignment ) ) {
799
+ if (
800
+ ! isEmptyUnitVal ( this . context . currentSelection ) &&
801
+ ! isEmptyUnitVal ( this . context . currentSelection . valueToMatch ) &&
802
+ classIsForeignKeyForClassAndCurrentSelection ( this . class_assignment , this . context . currentSelection )
803
+ ) {
804
+
805
+ // Insert the new rows below the last existing row
806
+ this . hot . alter ( 'insert_row_below' , startRowIndex , numRows ) ;
807
+ // Find the nearest index after the last non-empty row in the specified column
808
+ this . populateNewRows ( numRows , startRowIndex ) ;
809
+ } else {
810
+ console . warn ( 'No current selection to populate the new rows.' ) ;
811
+ $ ( '#empty-parent-key-modal' ) . modal ( 'show' ) ;
812
+ }
788
813
} else {
789
- console . warn ( 'No current selection to populate the new rows.' ) ;
814
+ // Insert the new rows below the last existing row
815
+ this . hot . alter ( 'insert_row_below' , startRowIndex , numRows ) ;
790
816
}
791
817
}
792
818
0 commit comments