@@ -158,6 +158,7 @@ def process_vocabulary_reference_by_value(self, node):
158
158
value = node .text
159
159
160
160
search_field = None
161
+ inverse_search_field = None
161
162
if self .ctx .enable_relation_vocabulary_column_logic and 'Relationship' in vocabulary_name :
162
163
parent = node .getparent ()
163
164
if parent == None :
@@ -180,10 +181,40 @@ def process_vocabulary_reference_by_value(self, node):
180
181
# 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`)
181
182
if entity_type_position < separator_position :
182
183
search_field = 'Reverse relationship'
184
+ inverse_search_field = 'Relationship type'
183
185
else :
184
186
search_field = 'Relationship type'
187
+ inverse_search_field = 'Reverse relationship'
185
188
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 )
187
218
188
219
# Set all attributes accordingly
189
220
node .attrib ['authority' ] = vocabulary_name
0 commit comments