Skip to content

Commit

Permalink
task: logic to correct wrong relation directions if the result is uni…
Browse files Browse the repository at this point in the history
…quely found
  • Loading branch information
Dominick Leppich committed Nov 12, 2024
1 parent 444c4a0 commit b875428
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion migration/lib/mets_manipulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def process_vocabulary_reference_by_value(self, node):
value = node.text

search_field=None
inverse_search_field=None
if self.ctx.enable_relation_vocabulary_column_logic and 'Relationship' in vocabulary_name:
parent = node.getparent()
if parent == None:
Expand All @@ -180,10 +181,40 @@ def process_vocabulary_reference_by_value(self, node):
# use second column of vocabulary: `Reverse relationship` (The relation vocabulary is specified from `A->B`, the relation references an entity of type `A` and is therefore of type `B`)
if entity_type_position < separator_position:
search_field='Reverse relationship'
inverse_search_field='Relationship type'
else:
search_field='Relationship type'
inverse_search_field='Reverse relationship'

new_record_id = self.ctx.api.find_record(self.ctx, vocabulary_id, value, search_field=search_field)
try:
new_record_id = self.ctx.api.find_record(self.ctx, vocabulary_id, value, search_field=search_field)
except:
new_record_id = self.ctx.api.find_record(self.ctx, vocabulary_id, value, search_field=inverse_search_field)
old_value = node.text
record_data = self.ctx.api.lookup_record(new_record_id)

v = self.ctx.api.lookup_vocabulary(record_data['vocabularyId'])
s = self.ctx.api.lookup_schema(v['schemaId'])
ids = [d['id'] for d in s['definitions'] if d['name'] == search_field] # We need the value, that we actually originally searched for
if len(ids) != 1:
logging.critical(f'Non unique "{search_field}" fields found: {ids}!')
sys.exit(1)

field_data = [f for f in record_data['fields'] if f['definitionId'] == ids[0]]
if len(field_data) != 1:
logging.critical(f'Record [{new_record_id}] has no unique search column entry field')
sys.exit(1)

# Replace node text if not matching any translation of main value
translated_main_values = self.ctx.extract_language_values(field_data[0])
new_value = self.ctx.extract_preferred_language(translated_main_values)

#dump_node(node)
logging.warn(f'Relation is saved in the wrong direction, correct direction found and corrected: "{old_value}" -> "{new_value}"')
node.text = new_value

else:
new_record_id = self.ctx.api.find_record(self.ctx, vocabulary_id, value, search_field=None)

# Set all attributes accordingly
node.attrib['authority'] = vocabulary_name
Expand Down

0 comments on commit b875428

Please sign in to comment.