|
9 | 9 | import java.util.Objects; |
10 | 10 | import java.util.function.Function; |
11 | 11 | import java.util.stream.Collectors; |
| 12 | +import java.util.stream.Stream; |
12 | 13 | import lombok.extern.log4j.Log4j2; |
13 | 14 | import org.folio.anonymization.domain.db.ModuleTable; |
14 | 15 | import org.folio.anonymization.domain.folio.Tenant; |
|
23 | 24 | @Repository |
24 | 25 | public class TenantRepository { |
25 | 26 |
|
| 27 | + // relational from Eureka |
26 | 28 | private static final Table<?> TENANT_TABLE = table(name("public", "tenant")); |
| 29 | + // JSON blob from Okapi |
| 30 | + private static final Table<?> TENANTS_TABLE = table(name("public", "tenants")); |
27 | 31 | private static final String MOD_CONSORTIA_KEYCLOAK_SCHEMA = "consortia_keycloak"; |
28 | 32 |
|
29 | 33 | @Autowired |
@@ -59,21 +63,48 @@ public Map<String, Tenant> getAllTenants() { |
59 | 63 | } |
60 | 64 |
|
61 | 65 | protected Map<String, Tenant> getTenantsWithoutConsortiaInfo() { |
62 | | - return create |
63 | | - .select(field("name"), field("description")) |
64 | | - .from(TENANT_TABLE) |
65 | | - .fetch() |
66 | | - .stream() |
67 | | - .map(record -> |
68 | | - new Tenant( |
69 | | - record.get("name", String.class), |
70 | | - record.get("name", String.class), |
71 | | - Objects.requireNonNullElse(record.get("description", String.class), ""), |
72 | | - null, |
73 | | - false |
74 | | - ) |
| 66 | + return Stream |
| 67 | + .concat( |
| 68 | + utilRepository.doesTableExist("public", "tenant") |
| 69 | + ? create |
| 70 | + .select(field("name"), field("description")) |
| 71 | + .from(TENANT_TABLE) |
| 72 | + .fetch() |
| 73 | + .stream() |
| 74 | + .map(record -> |
| 75 | + new Tenant( |
| 76 | + record.get("name", String.class), |
| 77 | + record.get("name", String.class), |
| 78 | + Objects.requireNonNullElse(record.get("description", String.class), ""), |
| 79 | + null, |
| 80 | + false |
| 81 | + ) |
| 82 | + ) |
| 83 | + : Stream.empty(), |
| 84 | + utilRepository.doesTableExist("public", "tenants") |
| 85 | + ? create |
| 86 | + .select( |
| 87 | + field("tenantjson->'descriptor'->>'id'").as("id"), |
| 88 | + field("tenantjson->'descriptor'->>'name'").as("name"), |
| 89 | + field("tenantjson->'descriptor'->>'description'").as("description") |
| 90 | + ) |
| 91 | + .from(TENANTS_TABLE) |
| 92 | + .where(field("tenantjson->'descriptor'->>'id'").ne("supertenant")) |
| 93 | + .fetch() |
| 94 | + .stream() |
| 95 | + .map(record -> |
| 96 | + new Tenant( |
| 97 | + record.get("id", String.class), |
| 98 | + record.get("name", String.class), |
| 99 | + Objects.requireNonNullElse(record.get("description", String.class), ""), |
| 100 | + null, |
| 101 | + false |
| 102 | + ) |
| 103 | + ) |
| 104 | + : Stream.empty() |
75 | 105 | ) |
76 | | - .collect(Collectors.toMap(Tenant::id, Function.identity())); |
| 106 | + // tenants table has better data than tenant table, so use its data when possible |
| 107 | + .collect(Collectors.toMap(Tenant::id, Function.identity(), (a, b) -> b)); |
77 | 108 | } |
78 | 109 |
|
79 | 110 | public List<ModuleTable> getModuleTablesWithSizes(String tenantId) { |
|
0 commit comments