Skip to content

Commit

Permalink
[MODFQMMGR-409] Remove composite entity names from field labels (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncovercash authored Oct 25, 2024
1 parent fd3ce89 commit 31fed36
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private EntityType getFlattenedEntityType(UUID entityTypeId, EntityTypeSource so

flattenedEntityType.columns(getFilteredColumns(allColumns).toList());
flattenedEntityType.requiredPermissions(new ArrayList<>(finalPermissions));
return localizationService.localizeEntityType(flattenedEntityType, sourceFromParent == null);
return localizationService.localizeEntityType(flattenedEntityType);
}

private String injectSourceAliasIntoViewExtractor(String sourceViewExtractor, Map<String, String> renamedAliases) {
Expand Down
31 changes: 3 additions & 28 deletions src/main/java/org/folio/fqm/service/LocalizationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class LocalizationService {

// refers to the entity type as a whole in plural, e.g. "Users", "Purchase order lines"
private static final String ENTITY_TYPE_LABEL_TRANSLATION_TEMPLATE = "mod-fqm-manager.entityType.%s";
// refers to the shortened entity type as a whole, e.g. "Users", "POL", "Orgs"
private static final String ENTITY_TYPE_SHORTENED_LABEL_TRANSLATION_TEMPLATE = "mod-fqm-manager.entityType.%s._shortened";
// refers to a single column inside the entity type, e.g. "Name", "Barcode"
private static final String ENTITY_TYPE_COLUMN_AND_SOURCE_LABEL_TRANSLATION_TEMPLATE = "mod-fqm-manager.entityType.%s.%s";
// refers to a property inside an objectType column inside the entity type, e.g. "City" inside "Address" column inside "Users"
Expand All @@ -40,15 +38,15 @@ public class LocalizationService {

private TranslationService translationService;

public EntityType localizeEntityType(EntityType entityType, boolean isRootEntityType) {
public EntityType localizeEntityType(EntityType entityType) {
entityType.setLabelAlias(getEntityTypeLabel(entityType.getName()));

entityType.getColumns().forEach(column -> localizeEntityTypeColumn(entityType, column, isRootEntityType));
entityType.getColumns().forEach(column -> localizeEntityTypeColumn(entityType, column));

return entityType;
}

void localizeEntityTypeColumn(EntityType entityType, EntityTypeColumn column, boolean prependShortenedName) {
void localizeEntityTypeColumn(EntityType entityType, EntityTypeColumn column) {
if (column.getLabelAlias() == null) {
// Custom field names are already localized as they are user-defined, so they require special handling
if (Boolean.TRUE.equals(column.getIsCustomField())) {
Expand All @@ -62,29 +60,6 @@ void localizeEntityTypeColumn(EntityType entityType, EntityTypeColumn column, bo
} else if (column.getDataType() instanceof ArrayType arrayColumn) {
localizeArrayColumn(entityType, column, arrayColumn);
}
} else {
// column has been previously translated, so just append source translations to it
String sourceTranslation = getSourceTranslationPrefix(entityType, column.getName());
column.setLabelAlias(sourceTranslation + column.getLabelAlias());
}
if (prependShortenedName) {
String untranslated = ENTITY_TYPE_SHORTENED_LABEL_TRANSLATION_TEMPLATE.formatted(entityType.getName());
String shortenedName = translationService.format(ENTITY_TYPE_SHORTENED_LABEL_TRANSLATION_TEMPLATE.formatted(entityType.getName()));
if (shortenedName.equals(untranslated)) {
shortenedName = getEntityTypeLabel(entityType.getName());
}
column.setLabelAlias(shortenedName + " — " + column.getLabelAlias());
}
}

private String getSourceTranslationPrefix(EntityType entityType, String columnName) {
int currentSourceIndex = columnName.indexOf(".");
if (currentSourceIndex > 0) {
String currentSource = columnName.substring(0, currentSourceIndex);
String formattedKey = ENTITY_TYPE_COLUMN_AND_SOURCE_LABEL_TRANSLATION_TEMPLATE.formatted(entityType.getName(), currentSource);
return translationService.format(formattedKey) + " — ";
} else {
return "";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ void shouldFlattenSimpleEntityType() {
.requiredPermissions(List.of("simple_permission1", "simple_permission2"));

when(entityTypeRepository.getEntityTypeDefinition(SIMPLE_ENTITY_TYPE_ID, null)).thenReturn(Optional.of(copyEntityType(SIMPLE_ENTITY_TYPE)));
when(localizationService.localizeEntityType(any(EntityType.class), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
when(localizationService.localizeEntityType(any(EntityType.class))).thenAnswer(invocation -> invocation.getArgument(0));
when(executionContext.getTenantId()).thenReturn("tenant_01");
when(userTenantService.getUserTenantsResponse("tenant_01")).thenReturn("{'totalRecords': 0}");
EntityType actualEntityType = entityTypeFlatteningService.getFlattenedEntityType(SIMPLE_ENTITY_TYPE_ID, null);
Expand Down Expand Up @@ -629,7 +629,7 @@ void shouldFlattenComplexEntityType() {

when(entityTypeRepository.getEntityTypeDefinition(SIMPLE_ENTITY_TYPE_ID, null)).thenReturn(Optional.of(copyEntityType(SIMPLE_ENTITY_TYPE)));
when(entityTypeRepository.getEntityTypeDefinition(COMPLEX_ENTITY_TYPE_ID, null)).thenReturn(Optional.of(copyEntityType(COMPLEX_ENTITY_TYPE)));
when(localizationService.localizeEntityType(any(EntityType.class), anyBoolean())).thenAnswer(invocation -> {
when(localizationService.localizeEntityType(any(EntityType.class))).thenAnswer(invocation -> {
EntityType entityType = invocation.getArgument(0);
if (entityType.getSourceViewExtractor() == null) {
entityType.sourceViewExtractor(expectedSourceViewExtractor);
Expand Down Expand Up @@ -845,7 +845,7 @@ void shouldFlattenTripleNestedEntityType() {
when(entityTypeRepository.getEntityTypeDefinition(SIMPLE_ENTITY_TYPE_ID, null)).thenReturn(Optional.of(copyEntityType(SIMPLE_ENTITY_TYPE)));
when(entityTypeRepository.getEntityTypeDefinition(COMPLEX_ENTITY_TYPE_ID, null)).thenReturn(Optional.of(copyEntityType(COMPLEX_ENTITY_TYPE)));
when(entityTypeRepository.getEntityTypeDefinition(TRIPLE_NESTED_ENTITY_TYPE_ID, null)).thenReturn(Optional.of(copyEntityType(TRIPLE_NESTED_ENTITY_TYPE)));
when(localizationService.localizeEntityType(any(EntityType.class), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
when(localizationService.localizeEntityType(any(EntityType.class))).thenAnswer(invocation -> invocation.getArgument(0));
when(executionContext.getTenantId()).thenReturn("tenant_01");
when(userTenantService.getUserTenantsResponse("tenant_01")).thenReturn("{'totalRecords': 0}");

Expand All @@ -859,7 +859,7 @@ void shouldGetJoinClause() {

when(entityTypeRepository.getEntityTypeDefinition(SIMPLE_ENTITY_TYPE_ID, null)).thenReturn(Optional.of(copyEntityType(SIMPLE_ENTITY_TYPE)));
when(entityTypeRepository.getEntityTypeDefinition(COMPLEX_ENTITY_TYPE_ID, null)).thenReturn(Optional.of(copyEntityType(COMPLEX_ENTITY_TYPE)));
when(localizationService.localizeEntityType(any(EntityType.class), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
when(localizationService.localizeEntityType(any(EntityType.class))).thenAnswer(invocation -> invocation.getArgument(0));
when(executionContext.getTenantId()).thenReturn("tenant_01");
when(userTenantService.getUserTenantsResponse("tenant_01")).thenReturn("{'totalRecords': 0}");

Expand All @@ -874,7 +874,7 @@ void shouldReorderSourcesToMakeValidJoinClause() {
String expectedJoinClause = "source1_target \"source1\" JOIN source2_target \"source2\" ON \"source2\".field = \"source1\".field JOIN source3_target \"source3\" ON \"source3\".field = \"source2\".field JOIN source7_target \"source7\" ON \"source7\".field = \"source3\".field JOIN source4_target \"source4\" ON \"source4\".field = \"source3\".field JOIN source6_target \"source6\" ON \"source6\".field = \"source1\".field JOIN source5_target \"source5\" ON \"source5\".field = \"source4\".field";

when(entityTypeRepository.getEntityTypeDefinition(UNORDERED_ENTITY_TYPE_ID, null)).thenReturn(Optional.of(copyEntityType(UNORDERED_ENTITY_TYPE)));
when(localizationService.localizeEntityType(any(EntityType.class), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
when(localizationService.localizeEntityType(any(EntityType.class))).thenAnswer(invocation -> invocation.getArgument(0));
when(executionContext.getTenantId()).thenReturn("tenant_01");
when(userTenantService.getUserTenantsResponse("tenant_01")).thenReturn("{'totalRecords': 0}");

Expand Down Expand Up @@ -964,7 +964,7 @@ void shouldIncludeEcsColumnsWhenEcsIsEnabled() {
.requiredPermissions(List.of("simple_permission1", "simple_permission2"));

when(entityTypeRepository.getEntityTypeDefinition(SIMPLE_ENTITY_TYPE_ID, null)).thenReturn(Optional.of(copyEntityType(SIMPLE_ENTITY_TYPE)));
when(localizationService.localizeEntityType(any(EntityType.class), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
when(localizationService.localizeEntityType(any(EntityType.class))).thenAnswer(invocation -> invocation.getArgument(0));
when(executionContext.getTenantId()).thenReturn("tenant_01");
when(userTenantService.getUserTenantsResponse("tenant_01")).thenReturn("{'totalRecords': 1}");

Expand Down Expand Up @@ -1050,7 +1050,7 @@ void shouldFlattenSimpleEntityTypeWithSourceViewExtractor() {
when(entityTypeRepository.getEntityTypeDefinition(SIMPLE_ENTITY_TYPE_ID, null))
.thenReturn(Optional.of(copyEntityType(SIMPLE_ENTITY_TYPE_WITH_SOURCE_VIEW_EXTRACTOR)));

when(localizationService.localizeEntityType(any(EntityType.class), anyBoolean()))
when(localizationService.localizeEntityType(any(EntityType.class)))
.thenAnswer(invocation -> invocation.getArgument(0));

EntityType actualEntityType = entityTypeFlatteningService.getFlattenedEntityType(SIMPLE_ENTITY_TYPE_ID, null);
Expand Down
28 changes: 7 additions & 21 deletions src/test/java/org/folio/fqm/service/LocalizationServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ class LocalizationServiceTest {
private void testBasicEntityTypeFormatting(Map<String, String> translations,
String expectedTableTranslation,
String expectedColumnTranslation,
boolean isRootEntityType,
int numInvocations) {
translations.forEach((translationKey, translationValue) -> when(translationService.format(translationKey)).thenReturn(translationValue));

EntityType entityType = new EntityType()
.name("table_name")
.addColumnsItem(new EntityTypeColumn().name("column_name"));

EntityType actual = localizationService.localizeEntityType(entityType, isRootEntityType);
EntityType actual = localizationService.localizeEntityType(entityType);
assertEquals(expectedTableTranslation, actual.getLabelAlias());
assertEquals(expectedColumnTranslation, actual.getColumns().get(0).getLabelAlias());
verify(translationService, times(numInvocations)).format(anyString());
Expand All @@ -58,34 +57,21 @@ void testSimpleEntityTypeTranslations() {
"mod-fqm-manager.entityType.table_name.column_name", expectedColumnTranslation),
expectedTableTranslation,
expectedColumnTranslation,
false,
2);
}

@Test
void testSimpleEntityTypeRootTranslations() {
String expectedTableTranslationKey = "mod-fqm-manager.entityType.table_name";
String expectedTableShortenedTranslationKey = "mod-fqm-manager.entityType.table_name._shortened";
String expectedColumnTranslationKey = "mod-fqm-manager.entityType.table_name.column_name";
String expectedTableTranslation = "Table Name";
String expectedColumnTranslation = "Column Name";
testBasicEntityTypeFormatting(
Map.of(expectedTableTranslationKey, expectedTableTranslation,
expectedColumnTranslationKey, expectedColumnTranslation,
expectedTableShortenedTranslationKey, expectedTableShortenedTranslationKey), // Emulates the scenario where there is no translation
expectedColumnTranslationKey, expectedColumnTranslation),
expectedTableTranslation,
"Table Name — Column Name", // No shortened translation -> prepend the ET name
true,
4);

testBasicEntityTypeFormatting(
Map.of(expectedTableTranslationKey, expectedTableTranslation,
expectedColumnTranslationKey, expectedColumnTranslation,
expectedTableShortenedTranslationKey, "Table"), // Provide a shortened translation
expectedTableTranslation,
"Table — Column Name", // Shortened translation provided -> prepend it
true,
7);
"Column Name",
2);
}

@Test
Expand All @@ -100,7 +86,7 @@ void testCustomFieldFormatting() {
when(translationService.format(expectedTranslationKey, "customField", "Custom Field"))
.thenReturn("Test's Custom Field");

localizationService.localizeEntityTypeColumn(entityType, entityType.getColumns().get(0), false);
localizationService.localizeEntityTypeColumn(entityType, entityType.getColumns().get(0));

assertEquals(expectedTranslation, entityType.getColumns().get(0).getLabelAlias());

Expand Down Expand Up @@ -132,7 +118,7 @@ void testObjectTypeColumn() {
when(translationService.format(expectedInnerTranslationKey)).thenReturn(expectedInnerTranslation);
when(translationService.format(expectedInnerQualifiedTranslationKey)).thenReturn(expectedInnerQualifiedTranslation);

localizationService.localizeEntityTypeColumn(entityType, entityType.getColumns().get(0), false);
localizationService.localizeEntityTypeColumn(entityType, entityType.getColumns().get(0));

assertEquals(expectedOuterTranslation, entityType.getColumns().get(0).getLabelAlias());
assertEquals(
Expand Down Expand Up @@ -187,7 +173,7 @@ void testNestedObjectArrayTypeColumn() {
when(translationService.format(expectedInnerQualifiedTranslationKey)).thenReturn(expectedInnerQualifiedTranslation);
when(translationService.format(expectedInnermostQualifiedTranslationKey)).thenReturn(expectedInnermostQualifiedTranslation);

localizationService.localizeEntityTypeColumn(entityType, entityType.getColumns().get(0), false);
localizationService.localizeEntityTypeColumn(entityType, entityType.getColumns().get(0));

assertEquals(expectedOuterTranslation, entityType.getColumns().get(0).getLabelAlias());
assertEquals(expectedInnerTranslation, inner.getLabelAlias());
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/folio/fqm/utils/IdStreamerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void shouldFetchIdStreamForFql() {
List<String[]> ids = idsWithCancelCallback.ids();
ids.forEach(idSet -> actualIds.add(Arrays.asList(idSet)));
};
when(localizationService.localizeEntityType(any(EntityType.class), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
when(localizationService.localizeEntityType(any(EntityType.class))).thenAnswer(invocation -> invocation.getArgument(0));
when(executionContext.getTenantId()).thenReturn(tenantId);
when(userTenantService.getUserTenantsResponse(tenantId)).thenReturn(NON_ECS_USER_TENANT_JSON);
int idsCount = idStreamer.streamIdsInBatch(
Expand All @@ -158,7 +158,7 @@ void shouldUseAdditionalEcsConditionsInEcsEnvironment() {
List<String[]> ids = idsWithCancelCallback.ids();
ids.forEach(idSet -> actualIds.add(Arrays.asList(idSet)));
};
when(localizationService.localizeEntityType(any(EntityType.class), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
when(localizationService.localizeEntityType(any(EntityType.class))).thenAnswer(invocation -> invocation.getArgument(0));
when(userTenantService.getUserTenantsResponse(tenantId)).thenReturn(USER_TENANT_JSON);
when(ecsClient.get(eq("consortia/0e88ed41-eadb-44c3-a7a7-f6572bbe06fc/user-tenants"), anyMap())).thenReturn(USER_TENANT_JSON);
when(executionContext.getTenantId()).thenReturn("tenant_01");
Expand All @@ -185,7 +185,7 @@ void shouldUseUnionAllForCrossTenantQuery() {
List<String[]> ids = idsWithCancelCallback.ids();
ids.forEach(idSet -> actualIds.add(Arrays.asList(idSet)));
};
when(localizationService.localizeEntityType(any(EntityType.class), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
when(localizationService.localizeEntityType(any(EntityType.class))).thenAnswer(invocation -> invocation.getArgument(0));
when(executionContext.getTenantId()).thenReturn("tenant_01");
when(userTenantService.getUserTenantsResponse(tenantId)).thenReturn(USER_TENANT_JSON);
when(ecsClient.get(eq("consortia/0e88ed41-eadb-44c3-a7a7-f6572bbe06fc/user-tenants"), anyMap())).thenReturn(USER_TENANT_JSON);
Expand All @@ -211,7 +211,7 @@ void shouldHandleGroupByFields() {
List<String[]> ids = idsWithCancelCallback.ids();
ids.forEach(idSet -> actualIds.add(Arrays.asList(idSet)));
};
when(localizationService.localizeEntityType(any(EntityType.class), anyBoolean())).thenReturn(TEST_GROUP_BY_ENTITY_TYPE_DEFINITION);
when(localizationService.localizeEntityType(any(EntityType.class))).thenReturn(TEST_GROUP_BY_ENTITY_TYPE_DEFINITION);
when(executionContext.getTenantId()).thenReturn("tenant_01");
when(userTenantService.getUserTenantsResponse(tenantId)).thenReturn(NON_ECS_USER_TENANT_JSON);
int idsCount = idStreamer.streamIdsInBatch(
Expand Down
2 changes: 0 additions & 2 deletions translations/mod-fqm-manager/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"entityType.composite_po_instance.ship_to": "Ship to",
"entityType.composite_po_instance.ship_to_name": "Ship to - name",
"entityType.composite_purchase_order_lines": "Purchase order lines",
"entityType.composite_purchase_order_lines._shortened": "POL",
"entityType.composite_purchase_order_lines.assigned_to_user": "PO assigned to user",
"entityType.composite_purchase_order_lines.po": "PO",
"entityType.composite_purchase_order_lines.po_created_by_user": "PO created by user",
Expand Down Expand Up @@ -970,7 +969,6 @@
"entityType.simple_mode_of_issuance.id": "UUID",
"entityType.simple_mode_of_issuance.name": "Name",
"entityType.simple_organization": "Organizations",
"entityType.simple_organization._shortened": "Orgs",
"entityType.simple_organization.access_provider": "Access provider",
"entityType.simple_organization.accounting_code": "Accounting code",
"entityType.simple_organization.accounts": "Accounts",
Expand Down
2 changes: 0 additions & 2 deletions translations/mod-fqm-manager/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -1461,8 +1461,6 @@
"entityType.simple_mode_of_issuance.id": "UUID",
"entityType.simple_mode_of_issuance.name": "Name",
"entityType.simple_instance.instance_type_name": "Resource type",
"entityType.composite_purchase_order_lines._shortened": "POL",
"entityType.simple_organization._shortened": "Orgs",
"entityType.simple_instance.alternative_titles_ids": "Alternative titles - Type UUID",
"entityType.simple_instance.contributor_type_id": "Contributor type UUID",
"entityType.simple_instance.contributors_name": "Contributor name",
Expand Down

0 comments on commit 31fed36

Please sign in to comment.