Skip to content

Commit

Permalink
feat(conditional mapping fields) : add constraint check
Browse files Browse the repository at this point in the history
add destination in conditions field column check

Fix(mapping) fixing additional fields and selected mapping
  • Loading branch information
jacquesfize committed Jul 25, 2024
1 parent 495ab79 commit 8e913c7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"""

from alembic import op
from geonature.core.imports.models import BibFields, Destination
import sqlalchemy as sa
from sqlalchemy.orm.session import Session


# revision identifiers, used by Alembic.
Expand All @@ -29,7 +31,63 @@ def upgrade():
column=sa.Column("optional_conditions", sa.ARRAY(sa.Unicode)),
)

op.execute(
"""
CREATE OR REPLACE FUNCTION gn_imports.isInNameFields (
fields TEXT[],destination_id INTEGER
)
RETURNS BOOLEAN
AS $$
DECLARE
name_field_other TEXT;
BEGIN
IF fields IS DISTINCT FROM NULL THEN
FOREACH name_field_other IN ARRAY fields LOOP
IF NOT EXISTS (
SELECT *
FROM gn_imports.bib_fields
WHERE name_field = name_field_other AND id_destination = destination_id
) then
return FALSE;
END IF;
END LOOP;
END IF;
return TRUE;
END;
$$ LANGUAGE plpgsql;
"""
)

op.execute(
"""
alter table gn_imports.bib_fields
ADD CONSTRAINT mandatory_conditions_field_exists CHECK (gn_imports.isInNameFields(mandatory_conditions,id_destination));
"""
)
op.execute(
"""
alter table gn_imports.bib_fields
ADD CONSTRAINT optional_conditions_field_exists CHECK (gn_imports.isInNameFields(optional_conditions,id_destination));
"""
)
session = Session(bind=op.get_bind())
synthese_dest_id = session.scalar(
sa.select(Destination.id_destination).where(Destination.code == "synthese")
)
op.execute(
sa.update(BibFields)
.where(BibFields.name_field == "WKT", BibFields.id_destination == synthese_dest_id)
.values(optional_conditions=["latitude", "longitude"], mandatory=True)
)
session.close()


def downgrade():

op.drop_constraint("mandatory_conditions_field_exists", "bib_fields", schema="gn_imports")
op.drop_constraint("optional_conditions_field_exists", "bib_fields", schema="gn_imports")
op.execute("DROP FUNCTION IF EXISTS gn_imports.isInNameFields")
op.drop_column(table_name="bib_fields", schema="gn_imports", column_name="mandatory_conditions")
op.drop_column(table_name="bib_fields", schema="gn_imports", column_name="optional_conditions")

op.execute(sa.update(BibFields).where(BibFields.name_field == "WKT").values(mandatory=False))
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export class MappingSelectionComponent implements OnInit {
if (!fieldMappings) return;
this.userFieldMappings = fieldMappings;
});
this.fieldMappingForm.setValue(null);
this._fm.currentFieldMapping.next(null);
this.fieldMappingSub = this.fieldMappingForm.valueChanges
.pipe(
// skip first empty value to avoid reseting the field form if importData as mapping:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ export class FieldMappingService {
return flattened;
}

manageValueChangeMulti(oldValue = [], newValue) {
for (let value of newValue ? newValue : []) {
if (!oldValue || !oldValue.includes(value)) {
this.onFieldMappingChange(value, null);
}
}

for (let value of oldValue ? oldValue : []) {
if (!newValue || !newValue.includes(value)) {
this.onFieldMappingChange(null, value);
}
}
}

/**
* Populates the mapping form with target field data.
*
Expand All @@ -193,7 +207,8 @@ export class FieldMappingService {
control = this.mappingFormGroup.controls[name_field];
}
control.valueChanges.subscribe((vc) => {
this.onFieldMappingChange(name_field, vc, oldValue);
if (Array.isArray(vc)) this.manageValueChangeMulti(oldValue, vc);
else this.onFieldMappingChange(vc, oldValue);
oldValue = vc;
});

Expand Down Expand Up @@ -238,7 +253,6 @@ export class FieldMappingService {
unmapped: new Set(this.sourceFields),
autogenerated: new Set<string>(),
};
console.log(this.sourceFields);

if (fieldMapping === null) {
this.mappingFormGroup.reset();
Expand Down Expand Up @@ -292,7 +306,7 @@ export class FieldMappingService {
});
}

onFieldMappingChange(name_field: string, value: any, oldValue: any) {
onFieldMappingChange(value: any, oldValue: any) {
if (value) {
this.fieldMappingStatus.mapped.add(value);
this.fieldMappingStatus.unmapped.delete(value);
Expand Down

0 comments on commit 8e913c7

Please sign in to comment.