Skip to content

Commit b30031c

Browse files
author
Dominick Leppich
committed
fix: don't invert value for A -> A relationships
1 parent 89d2abf commit b30031c

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

migration/lib/mets_manipulator.py

+28-21
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def process_vocabulary_reference_by_value(self, node):
171171

172172
search_field=None
173173
inverse_search_field=None
174+
perform_inversion_fix=False
174175
if self.ctx.enable_relation_vocabulary_column_logic and 'Relationship' in vocabulary_name:
175176
parent = node.getparent()
176177
if parent == None:
@@ -197,6 +198,8 @@ def process_vocabulary_reference_by_value(self, node):
197198
else:
198199
search_field='Relationship type'
199200
inverse_search_field='Reverse relationship'
201+
perform_inversion_fix=True
202+
200203
elif entity_type_in_relation_count == 2:
201204
search_field='Relationship type'
202205
inverse_search_field='Reverse relationship'
@@ -210,27 +213,31 @@ def process_vocabulary_reference_by_value(self, node):
210213
# If failed, try to find the value in the other column (assuming the value was stored incorrectly)
211214
new_record_id = self.ctx.api.find_record(self.ctx, vocabulary_id, value, search_field=inverse_search_field)
212215
old_value = node.text
213-
record_data = self.ctx.api.lookup_record(new_record_id)
214-
215-
v = self.ctx.api.lookup_vocabulary(record_data['vocabularyId'])
216-
s = self.ctx.api.lookup_schema(v['schemaId'])
217-
ids = [d['id'] for d in s['definitions'] if d['name'] == search_field] # We need the value, that we actually originally searched for
218-
if len(ids) != 1:
219-
logging.critical(f'Non unique "{search_field}" fields found: {ids}!')
220-
sys.exit(1)
221-
222-
field_data = [f for f in record_data['fields'] if f['definitionId'] == ids[0]]
223-
if len(field_data) != 1:
224-
logging.critical(f'Record [{new_record_id}] has no unique search column entry field')
225-
sys.exit(1)
226-
227-
# Replace node text if not matching any translation of main value
228-
translated_main_values = self.ctx.extract_language_values(field_data[0])
229-
new_value = self.ctx.extract_preferred_language(translated_main_values)
230-
231-
#dump_node(node)
232-
logging.warn(f'Relation [{vocabulary_name}] is saved in the wrong direction, correct direction found and corrected: "{old_value}" -> "{new_value}"')
233-
node.text = new_value
216+
217+
if perform_inversion_fix:
218+
record_data = self.ctx.api.lookup_record(new_record_id)
219+
220+
v = self.ctx.api.lookup_vocabulary(record_data['vocabularyId'])
221+
s = self.ctx.api.lookup_schema(v['schemaId'])
222+
ids = [d['id'] for d in s['definitions'] if d['name'] == search_field] # We need the value, that we actually originally searched for
223+
if len(ids) != 1:
224+
logging.critical(f'Non unique "{search_field}" fields found: {ids}!')
225+
sys.exit(1)
226+
227+
field_data = [f for f in record_data['fields'] if f['definitionId'] == ids[0]]
228+
if len(field_data) != 1:
229+
logging.critical(f'Record [{new_record_id}] has no unique search column entry field')
230+
sys.exit(1)
231+
232+
# Replace node text if not matching any translation of main value
233+
translated_main_values = self.ctx.extract_language_values(field_data[0])
234+
new_value = self.ctx.extract_preferred_language(translated_main_values)
235+
236+
#dump_node(node)
237+
logging.warn(f'Relation [{vocabulary_name}] is saved in the wrong direction, correct direction found and corrected: "{old_value}" -> "{new_value}"')
238+
node.text = new_value
239+
else:
240+
logging.debug(f'Relation [{vocabulary_name}] value "{value}" found in column "{inverse_search_field}", keeping as is')
234241

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

0 commit comments

Comments
 (0)