@@ -155,6 +155,7 @@ function updateWithChildrenAndSharedKeys(data) {
155
155
* Builds a schema tree from the given schema.
156
156
* @param {Object } schema - The schema object containing "classes".
157
157
* @returns {Object|null } The schema tree object, or null if no "Container" classes are found.
158
+
158
159
*/
159
160
function buildSchemaTree ( schema ) {
160
161
@@ -424,26 +425,26 @@ export default class AppContext {
424
425
* @param {Object } schema_tree - The schema tree from which to create Data Harmonizers.
425
426
* @returns {Object } An object mapping the class names to their respective Data Harmonizer objects.
426
427
*/
427
- makeDataHarmonizersFromSchemaTree ( schema , template_name ) {
428
+ makeDHsFromSchemaTree ( schema , template_name ) {
428
429
let data_harmonizers = { } ;
429
- const template = this . appConfig . template_path . split ( '/' ) [ 1 ] ;
430
-
431
- console . log ( "SCHEMA TREE" , this . schema_tree )
430
+ //const template_name = this.appConfig.template_path.split('/')[1];
432
431
433
- Object . entries ( this . schema_tree )
432
+ Object . entries ( schema . classes )
434
433
// Container class is only used in input and output file coordination.
435
- . filter ( ( [ class_name ] ) => class_name !== 'Container' )
436
- // FOR NOW: 1-many involves getting all children of selected template too.
437
- //.filter(([class_name]) => class_name === template)
438
-
434
+ . filter ( ( [ class_name ] ) =>
435
+ // If given template_name found, build it
436
+ class_name == template_name
437
+ // Or given class is a child of template
438
+ || this . relations [ class_name ] ?. parent ?. [ template_name ]
439
+ // Or given class is a child of child of template etc.
440
+ // ...
441
+ )
439
442
. forEach ( ( obj , index ) => {
440
443
if ( obj . length > 0 ) {
441
444
const [ class_name , tree_obj ] = obj ;
442
445
443
446
// if it shares a key with another class which is its parent, this DH must be a child
444
- const is_child = tree_obj . shared_keys . some (
445
- ( shared_key_spec ) => shared_key_spec . relation === 'parent'
446
- ) ;
447
+ const is_child = this . relations ?. [ class_name ] ?. parent ;
447
448
448
449
// Doesn't build if tab id already exists.
449
450
const dhId = `data-harmonizer-grid-${ index } ` ;
@@ -479,7 +480,6 @@ export default class AppContext {
479
480
dh . useTemplate ( class_name ) ;
480
481
dh . validator . useTargetClass ( class_name ) ;
481
482
482
-
483
483
// Initialization of each child table is to hide all rows until
484
484
// parent primary key record is selected as a foreign key index.
485
485
if ( is_child ) {
@@ -506,45 +506,47 @@ export default class AppContext {
506
506
// the existence of a container class signifies a 1-M data loading setup with an ID join
507
507
/* e.g.
508
508
509
- const container = {
510
- "Container": {
511
- "name": "Container",
512
- "from_schema": "https://example.com/GRDI",
513
- "attributes": {
514
- "GRDISamples": {
515
- "name": "GRDISamples",
516
- "from_schema": "https://example.com/GRDI",
517
- "multivalued": true,
518
- "alias": "GRDI_samples",
519
- "owner": "Container",
520
- "domain_of": [
521
- "Container"
522
- ],
523
- "range": "GRDISample",
524
- "inlined_as_list": true
525
- },
526
- "AMRTests": {
527
- "name": "AMRTests",
528
- "from_schema": "https://example.com/GRDI",
529
- "multivalued": true,
530
- "alias": "AMR_Tests",
531
- "owner": "Container",
532
- "domain_of": [
533
- "Container"initializeTemplate
534
- ],
535
- "range": "AMRTest",
536
- "inlined_as_list": true
537
- }
538
- },
539
- "tree_root": true
540
- }
541
- }
542
-
509
+ Each class has one or more slots (attributes) which contain foreign_keys which
510
+ determine parent-child relationships.
511
+
512
+
543
513
const schema_tree = {
544
514
"Container": { tree_root: true, children: ["AMRTest", "GRDISample"] }
545
515
"GRDISample": { shared_key: ["sample_collector_sample_ID"], children: ["AMRTest"] }
546
516
"AMRTest": { shared_key: ["sample_collector_sample_ID"], children: [] },
547
517
}
518
+
519
+ "attributes": {
520
+ "sample_collector_sample_id": {
521
+ "name": "sample_collector_sample_id",
522
+ "annotations": {
523
+ "required": {
524
+ "tag": "required",
525
+ "value": true
526
+ },
527
+ "foreign_key": {
528
+ "tag": "foreign_key",
529
+ "value": "GRDISample.sample_collector_sample_id"
530
+ }
531
+ },
532
+ "range":
533
+
534
+ relations: {
535
+ [class_name]: {
536
+ slots: {[slot_name]: {parent: [table_name], slot: [slot_name]}}
537
+
538
+ parents: {
539
+ [key_name]: {
540
+ slot: ___ ,
541
+ parent: ___
542
+ }, ...
543
+ }
544
+ dependents: {
545
+ [child_name]: {slot1, slot2 ...}
546
+ }
547
+ }
548
+ }
549
+
548
550
549
551
*/
550
552
async setupDataHarmonizers ( {
@@ -571,14 +573,31 @@ export default class AppContext {
571
573
? this . template . localized . schema
572
574
: this . template . default . schema ;
573
575
576
+ this . relations = { } ;
577
+ //[class_name]: { slots: {[slot_name]: {parent: [table_name], slot: [slot_name]}}
578
+ Object . entries ( schema . classes ) . forEach ( ( [ class_name , class_obj ] ) => {
579
+ Object . entries ( class_obj . attributes ?? { } ) . forEach ( ( [ slot_name , attribute ] ) => {
580
+ if ( attribute . annotations ?. foreign_key ?. value ) {
581
+ let [ foreign_class , foreign_slot_name ] = attribute . annotations . foreign_key . value . split ( '.' , 2 ) ;
582
+ //Object.assign(this.relations, {[class_name]: {[slot_name]: {parent: foreign_class, slot: foreign_slot_name}}});
583
+ Object . assign ( this . relations , { [ class_name ] : { parent : { [ foreign_class ] : { [ slot_name ] : foreign_slot_name } } } } ) ;
584
+ //And reverse relation
585
+ Object . assign ( this . relations , { [ foreign_class ] : { child : { [ class_name ] : { [ foreign_slot_name ] : slot_name } } } } ) ;
586
+ }
587
+ } )
588
+ } ) ;
589
+
590
+ console . log ( "RELATIONS" , this . relations ) ;
591
+
574
592
this . schema_tree = buildSchemaTree ( schema ) ;
593
+ console . log ( "SCHEMA TREE" , this . schema_tree )
575
594
576
- let dhs = {
595
+ // Merges any existing dataharmonizer instances with the ones newly created.
596
+ this . dhs = {
577
597
...data_harmonizers ,
578
- ...this . makeDataHarmonizersFromSchemaTree ( schema , template_name ) ,
598
+ ...this . makeDHsFromSchemaTree ( schema , template_name ) ,
579
599
} ;
580
600
581
- this . dhs = dhs ;
582
601
// NOTE: Assumes that first dataharmonizer is starting point.
583
602
this . setCurrentDataHarmonizer ( Object . keys ( this . dhs ) [ 0 ] ) ;
584
603
0 commit comments