Skip to content

Commit b875428

Browse files
author
Dominick Leppich
committed
task: logic to correct wrong relation directions if the result is uniquely found
1 parent 444c4a0 commit b875428

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

migration/lib/mets_manipulator.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def process_vocabulary_reference_by_value(self, node):
158158
value = node.text
159159

160160
search_field=None
161+
inverse_search_field=None
161162
if self.ctx.enable_relation_vocabulary_column_logic and 'Relationship' in vocabulary_name:
162163
parent = node.getparent()
163164
if parent == None:
@@ -180,10 +181,40 @@ def process_vocabulary_reference_by_value(self, node):
180181
# 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`)
181182
if entity_type_position < separator_position:
182183
search_field='Reverse relationship'
184+
inverse_search_field='Relationship type'
183185
else:
184186
search_field='Relationship type'
187+
inverse_search_field='Reverse relationship'
185188

186-
new_record_id = self.ctx.api.find_record(self.ctx, vocabulary_id, value, search_field=search_field)
189+
try:
190+
new_record_id = self.ctx.api.find_record(self.ctx, vocabulary_id, value, search_field=search_field)
191+
except:
192+
new_record_id = self.ctx.api.find_record(self.ctx, vocabulary_id, value, search_field=inverse_search_field)
193+
old_value = node.text
194+
record_data = self.ctx.api.lookup_record(new_record_id)
195+
196+
v = self.ctx.api.lookup_vocabulary(record_data['vocabularyId'])
197+
s = self.ctx.api.lookup_schema(v['schemaId'])
198+
ids = [d['id'] for d in s['definitions'] if d['name'] == search_field] # We need the value, that we actually originally searched for
199+
if len(ids) != 1:
200+
logging.critical(f'Non unique "{search_field}" fields found: {ids}!')
201+
sys.exit(1)
202+
203+
field_data = [f for f in record_data['fields'] if f['definitionId'] == ids[0]]
204+
if len(field_data) != 1:
205+
logging.critical(f'Record [{new_record_id}] has no unique search column entry field')
206+
sys.exit(1)
207+
208+
# Replace node text if not matching any translation of main value
209+
translated_main_values = self.ctx.extract_language_values(field_data[0])
210+
new_value = self.ctx.extract_preferred_language(translated_main_values)
211+
212+
#dump_node(node)
213+
logging.warn(f'Relation is saved in the wrong direction, correct direction found and corrected: "{old_value}" -> "{new_value}"')
214+
node.text = new_value
215+
216+
else:
217+
new_record_id = self.ctx.api.find_record(self.ctx, vocabulary_id, value, search_field=None)
187218

188219
# Set all attributes accordingly
189220
node.attrib['authority'] = vocabulary_name

0 commit comments

Comments
 (0)