Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit 9166349

Browse files
committed
Added sorting of bods statements so dependencies appear first
1 parent 7c0b617 commit 9166349

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

app/controllers/entities_controller.rb

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ def show
105105
entity.merged_entities.map(&:bods_statement)
106106
].compact.flatten.uniq { |s| s.statementID }
107107

108+
statements = BodsStatementSorter.new.sort_statements(statements)
109+
108110
render json: JSON.pretty_generate(statements.as_json)
109111
end
110112
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'register_sources_bods/enums/statement_types'
2+
3+
class BodsStatementSorter
4+
# accepts array of statements
5+
# returns sorted by publication date, with any statements referred to by relationships
6+
def sort_statements(statements)
7+
statements = statements.sort_by { |statement| statement.publicationDetails&.publicationDate }
8+
9+
statements_by_id = statements.map { |statement| [statement.statementID, statement] }.to_h
10+
11+
used_ids = Set.new
12+
new_statements = []
13+
14+
while new_statements.length < statements.length
15+
current_new_statement_count = new_statements.length
16+
17+
statements.each do |statement|
18+
next if used_ids.include?(statement.statementID)
19+
20+
replaced_ids = statement.replacesStatements || []
21+
22+
dependent_ids =
23+
case statement.statementType
24+
when RegisterSourcesBods::StatementTypes['personStatement'], RegisterSourcesBods::StatementTypes['entityStatement']
25+
[]
26+
when RegisterSourcesBods::StatementTypes['ownershipOrControlStatement']
27+
[
28+
statement.subject&.describedByEntityStatement,
29+
statement.interestedParty&.describedByEntityStatement,
30+
statement.interestedParty&.describedByPersonStatement
31+
].compact
32+
end
33+
34+
all_dependent = (replaced_ids + dependent_ids).compact.uniq
35+
36+
all_dependencies_satisfied = all_dependent.all? { |dependency_id| used_ids.include? dependency_id }
37+
38+
next unless all_dependencies_satisfied
39+
40+
new_statements << statement
41+
used_ids << statement.statementID
42+
end
43+
44+
if current_new_statement_count == new_statements.count
45+
# This only happens when the level limiting means that there are relationship statements
46+
# without the entity dependency being in this statement list
47+
# In this scenario, these relationship statements should be skipped, so just stop here
48+
break
49+
end
50+
end
51+
52+
new_statements
53+
end
54+
end

0 commit comments

Comments
 (0)