Skip to content

Commit 8f074ab

Browse files
committed
testing code for this.relations
simpler version of this.schema_tree
1 parent 400c02e commit 8f074ab

File tree

2 files changed

+73
-50
lines changed

2 files changed

+73
-50
lines changed

lib/AppContext.js

+69-50
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ function updateWithChildrenAndSharedKeys(data) {
155155
* Builds a schema tree from the given schema.
156156
* @param {Object} schema - The schema object containing "classes".
157157
* @returns {Object|null} The schema tree object, or null if no "Container" classes are found.
158+
158159
*/
159160
function buildSchemaTree(schema) {
160161

@@ -424,26 +425,26 @@ export default class AppContext {
424425
* @param {Object} schema_tree - The schema tree from which to create Data Harmonizers.
425426
* @returns {Object} An object mapping the class names to their respective Data Harmonizer objects.
426427
*/
427-
makeDataHarmonizersFromSchemaTree(schema, template_name) {
428+
makeDHsFromSchemaTree(schema, template_name) {
428429
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];
432431

433-
Object.entries(this.schema_tree)
432+
Object.entries(schema.classes)
434433
// 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+
)
439442
.forEach((obj, index) => {
440443
if (obj.length > 0) {
441444
const [class_name, tree_obj] = obj;
442445

443446
// 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;
447448

448449
// Doesn't build if tab id already exists.
449450
const dhId = `data-harmonizer-grid-${index}`;
@@ -479,7 +480,6 @@ export default class AppContext {
479480
dh.useTemplate(class_name);
480481
dh.validator.useTargetClass(class_name);
481482

482-
483483
// Initialization of each child table is to hide all rows until
484484
// parent primary key record is selected as a foreign key index.
485485
if (is_child) {
@@ -506,45 +506,47 @@ export default class AppContext {
506506
// the existence of a container class signifies a 1-M data loading setup with an ID join
507507
/* e.g.
508508
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+
543513
const schema_tree = {
544514
"Container": { tree_root: true, children: ["AMRTest", "GRDISample"] }
545515
"GRDISample": { shared_key: ["sample_collector_sample_ID"], children: ["AMRTest"] }
546516
"AMRTest": { shared_key: ["sample_collector_sample_ID"], children: [] },
547517
}
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+
548550
549551
*/
550552
async setupDataHarmonizers({
@@ -571,14 +573,31 @@ export default class AppContext {
571573
? this.template.localized.schema
572574
: this.template.default.schema;
573575

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+
574592
this.schema_tree = buildSchemaTree(schema);
593+
console.log("SCHEMA TREE", this.schema_tree)
575594

576-
let dhs = {
595+
// Merges any existing dataharmonizer instances with the ones newly created.
596+
this.dhs = {
577597
...data_harmonizers,
578-
...this.makeDataHarmonizersFromSchemaTree(schema, template_name),
598+
...this.makeDHsFromSchemaTree(schema, template_name),
579599
};
580600

581-
this.dhs = dhs;
582601
// NOTE: Assumes that first dataharmonizer is starting point.
583602
this.setCurrentDataHarmonizer(Object.keys(this.dhs)[0]);
584603

lib/DataHarmonizer.js

+4
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ class DataHarmonizer {
476476
Object.assign(new_field, this.field_settings[name]);
477477
}
478478

479+
if (field.annotations) {
480+
new_field.annotations = field.annotations;
481+
}
482+
479483
section.children.push(new_field);
480484
} // End slot processing loop
481485

0 commit comments

Comments
 (0)